00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef VDR_SUDOKU_PUZZLE_H
00022 #define VDR_SUDOKU_PUZZLE_H
00023
00024 #include "sudoku.h"
00025
00026
00027
00028
00029
00030
00031
00032
00033 namespace Sudoku
00034 {
00035 class History;
00036
00037
00038
00039 enum
00040 {
00041
00042 RDIM = 3,
00043
00044
00045 DIM = RDIM * RDIM,
00046
00047
00048 SDIM = DIM * DIM
00049 };
00050
00051
00052
00053
00054
00055 class Pos
00056 {
00057 unsigned int pos;
00058 public:
00059 Pos(unsigned int col, unsigned int row) : pos(col + row * DIM) {}
00060 Pos(unsigned int pos = 0) : pos(pos) {}
00061 operator unsigned int() const { return pos; }
00062 static Pos first() { return 0; }
00063 static Pos last() { return SDIM-1; }
00064 Pos next() const { return pos + 1; }
00065 unsigned int col() const { return pos % DIM; }
00066 unsigned int row() const { return pos / DIM; }
00067 unsigned int reg() const { return (col() / RDIM) + RDIM * (row() / RDIM); }
00068 static Pos center() { return SDIM / 2; }
00069 Pos symmetric() const { return SDIM - 1 - pos; }
00070 Pos prev_col() const { return col() > 0 ? pos - 1 : pos; }
00071 Pos next_col() const { return col() < DIM-1 ? pos + 1 : pos; }
00072 Pos prev_row() const { return row() > 0 ? pos - DIM : pos; }
00073 Pos next_row() const { return row() < DIM-1 ? pos + DIM : pos; }
00074 bool interacts_with(Pos other) const { return pos != other &&
00075 (col() == other.col() || row() == other.row() || reg() == other.reg()); }
00076 };
00077
00078
00079
00080
00081
00082 class Numbers
00083 {
00084 unsigned int content[SDIM];
00085 mutable char* numbers_dump;
00086
00087
00088 Numbers(const Numbers& other);
00089 Numbers& operator=(const Numbers& other);
00090
00091 public:
00092
00093
00094 Numbers(const char* dump = 0);
00095
00096
00097 virtual ~Numbers();
00098
00099
00100 virtual const char* get_dump() const;
00101
00102
00103 virtual void reset();
00104
00105
00106 virtual void set_contents(const Numbers& sudoku, const bool marks[SDIM]);
00107
00108
00109 virtual void set(Pos pos, unsigned int number);
00110
00111
00112 virtual unsigned int get(Pos pos) const;
00113
00114
00115 virtual void load_from_dump(const char* dump);
00116 };
00117
00118
00119
00120
00121
00122 class Puzzle : public Numbers
00123 {
00124 Numbers givens;
00125 Numbers marks;
00126 bool numbers[SDIM][DIM+1];
00127 unsigned int count[SDIM];
00128 mutable char* puzzle_dump;
00129
00130
00131 Puzzle(const Puzzle& other);
00132 Puzzle& operator=(const Puzzle& other);
00133
00134 public:
00135
00136
00137 Puzzle(const char* dump = 0);
00138
00139
00140 Puzzle(unsigned int givens_count, bool symmetric = true);
00141
00142
00143 virtual ~Puzzle();
00144
00145
00146 virtual const char* get_dump() const;
00147
00148
00149 virtual void reset();
00150
00151
00152 virtual void reset(bool clear_marks);
00153
00154
00155 virtual void set(Pos pos, unsigned int number);
00156
00157
00158 virtual void load_from_dump(const char* dump);
00159
00160
00161 void generate(unsigned int givens_count, bool symmetric = true);
00162
00163
00164 void set_givens(const Numbers& sudoku, const bool given_marks[SDIM]);
00165
00166
00167 void clear_givens();
00168
00169
00170 bool untouched() const;
00171
00172
00173 bool given(Pos pos) const;
00174
00175
00176 bool error(Pos pos) const;
00177
00178
00179 bool ambiguous(Pos pos) const;
00180
00181
00182 bool solved() const;
00183
00184
00185 bool marked(Pos pos) const;
00186
00187
00188 void toggle_mark(Pos pos);
00189
00190
00191 Pos next_cell(Pos pos = Pos::last()) const;
00192
00193
00194 unsigned int next_number(Pos pos) const;
00195
00196
00197 unsigned int numbers_count(Pos pos) const;
00198
00199
00200 bool possible_number(Pos pos, unsigned int number) const;
00201
00202 private:
00203
00204
00205 void compute_numbers(Pos pos);
00206
00207
00208 bool correct(Pos pos) const;
00209 };
00210
00211
00212
00213
00214
00215 class PuzzleGame : public Puzzle
00216 {
00217 Pos pos;
00218 History* history;
00219
00220
00221 PuzzleGame(const PuzzleGame& other);
00222 PuzzleGame& operator=(const PuzzleGame& other);
00223
00224 public:
00225
00226
00227 PuzzleGame(const char* dump = 0);
00228
00229
00230 PuzzleGame(unsigned int givens_count, bool symmetric = true);
00231
00232
00233 virtual ~PuzzleGame();
00234
00235
00236 virtual void reset();
00237
00238
00239 virtual void reset(bool clear_marks);
00240
00241
00242 virtual void set_with_history(unsigned int number);
00243
00244
00245 Pos get_pos() const;
00246
00247
00248 void set_pos(Pos new_pos);
00249
00250
00251 void backward();
00252
00253
00254 void forward();
00255 };
00256
00257 }
00258
00259 #endif // VDR_SUDOKU_PUZZLE_H