summaryrefslogtreecommitdiff
path: root/puzzle.cpp
blob: 11b26931752dfd0fb24c9d78e1989fecd2115550 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 * Sudoku: A plug-in for the Video Disk Recorder
 *
 * Copyright (C) 2005-2010, Thomas Günther <tom@toms-cafe.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "puzzle.h"
#include "generator.h"
#include "history.h"
#include <string.h>
#include <assert.h>

using namespace Sudoku;


//--- class Sudoku::Numbers ----------------------------------------------------

/** Constructor */
Numbers::Numbers(const char* dump) :
  numbers_dump()
{
  load_from_dump(dump);

  assert(!numbers_dump);
}

/** Destructor */
Numbers::~Numbers()
{
  delete[] numbers_dump;
}

/** Get the numbers as dump */
const char* Numbers::get_dump() const
{
  if (!numbers_dump)
    numbers_dump = new char[SDIM + DIM];
  assert(numbers_dump);

  char* dump = numbers_dump;
  unsigned int n;
  for (unsigned int row = 0; row < DIM; ++row, *dump++ = '+')
    for (unsigned int col = 0; col < DIM; ++col, ++dump)
      *dump = (n = get(Pos(col, row))) ? '0' + n : '_';
  *--dump = 0;

  return numbers_dump;
}

/** Remove all numbers. */
void Numbers::reset()
{
  for (unsigned int i = 0; i < SDIM; ++i)
    content[i] = 0;
}

/** Set numbers from contents of sudoku if marked in marks. */
void Numbers::set_contents(const Numbers& sudoku, const bool marks[SDIM])
{
  for (unsigned int i = 0; i < SDIM; ++i)
    if (marks[i])
      content[i] = sudoku.get(i);
    else
      content[i] = 0;
}

/** Set the number into this cell. */
void Numbers::set(Pos pos, unsigned int number)
{
  assert(pos <= Pos::last());
  assert(0 <= number && number <= DIM);
  content[pos] = number;
}

/** Get the number from this cell. */
unsigned int Numbers::get(Pos pos) const
{
  assert(pos <= Pos::last());
  assert(0 <= content[pos] && content[pos] <= DIM);
  return content[pos];
}

/** Load numbers from a dump. */
void Numbers::load_from_dump(const char* dump)
{
  reset();

  if (dump)
    for (unsigned int i = 0; *dump && *dump != ':' && i < SDIM; ++i, ++dump)
      if (*dump == '+')
        --i;
      else if (*dump > '0' && *dump - '0' <= DIM)
        content[i] = *dump - '0';
}


//--- class Sudoku::Puzzle -----------------------------------------------------

/** Constructor */
Puzzle::Puzzle(const char* dump) :
  puzzle_dump()
{
  load_from_dump(dump);

  assert(!puzzle_dump);
}

/** Constructor with generation of a random puzzle */
Puzzle::Puzzle(unsigned int givens_count, bool symmetric) :
  puzzle_dump()
{
  if (givens_count == 0)
    clear_givens();
  else
    generate(givens_count, symmetric);

  assert(!puzzle_dump);
}

/** Destructor */
Puzzle::~Puzzle()
{
  delete[] puzzle_dump;
}

/** Get the puzzle as dump */
const char* Puzzle::get_dump() const
{
  if (!puzzle_dump)
    puzzle_dump = new char[3 * (SDIM + DIM)];
  assert(puzzle_dump);

  // Set givens as first part of the dump
  strcpy(puzzle_dump, givens.get_dump());
  if (!untouched())
  {
    // Set numbers as second part of the dump
    strcat(puzzle_dump, ":");
    strcat(puzzle_dump, Numbers::get_dump());

    // Set marks as third part of the dump
    strcat(puzzle_dump, ":");
    strcat(puzzle_dump, marks.get_dump());
  }

  return puzzle_dump;
}

/** Reset the puzzle (including marks). */
void Puzzle::reset()
{
  reset(true);
}

/** Reset the puzzle (either with or without marks). */
void Puzzle::reset(bool clear_marks)
{
  unsigned int i;

  // Fill the puzzle with the givens.
  for (i = 0; i < SDIM; ++i)
    Numbers::set(i, givens.get(i));

  // Compute possible numbers for all cells.
  for (i = 0; i < SDIM; ++i)
    compute_numbers(i);

  // Reset marked cells.
  if (clear_marks)
    marks.reset();
}

/** Set the number into this cell. */
void Puzzle::set(Pos pos, unsigned int number)
{
  assert(pos <= Pos::last());
  assert(0 <= number && number <= DIM);

  if (!given(pos) && get(pos) != number)
  {
    Numbers::set(pos, number);

    // Refresh possible numbers of all affected cells.
    for (Pos p = Pos::first(); p <= Pos::last(); p = p.next())
      if (p.interacts_with(pos))
        compute_numbers(p);
  }
}

/** Load a puzzle from a dump. */
void Puzzle::load_from_dump(const char* dump)
{
  // Set givens from the first part of the dump (before the first colon)
  givens.load_from_dump(dump);
  reset();

  // Set numbers from the second part (between the first and the second colon)
  if (dump)
    dump = strchr(dump, ':');
  if (dump)
  {
    Numbers numbers(++dump);
    for (unsigned int i = 0; i < SDIM; ++i)
      if (numbers.get(i) != 0)
        set(i, numbers.get(i));
  }

  // Set marks from the third part (behind the second colon)
  if (dump)
    dump = strchr(dump, ':');
  if (dump)
    marks.load_from_dump(++dump);
}

/** Generate a new puzzle. */
void Puzzle::generate(unsigned int givens_count, bool symmetric)
{
  // Search a random non-ambiguous puzzle.
  for (bool found = false; !found;)
  {
    Generator generator(*this, givens_count, symmetric, symmetric ? 500 : 2000);
    generator.find_next_solution();
    found = generator.solution_is_valid();
  }
  reset();
}

/** Set givens from contents of sudoku if marked in given_marks. */
void Puzzle::set_givens(const Numbers& sudoku, const bool given_marks[SDIM])
{
  givens.set_contents(sudoku, given_marks);
  reset();
}

/** Remove all givens. */
void Puzzle::clear_givens()
{
  givens.reset();
  reset();
}

/** No cells set? */
bool Puzzle::untouched() const
{
  for (unsigned int i = 0; i < SDIM; ++i)
    if (get(i) != givens.get(i))
      return false;

  return true;
}

/** Is the number in this cell given? */
bool Puzzle::given(Pos pos) const
{
  assert(pos <= Pos::last());
  return givens.get(pos) != 0;
}

/** Is there an error on this position? */
bool Puzzle::error(Pos pos) const
{
  assert(pos <= Pos::last());
  return !correct(pos);
}

/** Is the number in this cell ambiguous? */
bool Puzzle::ambiguous(Pos pos) const
{
  assert(pos <= Pos::last());
  return get(pos) != 0 && count[pos] > 1;
}

/** All cells set and no errors? */
bool Puzzle::solved() const
{
  for (unsigned int i = 0; i < SDIM; ++i)
    if (get(i) == 0 || !correct(i))
      return false;

  return true;
}

/** Is this cell marked? */
bool Puzzle::marked(Pos pos) const
{
  assert(pos <= Pos::last());
  return marks.get(pos) != 0;
}

/** Toggle the mark for this cell. */
void Puzzle::toggle_mark(Pos pos)
{
  assert(pos <= Pos::last());
  marks.set(pos, marks.get(pos) != 0 ? 0 : 1);
}

/** Get the next free cell with minimal possible numbers. */
Pos Puzzle::next_cell(Pos pos) const
{
  unsigned int min_count = DIM+1, min_index = SDIM, i;

  for (pos = (pos+1)%SDIM, i = 0; i < SDIM; ++i, pos = (pos+1)%SDIM)
    if (get(pos) == 0 && count[pos] < min_count)
      min_count = count[pos], min_index = pos;

  return min_index;
}

/** Get the next possible number for this cell. */
unsigned int Puzzle::next_number(Pos pos) const
{
  assert(pos <= Pos::last());
  unsigned int n = get(pos);
  unsigned int i;

  if (!given(pos))
    for (n = (n+1)%(DIM+1), i = 0; i < DIM+1; ++i, n = (n+1)%(DIM+1))
      if (numbers[pos][n])
        break;

  return n;
}

/** Get the count of possible numbers for this cell. */
unsigned int Puzzle::numbers_count(Pos pos) const
{
  assert(pos <= Pos::last());
  return count[pos];
}

/** Is this number in this cell a possible number? */
bool Puzzle::possible_number(Pos pos, unsigned int number) const
{
  assert(pos <= Pos::last());
  assert(0 < number && number <= DIM);
  return numbers[pos][number];
}

/** Compute all possible numbers for this cell. */
void Puzzle::compute_numbers(Pos pos)
{
  assert(pos <= Pos::last());
  unsigned int i;

  // Fill list with all numbers.
  for (i = 1; i <= DIM; ++i)
    numbers[pos][i] = true;

  // Disable numbers found in row, column or region.
  for (Pos p = Pos::first(); p <= Pos::last(); p = p.next())
    if (p.interacts_with(pos))
      numbers[pos][get(p)] = false;

  // Count the possible numbers.
  count[pos] = 0;
  for (i = 1; i <= DIM; ++i)
    if (numbers[pos][i])
      ++count[pos];

  // 0 is always possible.
  numbers[pos][0] = true;
}

/** Is the number in this cell a possible number? */
bool Puzzle::correct(Pos pos) const
{
  assert(pos <= Pos::last());
  return count[pos] != 0 && numbers[pos][get(pos)];
}


//--- class Sudoku::PuzzleGame -------------------------------------------------

/** Constructor */
PuzzleGame::PuzzleGame(const char* dump) :
  Puzzle(dump), pos(Pos::center()), history(new History())
{
  assert(history);
}

/** Constructor with generation of a random puzzle */
PuzzleGame::PuzzleGame(unsigned int givens_count, bool symmetric) :
  Puzzle(givens_count, symmetric), pos(Pos::center()), history(new History())
{
  assert(history);
}

/** Destructor */
PuzzleGame::~PuzzleGame()
{
  delete history;
}

/** Reset the puzzle (including marks). */
void PuzzleGame::reset()
{
  reset(true);
}

/** Reset the puzzle (either with or without marks). */
void PuzzleGame::reset(bool clear_marks)
{
  Puzzle::reset(clear_marks);
  if (history)
    history->reset();
}

/** Set the number into the current cell, write action into history. */
void PuzzleGame::set_with_history(unsigned int number)
{
  if (history && !given(pos) && get(pos) != number)
  {
    history->add(new SetMove(*this, number));
    history->current()->execute();
  }
  else
    set(pos, number);
}

/** Get the position of the current cell. */
Pos PuzzleGame::get_pos() const
{
  return pos;
}

/** Set the position of the current cell. */
void PuzzleGame::set_pos(Pos new_pos)
{
  pos = new_pos;
}

/** Go one step backward in the history. */
void PuzzleGame::backward()
{
  if (history->movesExecuted())
  {
    history->current()->takeBack();
    history->backward();
  }
}

/** Go one step forward in the history. */
void PuzzleGame::forward()
{
  if (history->movesToExecute())
  {
    history->forward();
    history->current()->execute();
  }
}