-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver9.c
More file actions
470 lines (393 loc) · 14.6 KB
/
solver9.c
File metadata and controls
470 lines (393 loc) · 14.6 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <err.h>
#include <string.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "solver9.h"
// Global variable to deal with sudoku and hexadoku.
int HEXA9 = 0;
size_t GRID_DIMENSION9 = 9;
size_t BOX_DIMENSION9 = 3;
// Save the grid in a new file with extension ".txt".
//
// grid: grid with the digits of sudoku.
// filepath: path of the file where the grid is stored.
void write_grid_in_file9txt(char grid[GRID_DIMENSION9][GRID_DIMENSION9], char filepath[])
{
// Concate filepath and extension.
char filepath_extension[strlen(filepath) + 7];
filepath_extension[0] = '\0';
strcat(filepath_extension, filepath);
strcat(filepath_extension, ".txt");
// Creates a new file.
FILE *result_file = fopen(filepath_extension, "w");
// Check if file has been created.
if (result_file == NULL)
errx(EXIT_FAILURE, "Unable to create file.");
// Writes in file.
for (size_t z = 0; z < BOX_DIMENSION9; z++)
{
for (size_t i = z * BOX_DIMENSION9; i < BOX_DIMENSION9 + z * BOX_DIMENSION9; i++)
{
// First blocks (2 for sudoku and 3 for hexadoku).
for (size_t j = 0; j < BOX_DIMENSION9 - 1; j++)
{
// 3 charaters for sudoku and 4 for hexadoku.
for (size_t k = j * BOX_DIMENSION9; k < BOX_DIMENSION9 + j * BOX_DIMENSION9; k++)
{
fprintf(result_file, "%c", grid[i][k]);
}
// Deal with ' '.
fprintf(result_file, " ");
}
// Last characters.
for (size_t k = (BOX_DIMENSION9 - 1) * BOX_DIMENSION9; k < GRID_DIMENSION9; k++)
{
fprintf(result_file, "%c", grid[i][k]);
}
// Writes a new line between blocks.
fprintf(result_file, "\n");
}
// Writes a new line betwenn blocks.
fprintf(result_file, "\n");
}
// Closes the new file.
fclose(result_file);
}
// Get grid from file and store it in an array.
//
// filename: path of the file where the grid is stored.
// grid: grid where the sudoku in file is put.
void get_grid_from_file9(char filepath[], char grid[GRID_DIMENSION9][GRID_DIMENSION9])
{
// Point on the file where the grid is stored.
FILE *file = fopen(filepath, "r");
// Throw error if the file is not found.
if (file == NULL)
errx(EXIT_FAILURE, "File not found.");
// Store all digits and point in the grid by reading each lines.
for (size_t z = 0; z < BOX_DIMENSION9; z++)
{
// First blocks.
for (size_t i = z * BOX_DIMENSION9; i < BOX_DIMENSION9 + z * BOX_DIMENSION9; i++)
{
for (size_t j = 0; j < BOX_DIMENSION9; j++)
{
// 3 or 4 characters.
for (size_t k = j * BOX_DIMENSION9; k < BOX_DIMENSION9 + j * BOX_DIMENSION9; k++)
{
grid[i][k] = fgetc(file);
}
// Deals with ' ' and '\n'.
fgetc(file);
}
}
// Deals with '\n'.
fgetc(file);
}
// Closes file.
fclose(file);
}
// Save the grid in a new file with extension ".result".
//
// grid: grid with the digits of sudoku.
// filepath: path of the file where the grid is stored.
void write_grid_in_file9(char grid[GRID_DIMENSION9][GRID_DIMENSION9], char filepath[])
{
// Concate filepath and extension.
char filepath_extension[strlen(filepath) + 7];
filepath_extension[0] = '\0';
strcat(filepath_extension, filepath);
strcat(filepath_extension, ".result");
// Creates a new file.
FILE *result_file = fopen(filepath_extension, "w");
// Check if file has been created.
if (result_file == NULL)
errx(EXIT_FAILURE, "Unable to create file.");
// Writes in file.
for (size_t z = 0; z < BOX_DIMENSION9; z++)
{
for (size_t i = z * BOX_DIMENSION9; i < BOX_DIMENSION9 + z * BOX_DIMENSION9; i++)
{
// First blocks (2 for sudoku and 3 for hexadoku).
for (size_t j = 0; j < BOX_DIMENSION9 - 1; j++)
{
// 3 charaters for sudoku and 4 for hexadoku.
for (size_t k = j * BOX_DIMENSION9; k < BOX_DIMENSION9 + j * BOX_DIMENSION9; k++)
{
fprintf(result_file, "%c", grid[i][k]);
}
// Deal with ' '.
fprintf(result_file, " ");
}
// Last characters.
for (size_t k = (BOX_DIMENSION9 - 1) * BOX_DIMENSION9; k < GRID_DIMENSION9; k++)
{
fprintf(result_file, "%c", grid[i][k]);
}
// Writes a new line between blocks.
fprintf(result_file, "\n");
}
// Writes a new line betwenn blocks.
fprintf(result_file, "\n");
}
// Closes the new file.
fclose(result_file);
}
// Check if a number can be put in the grid.
//
// digit: digit to test in grid.
// grid: grid with the digits of sudoku.
// row: row of the digit in grid.
// col: column of the digit in grid.
int number_is_valid9(char digit, char grid[GRID_DIMENSION9][GRID_DIMENSION9], size_t row, size_t col)
{
// Checks column.
for (size_t i = 0; i < GRID_DIMENSION9; i++)
{
if (grid[i][col] == digit)
return 0;
}
// Checks row.
for (size_t i = 0; i < GRID_DIMENSION9; i++)
{
if (grid[row][i] == digit)
return 0;
}
// Inits row and column of current box.
size_t begin_row = row - row % BOX_DIMENSION9;
size_t begin_col = col - col % BOX_DIMENSION9;
// Checks box.
for (size_t i = begin_row; i < begin_row + BOX_DIMENSION9; i++)
{
for (size_t j = begin_col; j < begin_col + BOX_DIMENSION9; j++)
{
if (grid[i][j] == digit)
return 0;
}
}
// Returns number is valid in grid.
return 1;
}
void immediat_solutions9(char grid[GRID_DIMENSION9][GRID_DIMENSION9], char possibilities[GRID_DIMENSION9*GRID_DIMENSION9][GRID_DIMENSION9])
{
struct Node*** solutions = calloc(GRID_DIMENSION9, sizeof(struct Node**));
for(size_t i = 0; i < GRID_DIMENSION9; i++)
{
solutions[i] = calloc(GRID_DIMENSION9, sizeof(struct Node*));
for(size_t j = 0; j < GRID_DIMENSION9; j++)
{
solutions[i][j] = newList();
if(grid[i][j] == '.')
{
for(char n = (HEXA9?0:1); n < (char)GRID_DIMENSION9+(HEXA9?0:1); n++)
insert_list(solutions[i][j], (n > 9 ? n+7: n)+'0');
}
else
insert_list(solutions[i][j], grid[i][j]);
}
}
int gotchanges = 1;
while(gotchanges)
{
gotchanges = 0;
for(size_t x = 0; x < GRID_DIMENSION9; x++)
{
for(size_t y = 0; y < GRID_DIMENSION9; y++)
{
if(grid[x][y] == '.')
{
//Check column
for(size_t i = 0; i < GRID_DIMENSION9; i++)
{
if(grid[i][y] != '.')
gotchanges = max(gotchanges, remove_list(solutions[x][y], grid[i][y]));
}
//Check lines
for(size_t j = 0; j < GRID_DIMENSION9; j++)
{
if(grid[x][j] != '.')
gotchanges = max(gotchanges, remove_list(solutions[x][y], grid[x][j]));
}
//Check box
for(size_t i = x/BOX_DIMENSION9 * BOX_DIMENSION9; i < (x/BOX_DIMENSION9 + 1) * BOX_DIMENSION9; i++)
{
for(size_t j = y/BOX_DIMENSION9 * BOX_DIMENSION9; j < (y/BOX_DIMENSION9 + 1) * BOX_DIMENSION9; j++)
{
if(grid[i][j] != '.')
gotchanges = max(gotchanges, remove_list(solutions[x][y], grid[i][j]));
}
}
if(solutions[x][y]->next->next == NULL)
grid[x][y] = solutions[x][y]->next->data;
}
}
}
}
for(size_t i = 0; i < GRID_DIMENSION9; i++)
{
for(size_t j = 0; j < GRID_DIMENSION9; j++)
{
size_t pos = i*GRID_DIMENSION9 + j;
size_t k = 0;
struct Node* current = solutions[i][j]->next;
while(current != NULL)
{
possibilities[pos][k] = current->data;
current = current->next;
k++;
}
if(k < GRID_DIMENSION9)
possibilities[pos][k] = -1;
}
}
}
// Solves sudoku.
//
// grid: grid where the digits of sudoku.
// pos: current pos the function is dealing with.
int solve9(char grid[GRID_DIMENSION9][GRID_DIMENSION9], char possibilities[GRID_DIMENSION9*GRID_DIMENSION9][GRID_DIMENSION9], size_t pos)
{
// Stops if it is the last coords.
if (pos == GRID_DIMENSION9 * GRID_DIMENSION9)
return 1;
// Gets row and col from the current position.
size_t row = pos / GRID_DIMENSION9;
size_t col = pos % GRID_DIMENSION9;
// Got to the next cell if there is already a number in the cell (row, col).
if (grid[row][col] != '.')
return solve9(grid, possibilities, pos + 1);
// Tests numbers between 1 and 9 for the cell (row, col).
for (size_t i = 0; i < GRID_DIMENSION9 && possibilities[pos][i] != -1; i++)
{
char possible_nb = possibilities[pos][i];
if (number_is_valid9(possible_nb, grid, row, col))
{
// Sets the cell (row, col) with the possible number.
grid[row][col] = possible_nb;
// Checks if the next cell won't have any problem in the grid.
// Otherwise, continues by setting current cell with the next number.
if (solve9(grid, possibilities, pos + 1))
return 1;
}
}
// Reinitializes grid to '.' if any number is possible.
grid[row][col] = '.';
// The loop exits, so there is not a possible number to set the cell.
// One possibility is that the original grid is not valid.
// The other possibility is there is one cell that is not valid in the grid.
return 0;
}
void drawHLine9(SDL_Surface* surface, int x, int y, int length, Uint32 color, int thickness)
{
SDL_LockSurface(surface);
for (int i = 0; i < thickness; i++)
{
for (int j = 0; j < length; j++)
{
Uint32* pixel = (Uint32*)surface->pixels + (y + i) * surface->pitch / sizeof(Uint32) + (x + j);
*pixel = color;
}
}
SDL_UnlockSurface(surface);
}
void drawVLine9(SDL_Surface* surface, int x, int y, int length, Uint32 color, int thickness)
{
SDL_LockSurface(surface);
for (int i = 0; i < thickness; i++)
{
for (int j = 0; j < length; j++)
{
Uint32* pixel = (Uint32*)surface->pixels + (y + j) * surface->pitch / sizeof(Uint32) + (x + i);
*pixel = color;
}
}
SDL_UnlockSurface(surface);
}
void draw_sudoku(char grid[9][9], char filepath[])
{
size_t CELL_SIZE = 90;
// Load the blank grid image
SDL_Surface* grid_surface = SDL_CreateRGBSurface(0, 800, 800, 32, 0, 0, 0, 0);
SDL_FillRect(grid_surface, NULL, SDL_MapRGB(grid_surface->format, 255, 255, 255)); // Fond blanc
// Load the digit images
SDL_Surface* digit_surfaces[GRID_DIMENSION9];
for (size_t i = 1; i < 10; i++) {
char filename[40];
sprintf(filename, "sudoku_solver/%li.png", i);
digit_surfaces[i-1] = IMG_Load(filename);
}
// Create a surface to draw on
SDL_Surface* draw_surface = SDL_CreateRGBSurface(0, grid_surface->w, grid_surface->h, 32, 0, 0, 0, 0);
SDL_FillRect(draw_surface, NULL, SDL_MapRGB(draw_surface->format, 255, 255, 255)); // Fond blanc
for (int col = 0; col < 9; col++)
drawVLine9(grid_surface, col*CELL_SIZE, 0, 800, SDL_MapRGB(draw_surface->format, 0, 0, 0), col%3==0?6:1);
for (int row = 0; row < 9; row++)
drawHLine9(grid_surface, 0, row*CELL_SIZE, 800, SDL_MapRGB(draw_surface->format, 0, 0, 0), row%3==0?6:1);
int w = digit_surfaces[0]->w;
int h = digit_surfaces[0]->h;
// Iterate through the grid and draw the digits
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
// Calculate the position of the cell in the image
int x = col * w;
int y = row * h;
// Get the digit to draw
int digit = grid[row][col] - '0';
// Blit the digit image onto the surface
SDL_Rect src_rect = {15, 15, w - 30, h - 30};
SDL_Rect dest_rect = {x + 10, y + 10, w - 20, h - 20};
SDL_BlitSurface(digit_surfaces[digit-1], &src_rect, grid_surface, &dest_rect);
}
}
// Save the surface as a PNG image
char result_filepath[strlen(filepath) + 12];
sprintf(result_filepath, "%s", filepath);
IMG_SavePNG(grid_surface, result_filepath);
// Clean up
SDL_FreeSurface(grid_surface);
for (int i = 0; i < 9; i++)
{
SDL_FreeSurface(digit_surfaces[i]);
}
SDL_FreeSurface(draw_surface);
}
// Main function.
int SolveSudoku(char grid[9][9])
{
char possibilities[9*9][9];
immediat_solutions9(grid, possibilities);
// Solves sudoku by checking if the original grid is not valid.
if (!solve9(grid, possibilities, 0))
errx(EXIT_FAILURE, "File found, but grid not valid.");
// Exits program with success.
return EXIT_SUCCESS;
}
// Main function.
int main_solver9(int argc, char *argv[])
{
// Exit if there is not only one parameter.
if (argc != 3)
errx(EXIT_FAILURE, "Usage: filepath + hexa");
HEXA9 = ((int) (*argv[2] - '0')) ? 1 : 0;
GRID_DIMENSION9 = HEXA9 ? 16 : 9;
BOX_DIMENSION9 = HEXA9 ? 4 : 3;
// Creates a new empty grid.
char grid[GRID_DIMENSION9][GRID_DIMENSION9];
char possibilities[GRID_DIMENSION9*GRID_DIMENSION9][GRID_DIMENSION9];
// Get the grid from file in one array.
get_grid_from_file9(argv[1], grid);
immediat_solutions9(grid, possibilities);
// Solves sudoku by checking if the original grid is not valid.
if (!solve9(grid, possibilities, 0))
errx(EXIT_FAILURE, "File found, but grid not valid.");
// Saves the result in a new file.
write_grid_in_file9(grid, argv[1]);
/*if(HEXA9)
draw_hexadoku(grid, argv[1]);
else
draw_sudoku(grid, argv[1]);*/
// Exits program with success.
return EXIT_SUCCESS;
}