-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
254 lines (222 loc) · 7.36 KB
/
game.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
#include "game.h"
/**
* State struct represents a sudoku game in its current state. It contains the board itself, a
* possible solution for it, and the number of cells left to fill in the board in its current
* configuration.
* Note: the implementation of this struct is meant to be hidden from the user.
*/
struct State {
Board puzzle;
Board solution;
int numNonSet;
};
void exportBoard(State* state, Board* boardOut) {
*boardOut = state->puzzle;
}
/**
* rowContains checks whether a particular row in the provided board contains a particular
* value or not.
*
* @param board [in] pointer to the Board struct to be inspected
* @param row [in] the number of row to check
* @param value [in] value being checked
* @return true iff row contains value
* @return false iff row does not contain value
*/
bool rowContains(Board* board, int row, int value) {
int col = 0;
for (col = 0; col < N_SQUARE; col++)
if (getCellValue(board, row, col) == value)
return true;
return false;
}
/**
* colContains checks whether a particular column in the provided board contains a particular
* value or not.
*
* @param board [in] pointer to the Board struct to be inspected
* @param col [in] the number of column to check
* @param value [in] value being checked
* @return true iff column contains value
* @return false iff column does not contain value
*/
bool colContains(Board* board, int col, int value) {
int row = 0;
for (row = 0; row < N_SQUARE; row++)
if (getCellValue(board, row, col) == value)
return true;
return false;
}
/**
* whichBlock uses a linear mapping to find which block in the board a cell with given
* indices belongs to. The board blocks are of size N*N and there are N of them in total,
* numbered [0 - N-1], from left to right and top to bottom.
*
* @param row [in] the number of row the cell is in
* @param col [in] the number of column the cell is in
* @return int the number of block the cell is in
*/
int whichBlock(int row, int col) {
return ((row / N) * N) + (col / N);
}
/**
* blockContains checks whether a particular block in the provided board contains a particular
* value or not.
*
* @param board [in] pointer to the Board struct to be inspected
* @param block [in] the number of block to check
* @param value [in] value being checked
* @return true iff block contains value
* @return false iff block does not contain value
*/
bool blockContains(Board* board, int block, int value) {
int rowOffset = (block / N) * N;
int colOffset = (block % N) * N;
int row = 0, col = 0;
for (row = 0; row < N; row++)
for (col = 0; col < N; col++)
if (getCellValue(board, rowOffset + row, colOffset + col) == value)
return true;
return false;
}
int getCellValue(Board* board, int row, int col) {
return board->cells[row][col].value;
}
bool isCellValueValid(Board* board, int row, int col, int value) {
return (getCellValue(board, row, col) == value) ||
(!rowContains(board, row, value) &&
!colContains(board, col, value) &&
!blockContains(board, whichBlock(row, col), value));
}
void setCellValue(Board* board, int row, int col, int value) {
board->cells[row][col].value = value;
}
/**
* fixCell is used during the generation of a new sudoku board to make a cell fixed.
*
* @param board [in] pointer to the Board struct whose cell is set to be fixed
* @param row [in] the row number of that cell
* @param col [in] the column number of that cell
*/
void fixCell(Board* board, int row, int col) {
board->cells[row][col].isFixed = true;
}
bool isCellFixed(Board* board, int row, int col) {
return board->cells[row][col].isFixed;
}
bool isCellEmpty(Board* board, int row, int col) {
return getCellValue(board, row, col) == EMPTY_CELL_VALUE;
}
void emptyCell(Board* board, int row, int col) {
setCellValue(board, row, col, EMPTY_CELL_VALUE);
}
/**
* setPuzzleCell sets a value provided by the user to a cell in the sudoku board, in the
* current state of the game. Number of cells to be set is updated if need be.
*
* @param state [in] pointer to the Board struct whose cell is set to be set
* @param row [in] the row number of that cell
* @param col [in] the column number of that cell
* @param value [in] the value to be set to that cell
*/
void setPuzzleCell(State* state, int row, int col, int value) {
if (isCellEmpty(&(state->puzzle), row, col)) {
state->numNonSet--;
}
setCellValue(&(state->puzzle), row, col, value);
}
/**
* emptyPuzzleCell empties a cell in the sudoku board, in the current state of the game.
* Number of cells to be set is updated if need be.
*
* @param state [in] pointer to the Board struct whose cell is set to be emptied
* @param row [in] the row number of that cell
* @param col [in] the column number of that cell
*/
void emptyPuzzleCell(State* state, int row, int col) {
if (!isCellEmpty(&(state->puzzle), row, col)) {
state->numNonSet++;
}
emptyCell(&(state->puzzle), row, col);
}
bool set(State* state, int row, int col, int value, SetErrorType* errorTypeOut) {
if (isCellFixed(&(state->puzzle), row, col)) {
*errorTypeOut = VALUE_FIXED;
return false;
}
if (value != EMPTY_CELL_VALUE) {
if (! isCellValueValid(&(state->puzzle), row, col, value)) {
*errorTypeOut = VALUE_INVALID;
return false;
} else {
setPuzzleCell(state, row, col, value);
}
} else {
emptyPuzzleCell(state, row, col);
}
return true;
}
bool isGameWon(State* state) {
return state->numNonSet == 0;
}
int hint(State* state, int row, int col) {
return getCellValue(&(state->solution), row, col);
}
/**
* randomlyFixCells is to be used in ordered to randomly mark a certain
* amount of cells in a given board as fixed.
* Canonical use of this function: generation of a puzzle board.
*
* @param board [in, out] the board whose cells are to be fixed
* @param numCellsToFix [in] the number of cells to fix (should be not more
* than the number of cells in board)
*/
void randomlyFixCells(Board* board, int numCellsToFix) {
int fixCount = 0;
while (fixCount < numCellsToFix) {
int col = rand() % N_SQUARE;
int row = rand() % N_SQUARE;
if (! isCellFixed(board, row, col)) {
fixCell(board, row, col);
fixCount++;
}
}
}
/**
* clearNonFixedCells is used during the puzzle generation process. After the fixed cells
* in the new board have been selected, clearNonFixed cells empties each cell which haven't
* been fixed to create the puzzle.
*
* @param board [in, out] a pointer to the Board struct of which the non fixed cells
* are to be cleared
*/
void clearNonFixedCells(Board* board) {
int row = 0, col = 0;
for (row = 0; row < N_SQUARE; row++)
for (col = 0; col < N_SQUARE; col++)
if (! isCellFixed(board, row, col))
emptyCell(board, row, col);
}
bool initialise(int numCellsToFill, State** stateOut, Board* board) {
*stateOut = calloc(1, sizeof(State));
if (*stateOut == NULL) {
/* NOTE: for now we are allowed this behaviour: */
printf("Error: initialise has failed\n");
exit(EXIT_FAILURE);
return false;
}
(*stateOut)->puzzle = *board;
randomlyFixCells(&((*stateOut)->puzzle), numCellsToFill);
(*stateOut)->solution = (*stateOut)->puzzle;
clearNonFixedCells(&((*stateOut)->puzzle));
(*stateOut)->numNonSet = N_SQUARE * N_SQUARE - numCellsToFill;
return true;
}
void setPuzzleSolution(State* state, Board* solution) {
state->solution = *solution;
}
void destruct(State* state) {
if (state != NULL) {
free(state);
}
}