-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
228 lines (203 loc) · 6.17 KB
/
main.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
#include "sudoku.h"
#include <stdio.h>
#include <stdlib.h>
int verbose = 0, pretty = 0, manual = 0; //global variables
void printUsage(char * call) //usage statement
{
printf("Usage: %s [-vpm] <sudoku_file>\n", call);
printf("\twhere\t-v\t\tspecifies 'verbose' mode.\n");
printf("\t\t-p\t\tspecifies 'pretty' mode.\n");
printf("\t\t-m\t\tspecifies 'manual entry' mode.\n");
printf("\t\t<sudoku_file>\tis a text file with 9 lines, each specifying one line\n\t\t\t\tof an unsolved puzzle. zeros represent unfilled squares.\n");
}
static void dS(void * s) { deleteSudoku((sudoku) s); } //delete function for the queue (part of the price for using a general purpose queue)
/* Input: Command line arguments argc and argv, and a file pointer f.
* Returns: -1 if an error occurs, otherwise 0.
* -----------------------------------------------------------
* Parses the command line arguments. This program takes between
* one and 4 arguments: 3 possible flags, representing verbose print,
* pretty print, and manual entry modes. If manual entry is not set,
* one of the arguments must be a sudoku text file (nominally 9 lines
* of integers between 1 and 9; in reality, any file with 81 integers
* between 1 and 9 will work correctly). Returns an error value of -1
* if the input is malformed; or if the filename is incorrect, and zero
* otherwise. Sets *f to be a pointer to the sudoku file.
*/
int parseInput(int argc, char ** argv, FILE ** f)
{
verbose = 0;
pretty = 0;
manual = 0;
*f = NULL;
int i = 0;
if (2 <= argc && argc <= 5) {
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') { //if the first character is a -, we have a flag
switch (argv[i][1]) {
case 'v':
verbose = 1;
break;
case 'p':
pretty = 1;
break;
case 'm':
manual = 1;
break;
default:
return -1;
}
} else { //otherwise, we assume it is a filename
*f = fopen(argv[i], "r");
if (!(*f)) {
printf("%s is not a file.\n", argv[i]);
free(*f);
return -1;
}
}
}
return 0;
}
return 1;
}
/* Input: The initialized pointer to the file.
* Returns: NULL if an error occurs, otherwise the initialized board.
* -----------------------------------------------------------
* Initializes the board. If manual entry is set, the function prompts
* the user for input; otherwise, it reads from the file. The function
* reads values in row over column, and sets the value of each cell if
* it encounters an integer other than zero. Returns NULL if manual is
* not set and f is NULL, or if it encounters malform input, and
* otherwise returns an initialized sudoku.
*/
sudoku initBoard(FILE * f)
{
sudoku s = newSudoku();
int r, c, v = 0;
if (manual) //manual entry mode, from the command line. Can also be used for redirecting input from a file
{
printf("Enter the given values for a 9X9 sudoku, or enter 0 if the cell is not given.\n");
for (r = 1; r <= 9; ++r)
{
printf("Enter the elements of row %d:\n", r);
for(c = 1; c <= 9; ++c)
{
if (scanf("%1d", &v) != 1) //scan character by character
{
printf("Malformed input. Exiting.\n");
return NULL;
}
if (v > 9 || v < 0)
{
printf("Value out of range. Exiting.\n");
return NULL;
}
if (v != 0) setCell(s, r, c, v);
}
}
}
else //read info from the file
{
if (!f) return NULL;
for (r = 1; r <= 9; ++r)
{
for(c = 1; c <= 9; ++c)
{
if (fscanf(f, "%1d", &v) != 1) //scan character by character
{
printf("Malformed file. Exiting.\n");
return NULL;
}
if (v > 9 || v < 0)
{
printf("Value out of range. Exiting.\n");
return NULL;
}
if (v != 0) setCell(s, r, c, v);
}
}
}
return s;
}
/* Input: An empty queue, and an initialized sudoku s, an empty queue
* for the solutions, and a pointer to a variable to store the
* number of guesses
* Returns: NULL if no possible solutions exist or the queue is not
* empty, and otherwise the solution to the input puzzle.
* -----------------------------------------------------------
* Solves the puzzle using backtracking. The solver is initialized by
* putting the puzzle into the empty queue. Then, each iteration will
* pull a board out of the queue, perform a simple reduction on that
* board, and then make a guess on the cell which has the least number
* of possibilities. If verbose is set, it prints each board before
* making a guess, giving a sense of the whole solution process.
*
* If there is nothing to pull out of the queue, there are no possible
* solutions to the puzzle, and the function returns an error value of
* NULL. If the queue is not empty at initialization, the function
* prints an error message regardless of the state of the flags, and
* returns an error.
*/
sudoku solve(queue q, sudoku s, int * guesses)
{
if (!isEmptyQueue(q)) {
printf("Error: call to solve with a non-empty queue");
return NULL;
}
putQueue(q, (void *) s);
*guesses = 0;
int slvd = 0;
while (1)
{
if (getQueue(q, (void **) &s)){
return NULL;
}
reduce(s);
if(verbose) {
system("clear");
printSudoku(s, pretty);
printf("\n");
}
slvd = checkSudoku(s);
if (slvd == -1) deleteSudoku(s);
else if (slvd == 1) return s;
else
{
if(guess(q, s)) {
printf("Error: Full queue");
return NULL;
}
deleteSudoku(s);
(*guesses)++;
}
}
}
int main(int argc, char ** argv)
{
FILE * f = NULL;
if (parseInput(argc, argv, &f)) { //parse our input; exit if there's an error
printUsage(argv[0]);
return -1;
}
sudoku s = initBoard(f); //initialize the board; exit if there's an error
if (!s) {
printUsage(argv[0]);
return -1;
}
fclose(f);
if (verbose || manual) { //if we're in manual entry or verbose mode, print our starting board
printSudoku(s, pretty);
printf("\n");
}
queue q = newQueue(-1, dS); //create a new queue
int guesses;
sudoku sol = solve(q, s, &guesses);
if (!sol) printf("no solutions\n");
else if (verbose || manual) {
printf("\t--Solution--\n");
printf("Number of guesses made: %d\n\n", guesses);
}
if (sol) printSudoku(sol, pretty);
deleteSudoku(sol);
deleteQueue(q);
return 0;
}