author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 17:58:06 +0100 | |
changeset 13 | 2640a7fd96ca |
parent 9 | c3fecc82ade6 |
child 27 | 81a574d60c15 |
permissions | -rw-r--r-- |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
1 |
/** |
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
2 |
* test/demos/sudoku.c |
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
3 |
* Copyright (C) 2008 Markus Broeker |
0 | 4 |
* |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
5 |
* Sudoku - The Game |
0 | 6 |
*/ |
7 |
||
8 |
#include <stdio.h> |
|
9 |
#include <stdlib.h> |
|
10 |
#include <getopt.h> |
|
11 |
#include <string.h> |
|
12 |
#include <errno.h> |
|
13 |
#include <limits.h> |
|
14 |
||
15 |
#ifndef DIM |
|
16 |
#define DIM 9 |
|
17 |
#endif |
|
18 |
||
19 |
#define COLORED "\e[31m" |
|
20 |
#define FGCOLOR "\e[37m" |
|
21 |
#define BGCOLOR "\e[40m\e[2J" |
|
22 |
#define RESET "\e[0m" |
|
23 |
||
24 |
#define TRUE 1 |
|
25 |
#define FALSE 0 |
|
26 |
||
27 |
#ifndef u_int |
|
28 |
typedef unsigned int u_int; |
|
29 |
#endif |
|
30 |
||
31 |
#ifndef MAX_ITERATIONS |
|
32 |
#define MAX_ITERATIONS 10000000 |
|
33 |
#endif |
|
34 |
||
35 |
struct Field { |
|
36 |
u_int value; |
|
37 |
int isConst; |
|
38 |
}; |
|
39 |
||
40 |
/* the easiest way to get statistics */ |
|
41 |
static unsigned long iterations = 0; |
|
42 |
||
43 |
typedef struct Field Field; |
|
44 |
typedef Field **Board; |
|
45 |
||
46 |
/** |
|
47 |
* read nxn fields from commandline and store it in the Board variable |
|
48 |
*/ |
|
49 |
int readBoard (Board board) |
|
50 |
{ |
|
51 |
int i, j; |
|
52 |
||
53 |
u_int c; |
|
54 |
||
55 |
for (i = 0; i < DIM; i++) { |
|
56 |
for (j = 0; j < DIM; j++) { |
|
57 |
if (scanf ("%u", &c) < 1) { |
|
58 |
fprintf (stderr, "readBoard: Not enough elements\n"); |
|
59 |
fprintf (stderr, "readBoard: Got %d, needed %d\n", i * DIM + j, (DIM) * (DIM)); |
|
60 |
return FALSE; |
|
61 |
} |
|
62 |
board[i][j].value = c; |
|
63 |
board[i][j].isConst = (c == 0) ? 0 : 1; |
|
64 |
} |
|
65 |
} |
|
66 |
return TRUE; |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Show the content of the Board in a human readable manner |
|
71 |
*/ |
|
72 |
void showBoard (Board board, int colored) |
|
73 |
{ |
|
74 |
int i, j; |
|
75 |
||
76 |
for (i = 0; i < DIM; i++) { |
|
77 |
if ((i % 3) == 0) |
|
78 |
printf ("\n"); |
|
79 |
||
80 |
for (j = 0; j < DIM; j++) { |
|
81 |
if ((board[i][j].isConst && colored)) |
|
82 |
printf ("%s%2u%s ", COLORED, board[i][j].value, FGCOLOR); |
|
83 |
else |
|
84 |
printf ("%2u ", board[i][j].value); |
|
85 |
if (((j + 1) % 3) == 0) |
|
86 |
printf (" "); |
|
87 |
} |
|
88 |
printf ("\n"); |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* checks for the possibility of value at board[row][col] |
|
94 |
*/ |
|
95 |
int check (Board board, int row, int col, u_int value) |
|
96 |
{ |
|
97 |
int i, j; |
|
98 |
||
99 |
if (board[row][col].value > 0) |
|
100 |
return FALSE; |
|
101 |
||
102 |
for (i = 0; i < DIM; i++) { |
|
103 |
/* |
|
104 |
* check vertically |
|
105 |
*/ |
|
106 |
if (board[row][i].value == value) |
|
107 |
return FALSE; |
|
108 |
/* |
|
109 |
* check horizontally |
|
110 |
*/ |
|
111 |
if (board[i][col].value == value) |
|
112 |
return FALSE; |
|
113 |
} |
|
114 |
||
115 |
/* |
|
116 |
* check block for unique values |
|
117 |
*/ |
|
118 |
for (i = row - (row % 3); i < row - (row % 3) + 3; i++) |
|
119 |
for (j = col - (col % 3); j < col - (col % 3) + 3; j++) |
|
120 |
if (board[i][j].value == value) |
|
121 |
return FALSE; |
|
122 |
||
123 |
return TRUE; |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* go recursive from k to DIM^2 and try to solve the puzzle |
|
128 |
*/ |
|
129 |
int solveBoard (Board board, int k) |
|
130 |
{ |
|
131 |
u_int i; |
|
132 |
int q; |
|
133 |
||
134 |
if ((++iterations > MAX_ITERATIONS)) |
|
135 |
return FALSE; |
|
136 |
||
137 |
while (k < (DIM) * (DIM) && (board[k / DIM][k % DIM].value != 0)) |
|
138 |
k++; |
|
139 |
||
140 |
if (k == (DIM) * (DIM)) |
|
141 |
return TRUE; |
|
142 |
||
143 |
for (q = FALSE, i = 1; q == FALSE && i <= 9; i++) { |
|
144 |
if (check (board, k / DIM, k % DIM, i) == TRUE) { |
|
145 |
board[k / DIM][k % DIM].value = i; |
|
146 |
if (((DIM) * (DIM) - 1) == k) |
|
147 |
q = TRUE; |
|
148 |
else if ((q = solveBoard (board, k + 1)) == FALSE) { |
|
149 |
board[k / DIM][k % DIM].value = 0; |
|
150 |
} |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
return q; |
|
155 |
} |
|
156 |
||
157 |
/** |
|
158 |
* Deletes every Field of the Board (CLEANUP) |
|
159 |
*/ |
|
160 |
void freeBoard (const Board board) |
|
161 |
{ |
|
162 |
int i; |
|
163 |
||
164 |
for (i = 0; i < DIM; i++) { |
|
165 |
if (board[i] != NULL) |
|
166 |
free (board[i]); |
|
167 |
} |
|
168 |
} |
|
169 |
||
170 |
/** |
|
171 |
* Show plain usage screen |
|
172 |
*/ |
|
173 |
void usage (char *name) |
|
174 |
{ |
|
175 |
printf ("Usage: %s [-c] [-h]\n\n", name); |
|
176 |
printf ("-c\t\tcolored output\n"); |
|
177 |
printf ("-h\t\tshows this help\n"); |
|
178 |
printf ("Report bugs to mbroeker@largo.homelinux.org\n"); |
|
179 |
} |
|
180 |
||
181 |
int main (int argc, char **argv) |
|
182 |
{ |
|
183 |
Board b; |
|
184 |
int i; |
|
185 |
int ret; |
|
186 |
int colored = 0; |
|
187 |
||
188 |
while ((i = getopt (argc, argv, "ch")) != -1) { |
|
189 |
switch (i) { |
|
190 |
case 'h': |
|
191 |
usage (argv[0]); |
|
192 |
return EXIT_SUCCESS; |
|
193 |
case 'c': |
|
194 |
printf ("%s%s", BGCOLOR, FGCOLOR); |
|
195 |
colored = 1; |
|
196 |
break; |
|
197 |
default: |
|
198 |
usage (argv[0]); |
|
199 |
return EXIT_FAILURE; |
|
200 |
} |
|
201 |
} |
|
202 |
||
203 |
if ((DIM % 3) != 0) { |
|
204 |
fprintf (stderr, "DIM must be a multiple of 3\n"); |
|
205 |
fprintf (stderr, "RECOMPILE WITH -DDIM=9\n"); |
|
206 |
return EXIT_FAILURE; |
|
207 |
} |
|
208 |
||
209 |
if ((b = calloc (DIM, sizeof (Field *))) == NULL) { |
|
210 |
perror ("calloc"); |
|
211 |
return EXIT_FAILURE; |
|
212 |
} |
|
213 |
||
214 |
for (i = 0; i < DIM; i++) { |
|
215 |
if ((b[i] = malloc ((DIM * sizeof (Field)) + 1)) == NULL) { |
|
216 |
perror ("malloc"); |
|
217 |
return EXIT_FAILURE; |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
printf ("Sudoku - Enter a %dx%d Matrix\n", DIM, DIM); |
|
222 |
||
223 |
if (readBoard (b) == FALSE) { |
|
224 |
fprintf (stderr, "Input Error: Aborting\n"); |
|
225 |
return EXIT_FAILURE; |
|
226 |
} |
|
227 |
||
228 |
showBoard (b, colored); |
|
229 |
||
230 |
if ((ret = solveBoard (b, 0)) == TRUE) |
|
231 |
printf ("\nPuzzle solved after %lu recursions\n", iterations); |
|
232 |
else |
|
233 |
printf ("\nCannot solve your puzzle after %lu recursions\n", iterations); |
|
234 |
||
235 |
showBoard (b, colored); |
|
236 |
freeBoard (b); |
|
237 |
||
238 |
if (b != NULL) |
|
239 |
free (b); |
|
240 |
||
241 |
if (colored) |
|
242 |
printf ("%s", RESET); |
|
243 |
||
244 |
return EXIT_SUCCESS; |
|
245 |
} |