forked from sunjay/sudoku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.c
401 lines (354 loc) · 9.03 KB
/
sudoku.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
/**
* Sudoku Board Primitives
*
* Author: Sunjay Varma (www.sunjay.ca)
*/
#include <stdlib.h>
#include <stdbool.h>
#include "sudoku.h"
/**
* Generates a completely empty, all zeros board
*
* Should always be freed after use.
*/
SudokuBoard* newSudokuBoard() {
SudokuBoard* sudoku = malloc(sizeof(SudokuBoard));
if (!sudoku) {
return NULL;
}
for (int i = 0; i < BOARD_SIZE; i++) {
sudoku->tiles[i] = calloc(BOARD_SIZE, sizeof(Tile));
if (!sudoku->tiles[i]) {
return NULL;
}
for (int j = 0; j < BOARD_SIZE; j++) {
// There are BOARD_SIZE possible values for every tile on
// an empty board
for (int p = 0; p < BOARD_SIZE; p++) {
sudoku->tiles[i][j].possibleValues[p] = true;
}
sudoku->tiles[i][j].possibleCount = BOARD_SIZE;
}
}
return sudoku;
}
/**
* Copies a board and all of its values
*
* Should always be freed after use.
*/
SudokuBoard* copySudokuBoard(SudokuBoard* board) {
SudokuBoard* copy = malloc(sizeof(SudokuBoard));
if (!copy) {
return NULL;
}
for (int i = 0; i < BOARD_SIZE; i++) {
copy->tiles[i] = malloc(BOARD_SIZE*sizeof(Tile));
for (int j = 0; j < BOARD_SIZE; j++) {
copy->tiles[i][j] = board->tiles[i][j];
}
}
return copy;
}
/**
* Frees a board from memory
*/
void freeSudokuBoard(SudokuBoard* board) {
for (int i = 0; i < BOARD_SIZE; i++) {
free(board->tiles[i]);
}
free(board);
}
/**
* Generates a valid, completely filled, pre-solved, random board
*
* Currently only supports b
*
* Should always be freed after use.
*/
/*SudokuBoard* solvedSudokuBoard() {
// A presolved board to shuffle around
SudokuBoard* board = newSudokuBoard();
if (board == NULL) {
return NULL;
}
short row0[BOARD_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
short row1[BOARD_SIZE] = {4, 5, 6, 7, 8, 9, 1, 2, 3};
short row2[BOARD_SIZE] = {7, 8, 9, 1, 2, 3, 4, 5, 6};
short row3[BOARD_SIZE] = {2, 3, 4, 5, 6, 7, 8, 9, 1};
short row4[BOARD_SIZE] = {5, 6, 7, 8, 9, 1, 2, 3, 4};
short row5[BOARD_SIZE] = {8, 9, 1, 2, 3, 4, 5, 6, 7};
short row6[BOARD_SIZE] = {3, 4, 5, 6, 7, 8, 9, 1, 2};
short row7[BOARD_SIZE] = {6, 7, 8, 9, 1, 2, 3, 4, 5};
short row8[BOARD_SIZE] = {9, 1, 2, 3, 4, 5, 6, 7, 8};
//TODO: Shuffle rows and columns randomly
setBoardRow(board, 0, row0);
setBoardRow(board, 1, row1);
setBoardRow(board, 2, row2);
setBoardRow(board, 3, row3);
setBoardRow(board, 4, row4);
setBoardRow(board, 5, row5);
setBoardRow(board, 6, row6);
setBoardRow(board, 7, row7);
setBoardRow(board, 8, row8);
return board;
}*/
/**
* Places a value on the sudoku board. Updates all related possible value
* caches and their counts.
*/
void placeSudokuValue(SudokuBoard* board, int row_i, int col_i, short value) {
// Place the value on its tile
board->tiles[row_i][col_i].value = value;
if (value == 0) {
return;
}
// The start index of this tile's box
int boxRowStart = (row_i / BOX_SIZE) * BOX_SIZE;
int boxColStart = (col_i / BOX_SIZE) * BOX_SIZE;
// The index at which this value will be found in all possibleValue arrays
int valueIndex = value - 1;
for (int i = 0; i < BOARD_SIZE; i++) {
// Update items in the same row
Tile* rowItem = &(board->tiles[row_i][i]);
if (rowItem->possibleValues[valueIndex]) {
// Tile was available, now it is not
rowItem->possibleValues[valueIndex] = false;
rowItem->possibleCount--;
}
// Update items in the same column
Tile* colItem = &(board->tiles[i][col_i]);
if (colItem->possibleValues[valueIndex]) {
// Tile was available, now it is not
colItem->possibleValues[valueIndex] = false;
colItem->possibleCount--;
}
// Update items in the same box
Tile* boxItem = &(board->tiles[boxRowStart + i / BOX_SIZE][boxColStart + i % BOX_SIZE]);
if (boxItem->possibleValues[valueIndex]) {
// Tile was available, now it is not
boxItem->possibleValues[valueIndex] = false;
boxItem->possibleCount--;
}
}
}
/**
* Sets all items in a single board row to items
*
* items must be exactly BOARD_SIZE size
*/
void setBoardRow(SudokuBoard* board, int row_i, Tile* items) {
Tile* row = board->tiles[row_i];
for (int i = 0; i < BOARD_SIZE; i++) {
row[i] = items[i];
}
}
/**
* Sets all values of all items in a single board row to items
*
* items must be exactly BOARD_SIZE size
*/
void setBoardRowValues(SudokuBoard* board, int row_i, short* items) {
for (int i = 0; i < BOARD_SIZE; i++) {
placeSudokuValue(board, row_i, i, items[i]);
}
}
/**
* Returns an array of size BOARD_SIZE that represents a row of the board
*
* Should be freed after use
*/
Tile* getBoardRow(SudokuBoard* board, int row_i) {
Tile* row = malloc(sizeof(Tile)*BOARD_SIZE);
if (!row) {
return NULL;
}
for (int i = 0; i < BOARD_SIZE; i++) {
row[i] = board->tiles[row_i][i];
}
return row;
}
/**
* Returns an array of size BOARD_SIZE that represents a column of the board
*
* Should be freed after use
*/
Tile* getBoardColumn(SudokuBoard* board, int col_i) {
Tile* col = malloc(sizeof(Tile)*BOARD_SIZE);
if (!col) {
return NULL;
}
for (int i = 0; i < BOARD_SIZE; i++) {
col[i] = board->tiles[i][col_i];
}
return col;
}
/**
* Gets one of the boxes of the board of BOX_SIZE
* Returns an array of array pointers that represents the box
*
* Box indexes work like this for BOARD_SIZE = 9:
* 0 1 2
* 3 4 5
* 6 7 8
*
* Should be freed after use
*/
Tile** getBoardBox(SudokuBoard* board, int box_i) {
Tile** box = malloc(sizeof(Tile*)*BOX_SIZE);
if (!box) {
return NULL;
}
int y_offset = (box_i / ((int)BOX_SIZE)) * BOX_SIZE;
int x_offset = (box_i % ((int)BOX_SIZE)) * BOX_SIZE;
for (int i = 0; i < BOX_SIZE; i++) {
box[i] = malloc(sizeof(Tile)*BOX_SIZE);
if (!box[i]) {
return NULL; // yes this is a memory leak...
}
for (int j = 0; j < BOX_SIZE; j++) {
box[i][j] = board->tiles[y_offset + i][x_offset + j];
}
}
return box;
}
/**
* Frees a box from memory
*/
void freeBox(Tile** box) {
for (int i = 0; i < BOX_SIZE; i++) {
free(box[i]);
}
free(box);
}
/**
* Converts tile column/row position to box index
*/
int coordinatesToBoxIndex(int col_i, int row_i) {
return (row_i / ((int)BOX_SIZE))*BOX_SIZE + col_i / ((int)BOX_SIZE);
}
/**
* Gets the tiles in the box surrounding a given tile between
*
* col and row are the row and column index of the tile
*
* Should be freed after use
*/
Tile** getTileBox(SudokuBoard* board, int col_i, int row_i) {
int box_i = coordinatesToBoxIndex(col_i, row_i);
return getBoardBox(board, box_i);
}
/**
* Checks for duplicates
*/
static bool isValid(Tile* row) {
bool found[BOARD_SIZE] = {false, false, false, false, false, false, false, false, false};
for (int i = 0; i < BOARD_SIZE; i++) {
short value = row[i].value;
if (value == 0) {
continue;
}
if (found[value-1]) {
return false;
}
found[value-1] = true;
}
return true;
}
/**
* Validates a single tile by checking if there are repetitions in the
* tile's row, column or box. This is much faster than checking the entire
* board after every move.
*
* Ignores zero values.
*/
bool isValidTile(SudokuBoard* board, int col_i, int row_i) {
Tile* row = getBoardRow(board, row_i);
Tile* col = getBoardColumn(board, col_i);
Tile** box = getTileBox(board, col_i, row_i);
Tile box_values[BOARD_SIZE];
for (int i = 0; i < BOX_SIZE; i++) {
for (int j = 0; j < BOX_SIZE; j++) {
box_values[i*BOX_SIZE + j] = box[i][j];
}
}
bool valid = isValid(row) && isValid(col) && isValid(box_values);
free(col);
free(row);
freeBox(box);
return valid;
}
/**
* Validates whether the board is correct
*
* A board is incorrect if it has any repetitions
*/
bool isValidBoard(SudokuBoard* board) {
for (int i = 0; i < BOARD_SIZE; i++) {
// validate box
Tile** box = getBoardBox(board, i);
Tile box_values[BOARD_SIZE];
for (int i = 0; i < BOX_SIZE; i++) {
for (int j = 0; j < BOX_SIZE; j++) {
box_values[i*BOX_SIZE + j] = box[i][j];
}
}
freeBox(box);
if (!isValid(box_values)) {
return false;
}
// validate column
Tile* col = getBoardColumn(board, i);
bool colValid = isValid(col);
free(col);
if (!colValid) {
return false;
}
// validate row
Tile* row = getBoardRow(board, i);
bool rowValid = isValid(row);
free(row);
if (!rowValid) {
return false;
}
}
return true;
}
/**
* Returns whether you have a valid, complete board
*/
bool isCompleteBoard(SudokuBoard* board) {
if (board == NULL) {
return false;
}
// make sure everything is full
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
Tile tile = board->tiles[i][j];
if (tile.value == 0) {
return false;
}
}
}
return isValidBoard(board);
}
/**
* Rates the given board on a scale from 0.0 to 1.0
* based on how difficult it is.
*/
double getBoardDifficultyRating(SudokuBoard* board) {
// More empty tiles = more difficult
double empty = 0;
// Loop through all tiles and count how many are already
// filled
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
Tile tile = board->tiles[i][j];
if (tile.value == 0) {
empty += 1;
}
}
}
// Returns the inverse divided by the maximum board size
return empty / (BOARD_SIZE * BOARD_SIZE);
}