This repository has been archived by the owner on Jul 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgameUtils.c
487 lines (399 loc) · 13.2 KB
/
gameUtils.c
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#include "gameUtils.h"
/* preconditions:
* checks whether board has any erroneous cells
* returns TRUE (1) if an erroneous cell was found, and FALSE (0) otherwise
* (used by validate command) */
int hasErrCells(gameParams *game) {
int i, j, N;
cell *c;
N = game->N;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
c = game->userBoard[i][j];
if (c->isValid == FALSE) {
return TRUE; /* invalid cell found! */
}
}
}
/* All board cells are valid - therefore hasErrCells returns FALSE */
return FALSE;
}
int boardIsEmpty(gameParams *game) {
int i, j, N;
cell *c;
N = game->N;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
c = game->userBoard[i][j];
if (c->value != EMPTY) {
return FALSE; /* found an assigned cell - board is not empty*/
}
}
}
return TRUE;
}
/* Allocates memory for a new board and copies values of
* board_to_be_copied.
* Returns pointer to the new board struct (Notice - it is a cell ****) */
cell ***copyBoard(cell ***board_to_be_copied, int N) {
int i, j;
cell ***copyOfBoard;
copyOfBoard = allocateCellMatrix(N);
/* Copy cell by cell values: */
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
copyOfBoard[i][j]->value = board_to_be_copied[i][j]->value;
copyOfBoard[i][j]->isFixed = board_to_be_copied[i][j]->isFixed;
copyOfBoard[i][j]->isValid = board_to_be_copied[i][j]->isValid;
}
}
return copyOfBoard;
}
/* returns the line separator for print_board
* consists 4N+m+1 dashes ('-')
* exits with exit(0) if failed to malloc */
char *getLineSeparator(gameParams *game) {
char *separator;
int N, m, i;
m = game->m;
N = game->N;
separator = malloc(sizeof(char) * (4 * N + m + 1) + 1);
if (separator == NULL) {
freeSudokuGame(game);
printMallocFailed();
exit(EXIT_FAILURE);
}
for (i = 0; i < 4 * N + m + 1; i++) {
separator[i] = '-';
}
separator[i] = '\0';
return separator;
}
/* Allocates memory to new nodes
* frees all previous nodes that were next to current node
* sets the curr and prev pointers
* -- no data is added -- */
void getNewCurrentMove(gameParams *game) {
userMoveNode *newPrev, *newCurr;
newCurr = (userMoveNode *) malloc(sizeof(userMoveNode));
/* First free all userMove nodes that are next to currentMove (NULL check needed in case moveList is empty) */
if (game->movesList->currentMove != NULL) {
freeAllUserMoveNodes(game->movesList->currentMove->next);
} else {
freeAllUserMoveNodes(game->movesList->head);
game->movesList->head = newCurr;
}
newPrev = game->movesList->currentMove;
if (newCurr == NULL) {
printMallocFailed();
exit(0);
}
if (newPrev != NULL) { /* Set previous's next to the new current move, unless prev was NULL */
newPrev->next = newCurr;
}
game->movesList->currentMove = newCurr;
newCurr->prev = newPrev;
newCurr->next = NULL;
/* CHANGE NODE */
newCurr->change = (cellChangeRecNode *) malloc(sizeof(cellChangeRecNode));
if (newCurr->change == NULL) {
printMallocFailed();
exit(0);
}
newCurr->change->currVal = (cell *) malloc(sizeof(cell));
if (newCurr->change->currVal == NULL) {
printMallocFailed();
exit(0);
}
newCurr->change->next = NULL;
/* game->movesList->size++; */
}
/* Checks if Z is a valid value for non-fixed cell <X,Y>
*
* (the real cell at matrix) game->userBoard[x][y]
* */
int checkIfValid(int x, int y, int z, gameParams *game) {
if (z == 0) return 1; /* always legal to set a non-fixed cell to 0 */
if (checkIfSquareValid(x, y, z, game) == FALSE) {
return 0;
}
if (checkIfRowValid(x, y, z, game) == FALSE) {
return 0;
}
if (checkIfColumnValid(x, y, z, game) == FALSE) {
return 0;
}
return 1;
}
/* prints the changes after undo/redo */
int printChanges(cellChangeRecNode *moveToPrint, int isRedo) {
int curr, prev, tmp;
char *command;
while (moveToPrint != NULL) {
curr = moveToPrint->currVal->value;
prev = moveToPrint->prevVal->value;
/* switching values of prev and curr at redo */
if (isRedo) {
tmp = curr;
curr = prev;
prev = tmp;
}
command = isRedo ? "Redo" : "Undo";
printf("%s %d,%d: ", command, moveToPrint->x, moveToPrint->y);
if (!curr) {
if (!prev) {
printf("from _ to _\n");
} else {
printf("from _ to %d\n", prev);
}
} else {
if (!prev) {
printf("from %d to _\n", curr);
} else {
printf("from %d to %d\n", curr, prev);
}
}
moveToPrint = moveToPrint->next;
}
return 1;
}
/* Checks if value z does not appear his square in the matrix */
int checkIfSquareValid(int x, int y, int z, gameParams *game) {
int i, j, m, n;
m = game->m;
n = game->n;
for (i = x - x % m; i < x - x % m + m; i++) {
for (j = y - y % n; j < y - y % n + n; j++) {
if (game->userBoard[i][j]->value == z) {
if (!((i == x) && (j == y))) { /* exclude cell (x,y) from the square check */
return 0;
}
}
}
}
return 1;
}
/* Checks if value z does not appear in row x */
int checkIfRowValid(int x, int y, int z, gameParams *game) {
int j, N;
N = game->n * game->m;
for (j = 0; j < N; j++) {
if (j != y) { /* exclude cell (x,y) from the square check */
if (game->userBoard[x][j]->value == z) {
return 0;
}
}
}
return 1;
}
/* Checks if value z does not appear in column y */
int checkIfColumnValid(int x, int y, int z, gameParams *game) {
int i, N;
N = game->n * game->m;
for (i = 0; i < N; i++) {
if (i != x) { /* exclude cell (x,y) from the square check */
if (game->userBoard[i][y]->value == z) {
return 0;
}
}
}
return 1;
}
/* Returns the only legal value
* for the empty Cell [x][y]
* returns FALSE - iff has 0, or more than 1 values */
int doesCellHaveASingleLegalValue(gameParams *game, int x, int y) {
int k, counter, N, value;
value = 0;
N = game->n * game->m;
counter = 0;
for (k = 1; k < N + 1; k++) {
if (checkIfValid(y, x, k, game)) {
counter++;
value = k;
if (counter > 1) { return FALSE; }
}
}
if (counter == 1) {
return value;
}
return FALSE;
}
/* sets a new value z to cell [x][y] */
void setValue(gameParams *game, int x, int y, int z) {
/* according to z value - increment or decrement game counter
* if z was already set to (x,y) cell - don't change counter */
if ((z == 0) && (game->userBoard[y][x]->value != 0)) { /* when a non-zero cell is set back to zero (emptied) */
game->counter--;
} else if ((z != 0) && (game->userBoard[y][x]->value == 0)) { /* when a zero cell is set to z (!=0) */
game->counter++;
}
/* sets the value */
game->userBoard[y][x]->value = z;
game->userBoard[y][x]->isValid = FALSE; /* Doesn't matter what we assign - in the end we run a function that
* goes over all the board and checks each cell's validity*/
game->userBoard[y][x]->isFixed = FALSE;
}
/* Called by autoFill
* returns the list of changes */
cellChangeRecNode *getAutoFillChangeList(gameParams *game, int *numOfChanges) {
int i, j, N, legalValue, changes;
cellChangeRecNode *changeListHead, *currentChange;
currentChange = NULL;
changeListHead = NULL;
N = game->N;
changes = 0;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (game->userBoard[i][j]->value == 0) {
legalValue = doesCellHaveASingleLegalValue(game, j, i);
if (legalValue != FALSE) {
if (changes == 0) {
/* keep the first node */
changeListHead = (cellChangeRecNode *) malloc(sizeof(cellChangeRecNode));
if (changeListHead == NULL) {
freeSudokuGame(game);
printMallocFailed();
exit(0);
}
currentChange = changeListHead;
currentChange->next = NULL;
} else {
currentChange->next = (cellChangeRecNode *) malloc(sizeof(cellChangeRecNode));
if (currentChange->next == NULL) {
freeSudokuGame(game);
printMallocFailed();
exit(0);
}
currentChange = currentChange->next;
currentChange->next = NULL;
}
currentChange->prevVal = createCell(0);
copyCell(game->userBoard[i][j], currentChange->prevVal);
free(game->userBoard[i][j]);
game->userBoard[i][j] = createCell(-1);
currentChange->currVal = createCell(legalValue);
currentChange->x = j + 1;
currentChange->y = i + 1;
currentChange->next = NULL;
changes++;
printf("Cell <%d,%d> set to %d\n", j + 1, i + 1, legalValue);
}
}
}
}
*numOfChanges = changes;
return changeListHead;
}
void setValuesByChangeListHead(gameParams *game, cellChangeRecNode *changeListNode) {
int x, y;
while (changeListNode != NULL) {
x = changeListNode->x;
y = changeListNode->y;
setValue(game, x - 1, y - 1, changeListNode->currVal->value);
changeListNode = changeListNode->next;
}
}
/* Called by autoFill */
void setNewChangeListToGame(gameParams *game, cellChangeRecNode *changeListHead) {
userMoveNode *newMove;
if (game->movesList->currentMove != NULL && game->movesList->currentMove->next != NULL) {
freeAllUserMoveNodes(game->movesList->currentMove->next);
}
newMove = (userMoveNode *) malloc(sizeof(userMoveNode));
if (newMove == NULL) {
freeSudokuGame(game);
printMallocFailed();
exit(0);
}
if (game->movesList->currentMove == NULL) {
game->movesList->currentMove = newMove;
game->movesList->head = newMove;
newMove->prev = NULL;
} else {
newMove->prev = game->movesList->currentMove;
game->movesList->currentMove->next = newMove;
game->movesList->currentMove = newMove;
}
newMove->change = changeListHead;
newMove->next = NULL;
}
/* Cleans game->userBoard and game->solution */
void cleanUserBoardAndSolution(gameParams *game) {
int i, j;
for (i = 0; i < game->N; i++) {
for (j = 0; j < game->N; j++) {
game->userBoard[i][j]->value = EMPTY;
game->userBoard[i][j]->isFixed = FALSE;
game->userBoard[i][j]->isValid = TRUE;
game->solution[i][j]->value = EMPTY;
game->solution[i][j]->isFixed = FALSE;
game->solution[i][j]->isValid = TRUE;
}
}
}
/* Cleans game->userBoard */
void cleanUserBoard(gameParams *game) {
int i, j;
for (i = 0; i < game->N; i++) {
for (j = 0; j < game->N; j++) {
game->userBoard[i][j]->value = EMPTY;
game->userBoard[i][j]->isFixed = FALSE;
game->userBoard[i][j]->isValid = TRUE;
}
}
}
void setToEmpty(int **matrix, int N) {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
matrix[i][j] = EMPTY;
}
}
}
void markFullCellsAsFixed(cell ***board, int N) {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (board[i][j]->value != EMPTY) {
board[i][j]->isFixed = TRUE;
}
}
}
}
/* Iterates over each cell
* and check if it is valid
* or erroneous */
void updateErrors(gameParams *game) {
int i, j, N, isValid;
N = game->N;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
isValid = checkIfValid(j, i, game->userBoard[j][i]->value, game);
game->userBoard[j][i]->isValid = isValid;
}
}
}
void copyCell(cell *src, cell *dst) {
dst->isValid = src->isValid;
dst->value = src->value;
dst->isFixed = src->isFixed;
}
#if 0
/* Called by undo
* implemented recursively for printing in thr right order:
* make changes -> print board -> print changes
* prints at the opposite order, MIGHT NOT BE USED!!
* */
int makeRecChanges(gameParams *game, cellChangeRecNode *moveToUndo) {
if (moveToUndo == NULL) {
printBoard(game);
return 1;
}
game->userBoard[moveToUndo->x - 1][moveToUndo->y - 1] = moveToUndo->prevVal;
makeRecChanges(game, moveToUndo->next);
printf("Undo %d,%d: from %d to %d\n", moveToUndo->x, moveToUndo->y, moveToUndo->currVal->value,
moveToUndo->prevVal->value);
return 1;
}
#endif