forked from lksj/einstein-puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzgen.cpp
396 lines (327 loc) · 9.65 KB
/
puzgen.cpp
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
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <list>
#include "puzgen.h"
#include "exceptions.h"
#include "utils.h"
Possibilities::Possibilities()
{
reset();
}
Possibilities::Possibilities(std::istream &stream)
{
for (int row = 0; row < PUZZLE_SIZE; row++)
for (int col = 0; col < PUZZLE_SIZE; col++)
for (int element = 0; element < PUZZLE_SIZE; element++)
pos[col][row][element] = readInt(stream);
}
void Possibilities::reset()
{
for (int i = 0; i < PUZZLE_SIZE; i++)
for (int j = 0; j < PUZZLE_SIZE; j++)
for (int k = 0; k < PUZZLE_SIZE; k++)
pos[i][j][k] = k + 1;
}
void Possibilities::checkSingles(int row)
{
int cellsCnt[PUZZLE_SIZE]; // count of elements in cells
int elsCnt[PUZZLE_SIZE]; // total count of elements in row
int elements[PUZZLE_SIZE]; // one element of each cell
int elCells[PUZZLE_SIZE]; // one cell of each element
memset(cellsCnt, 0, sizeof(cellsCnt));
memset(elsCnt, 0, sizeof(elsCnt));
memset(elements, 0, sizeof(elements));
memset(elCells, 0, sizeof(elCells));
// check if there is only one element left in cell(col, row)
for (int col = 0; col < PUZZLE_SIZE; col++)
for (int i = 0; i < PUZZLE_SIZE; i++) {
if (pos[col][row][i]) {
elsCnt[i]++;
elCells[i] = col;
cellsCnt[col]++;
elements[col] = i + 1;
}
}
bool changed = false;
// check for cells with single element
for (int col = 0; col < PUZZLE_SIZE; col++) {
if ((cellsCnt[col] == 1) && (elsCnt[elements[col] - 1] != 1)) {
// there is only one element in cell but it used somewhere else
int e = elements[col] - 1;
for (int i = 0; i < PUZZLE_SIZE; i++)
if (i != col)
pos[i][row][e] = 0;
changed = true;
}
}
// check for single element without exclusive cell
for (int el = 0; el < PUZZLE_SIZE; el++)
if ((elsCnt[el] == 1) && (cellsCnt[elCells[el]] != 1)) {
int col = elCells[el];
for (int i = 0; i < PUZZLE_SIZE; i++)
if (i != el)
pos[col][row][i] = 0;
changed = true;
}
if (changed)
checkSingles(row);
}
void Possibilities::exclude(int col, int row, int element)
{
if (! pos[col][row][element - 1])
return;
pos[col][row][element - 1] = 0;
checkSingles(row);
}
void Possibilities::set(int col, int row, int element)
{
for (int i = 0; i < PUZZLE_SIZE; i++)
if ((i != element - 1))
pos[col][row][i] = 0;
else
pos[col][row][i] = element;
for (int j = 0; j < PUZZLE_SIZE; j++)
if (j != col)
pos[j][row][element - 1] = 0;
checkSingles(row);
}
bool Possibilities::isPossible(int col, int row, int element)
{
return pos[col][row][element - 1] == element;
}
bool Possibilities::isDefined(int col, int row)
{
int solvedCnt = 0, unsolvedCnt = 0;
for (int i = 0; i < PUZZLE_SIZE; i++)
if (! pos[col][row][i])
unsolvedCnt++;
else
solvedCnt++;
return ((unsolvedCnt == PUZZLE_SIZE-1) && (solvedCnt == 1));
}
int Possibilities::getDefined(int col, int row)
{
for (int i = 0; i < PUZZLE_SIZE; i++)
if (pos[col][row][i])
return i + 1;
return 0;
}
bool Possibilities::isSolved()
{
for (int i = 0; i < PUZZLE_SIZE; i++)
for (int j = 0; j < PUZZLE_SIZE; j++)
if (! isDefined(i, j))
return false;
return true;
}
bool Possibilities::isValid(SolvedPuzzle &puzzle)
{
for (int row = 0; row < PUZZLE_SIZE; row++)
for (int col = 0; col < PUZZLE_SIZE; col++)
if (! isPossible(col, row, puzzle[row][col]))
return false;
return true;
}
int Possibilities::getPosition(int row, int element)
{
int cnt = 0;
int lastPos = -1;
for (int i = 0; i < PUZZLE_SIZE; i++)
if (pos[i][row][element - 1] == element) {
cnt++;
lastPos = i;
}
return cnt == 1 ? lastPos : -1;
}
void Possibilities::print()
{
for (int row = 0; row < PUZZLE_SIZE; row++) {
std::cout << (char)('A' + row) << " ";
for (int col = 0; col < PUZZLE_SIZE; col++) {
for (int i = 0; i < PUZZLE_SIZE; i++)
if (pos[col][row][i])
std::cout << pos[col][row][i];
else
std::cout << " ";
std::cout << " ";
}
std::cout << std::endl;
}
}
void Possibilities::makePossible(int col, int row, int element)
{
pos[col][row][element-1] = element;
}
void Possibilities::save(std::ostream &stream)
{
for (int row = 0; row < PUZZLE_SIZE; row++)
for (int col = 0; col < PUZZLE_SIZE; col++)
for (int element = 0; element < PUZZLE_SIZE; element++)
writeInt(stream, pos[col][row][element]);
}
static void shuffle(short arr[PUZZLE_SIZE])
{
int a, b, c;
for (int i = 0; i < 30; i++) {
a = (int)(((double)PUZZLE_SIZE)*rand()/(RAND_MAX+1.0));
if ((a < 0) || (a >= PUZZLE_SIZE)) {
std::cerr << "Index error" << std::endl;
exit(1);
}
b = (int)(((double)PUZZLE_SIZE)*rand()/(RAND_MAX+1.0));
if ((b < 0) || (b >= PUZZLE_SIZE)) {
std::cerr << "Index error" << std::endl;
exit(1);
}
c = arr[a];
arr[a] = arr[b];
arr[b] = c;
}
}
static bool canSolve(SolvedPuzzle &puzzle, Rules &rules)
{
Possibilities pos;
bool changed = false;
do {
changed = false;
for (Rules::iterator i = rules.begin(); i != rules.end(); i++) {
Rule *rule = *i;
if (rule->apply(pos)) {
changed = true;
if (! pos.isValid(puzzle)) {
std::cout << "after error:" << std::endl;
pos.print();
throw Exception(L"Invalid possibilities after rule " +
rule->getAsText());
}
}
}
} while (changed);
bool res = pos.isSolved();
return res;
}
static void removeRules(SolvedPuzzle &puzzle, Rules &rules)
{
bool possible;
do {
possible = false;
for (Rules::iterator i = rules.begin(); i != rules.end(); i++) {
Rule *rule = *i;
Rules excludedRules = rules;
excludedRules.remove(rule);
if (canSolve(puzzle, excludedRules)) {
possible = true;
rules.remove(rule);
delete rule;
break;
}
}
} while (possible);
}
static void genRules(SolvedPuzzle &puzzle, Rules &rules)
{
bool rulesDone = false;
do {
Rule *rule = genRule(puzzle);
if (rule) {
std::wstring s = rule->getAsText();
for (std::list<Rule*>::iterator i = rules.begin();
i != rules.end(); i++)
if ((*i)->getAsText() == s) {
delete rule;
rule = NULL;
break;
}
if (rule) {
//printf("adding rule %s\n", rule->getAsText().c_str());
rules.push_back(rule);
rulesDone = canSolve(puzzle, rules);
}
}
} while (! rulesDone);
}
/*static void printPuzzle(SolvedPuzzle &puzzle)
{
for (int i = 0; i < PUZZLE_SIZE; i++) {
char prefix = 'A' + i;
for (int j = 0; j < PUZZLE_SIZE; j++) {
if (j)
std::cout << " ";
std::cout << prefix << puzzle[i][j];
}
std::cout << std::endl;
}
}
static void printRules(Rules &rules)
{
for (Rules::iterator i = rules.begin(); i != rules.end(); i++)
std::cout << (*i)->getAsText() << std::endl;;
}*/
void genPuzzle(SolvedPuzzle &puzzle, Rules &rules)
{
srand(time(NULL));
for (int i = 0; i < PUZZLE_SIZE; i++) {
for (int j = 0; j < PUZZLE_SIZE; j++)
puzzle[i][j] = j + 1;
shuffle(puzzle[i]);
}
genRules(puzzle, rules);
removeRules(puzzle, rules);
//printPuzzle(puzzle);
//printRules(rules);
}
void openInitial(Possibilities &possib, Rules &rules)
{
for (Rules::iterator i = rules.begin(); i != rules.end(); i++) {
Rule *r = *i;
if (r->applyOnStart())
r->apply(possib);
}
}
void getHintsQty(Rules &rules, int &vert, int &horiz)
{
vert = 0;
horiz = 0;
for (Rules::iterator i = rules.begin(); i != rules.end(); i++) {
Rule::ShowOptions so = (*i)->getShowOpts();
switch (so) {
case Rule::SHOW_VERT: vert++; break;
case Rule::SHOW_HORIZ: horiz++; break;
default: ;
}
}
}
void savePuzzle(SolvedPuzzle &puzzle, std::ostream &stream)
{
for (int row = 0; row < PUZZLE_SIZE; row++)
for (int col = 0; col < PUZZLE_SIZE; col++)
writeInt(stream, puzzle[row][col]);
}
void loadPuzzle(SolvedPuzzle &puzzle, std::istream &stream)
{
for (int row = 0; row < PUZZLE_SIZE; row++)
for (int col = 0; col < PUZZLE_SIZE; col++)
puzzle[row][col] = readInt(stream);
}
Rule* getRule(Rules &rules, int no)
{
int j = 0;
for (Rules::iterator i = rules.begin(); i != rules.end(); i++) {
if (j == no)
return *i;
j++;
}
throw Exception(L"Rule is not found");
}
/*int main(int argc, char *argv[])
{
srand(time(NULL));
Rules rules;
Puzzle puzzle;
genPuzzle(puzzle, rules);
printPuzzle(puzzle);
printRules(rules);
return 0;
}*/