-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
314 lines (280 loc) · 7.91 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "game.h"
#include "parser.h"
#include "solver.h"
#define MALLOC_ERROR "Error: malloc has failed"
/*
* Gets a pointer to a cell, and initializes it with given value.
* fixed == 1 means cell is fixed; fixed == 0 means cell is not fixed
* Initializes as non fixed cell.
*/
void createCell(Cell* cell,int val){
cell->value = val;
cell->isFixed = 0;
}
/*
* Receives dimensions of the wanted board and the blocks in the board.
* Returns a pointer to a Board struct, with the current game board and solution board set to default (all zeros).
*/
Board* create_blank_board(int boardSize,int blockRows, int blockCols){
int i, j;
Board* board;
Cell** solution;
Cell** current;
if((board = (Board*) malloc(sizeof(Board))) == NULL){
fprintf(stderr,"%s",MALLOC_ERROR);
return NULL;
}
board->board_size = boardSize;
board->block_rows = blockRows;
board->block_cols = blockCols;
board->num_empty_cells_current = boardSize*boardSize;
board->num_empty_cells_solution = boardSize*boardSize;
if((solution = (Cell**) malloc(boardSize*sizeof(Cell*))) == NULL){
fprintf(stderr,"%s",MALLOC_ERROR);
free(board);
return NULL;
}
if((current = (Cell**) malloc(boardSize*sizeof(Cell*))) == NULL){
fprintf(stderr,"%s",MALLOC_ERROR);
free(board);
return NULL;
}
for(i = 0; i < boardSize; i++){
solution[i] = (Cell*) malloc(boardSize*sizeof(Cell));
current[i] = (Cell*) malloc(boardSize*sizeof(Cell));
for(j = 0; j < boardSize; j++){
createCell(&solution[i][j],0);
createCell(¤t[i][j],0);
}
}
board->solution = solution;
board->current_board = current;
return board;
}
/*
* Destroys properly a given game board, freeing all allocated resources.
*/
void destroy_game_board(Cell** board, int size){
int i;
for(i = 0; i < size; i++){
free(board[i]);
}
free(board);
}
/*
* Destroys properly a given Board, freeing all allocated resources.
*/
void destroyBoard(Board* b){
destroy_game_board(b->current_board, b->board_size);
destroy_game_board(b->solution, b->board_size);
free(b);
}
/*
* Prints a single cell, acording to the sudoku board format.
*/
void printCell(Cell* c){
if(c->isFixed == 0 && c->value != 0)
printf(" %d ",c->value);
else
if(c->value != 0)
printf(".%d ",c->value);
else
printf(" ");
}
/*
* Prints the given board.
* if type == 1: prints the board's known solution.
* otherwise, prints board's current state board.
*/
void printBoard(Board* b, int type){
int i, j;
Cell** board;
char* sep_row;
int total_row_length = (b->board_size * 3) + ((b->board_size / b->block_cols) * 2) + 1;
if(type == 1)
board = b->solution;
else
board = b->current_board;
sep_row = (char*) malloc((total_row_length + 2)*sizeof(char));
for( i = 0; i < total_row_length; i++ ){
sep_row[i] ='-';
}
sep_row[total_row_length] = '\n';
sep_row[total_row_length + 1] = '\0';
for( i = 0; i < b->board_size; i++){
if( i % b->block_rows == 0 )
printf("%s",sep_row);
for( j = 0; j < b->board_size; j++){
if( j % b->block_cols == 0 )
printf("| ");
printCell(&(board[i][j]));
}
printf("|\n");
}
printf("%s",sep_row);
free(sep_row);
}
/*
* Generates a game board with fixed cells according to user input.
*
* Gets a pointer to an already created blank Board.
* generates a randomized full legal board and stores it in solution.
*/
void generate_user_board(Board* board){
int fixedCells;
int row, col;
printf("Please enter the number of cells to fill [0-80]:\n");
fixedCells = get_fixed_cells(board);
backtracking_solution(board, 1);
while( fixedCells > 0 ){
col = rand() % (board->board_size);
row = rand() % (board->board_size);
if(board->current_board[row][col].isFixed == 0){
board->current_board[row][col].value = board->solution[row][col].value;
board->current_board[row][col].isFixed = 1;
fixedCells--;
board->num_empty_cells_current--;
}
}
}
/*
* Creates and returns a duplicate of a given game_board. (the actual matrix of cells, not Board).
*/
Cell** copy_game_board(Cell** game_board, int board_size){
int i, j;
Cell** copy;
if((copy = (Cell**) malloc(board_size*sizeof(Cell*))) == NULL){
fprintf(stderr,"%s",MALLOC_ERROR);
return NULL;
}
for(i = 0; i < board_size; i++){
copy[i] = (Cell*) malloc(board_size*sizeof(Cell));
for(j = 0; j < board_size; j++){
copy[i][j].value = game_board[i][j].value;
copy[i][j].isFixed = game_board[i][j].isFixed;
}
}
return copy;
}
/*
* For use in user comand "validate".
* Checks that the current_board of b is solvable.
* if it is - sets b->solution to be the found solution.
* if not - prints a message accordingly.
*/
void validate(Board* b){
Cell** current_copy = copy_game_board(b->current_board, b->board_size);
int empty_cells_copy = b->num_empty_cells_current;
int solvable = backtracking_solution(b, 0);
if( solvable == 0){
printf("Validation failed: board is unsolvable\n");
destroy_game_board(b->current_board,b->board_size);
b->current_board = current_copy;
b->num_empty_cells_current = empty_cells_copy;
}
else{
printf("Validation passed: board is solvable\n");
destroy_game_board(b->solution, b->board_size);
b->solution = b->current_board;
b->current_board = current_copy;
b->num_empty_cells_current = empty_cells_copy;
}
}
/*
* For use when user enters command set.
* If legal and possible, enters inserted_value in to cell in given column and row.
* If command was legal, prints the new board.
* If also the board is now full - prints that the user has solved the puzzle.
*/
void set(Board* board, int col, int row, int inserted_val, int param_counter){
if(col < 0 || row < 0 || board->num_empty_cells_current == 0 || param_counter < 3){
printf("Error: invalid command\n");
return;
}
if(board->current_board[row][col].isFixed == 1) {
printf("Error: cell is fixed\n");
return;
}
if(inserted_val == 0 || check_valid_value(board, inserted_val, row, col, 0) == 1) {
if(inserted_val == 0 && board->current_board[row][col].value != 0) {
board->num_empty_cells_current++;
} else {
if(inserted_val != 0 && board->current_board[row][col].value == 0){
board->num_empty_cells_current--;
}
}
board->current_board[row][col].value = inserted_val;
printBoard(board, 0);
if(board->num_empty_cells_current == 0) {
printf("Puzzle solved successfully\n");
}
}
else {
printf("Error: value is invalid\n");
return;
}
return;
}
/*
* For use when user enters command restart.
* restarts the game board from scratch, generating a new puzzle.
*/
void restart(Board* board){
int i, j;
board->num_empty_cells_current = board->board_size*board->board_size;
board->num_empty_cells_solution = board->board_size*board->board_size;
for(i = 0; i < board->board_size; i++){
for(j = 0; j < board->board_size; j++){
createCell(&board->solution[i][j],0);
createCell(&board->current_board[i][j],0);
}
}
generate_user_board(board);
printBoard(board,0);
}
void exit_game(Board* board){
destroyBoard(board);
printf("Exiting...\n");
exit(EXIT_SUCCESS);
}
/*
*Recieves given command from user, and implements it appropriately.
*/
void execute_command(Command* command, Board* board) {
int row, col, inserted_val;
switch(command->id) {
case SET:
col = command->params[0] - 1;
row = command->params[1] - 1;
inserted_val = command->params[2];
set(board, col, row, inserted_val, command->param_counter);
break;
case VALIDATE:
validate(board);
break;
case HINT:
row = command->params[1] - 1;
col = command->params[0] - 1;
if(col < 0 || row < 0 || board->num_empty_cells_current == 0 || command->param_counter < 2){
printf("Error: invalid command\n");
break;
}
printf("Hint: set cell to %d\n", board->solution[row][col].value);
break;
case RESTART:
restart(board);
break;
case EXIT:
free(command);
exit_game(board);
break;
default :
printf("Error: invalid command\n");
break;
}
free(command);
}