-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.c
201 lines (143 loc) · 4.3 KB
/
sudoku.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
/*
* create.c - CS50 Sudoku Project 'sudoku ' module
* see IMPLEMENTATION.md and DESIGN.md for more information.
*
* Dylan Beinstock, Salifyanji J Namwila, Veronica Quidore
* November 02, 2021.
*
* Dylan Beinstock, Salifyanji J Namwila, Veronica Quidore
* November 02, 2021.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include "./create/create.h"
#include "./solve/solve.h"
#include "./puzzle/puzzle.h"
/***************************** protoptypes ***********************/
//bool parse_user_puzzle(FILE* fp, puzzle_t* puzzle, char* level);
/**************************** main ****************************/
int main(const int argc, const char** argv)
{
//check for valid number of parameters
if (argc != 3 && argc != 2 && argc != 4) {
fprintf(stderr, "Incorrect number of arguments. ");
fprintf(stderr,"Usage: ./sudoku mode difficulty size\n");
return 1;
}
char* mode;
char* difficulty;
int size = 9;
//Allocate space and copy to variable for inputs
if(argc >= 2) {
mode = count_calloc_assert(strlen(argv[1]), sizeof(char) + 1, "mode");
strcpy(mode, argv[1]);
}
if (argc >= 3){
difficulty = count_calloc_assert(strlen(argv[2]), sizeof(char) + 1, "difficulty");
strcpy(difficulty, argv[2]);
}
if (argc >= 4){
if(sscanf(argv[3], "%d", &size) < 1) {
fprintf(stderr, "Error: Please input a valid integer for size\n");
free(mode);
free(difficulty);
return 2;
}
}
//Check mode and difficulty are valid
if(strcmp(mode, "create") != 0 && strcmp(mode, "solve") != 0) {
fprintf(stderr, "Mode must be 'create' or 'solve'\n");
// check that number of received arguments is 3
free(mode);
if(argc >= 3) { free(difficulty); }
return 2;
}
if(argc >= 3 && strcmp(difficulty, "easy") != 0 && strcmp(difficulty, "hard") != 0) {
fprintf(stderr, "Difficulty must be 'easy' or 'hard'\n");
free(mode); // remember to free malloc'd spaced ( on heap)
free(difficulty); // """
return 2;
}
//If 2 arguments are given make sure mode is solve
if(argc == 2 && strcmp(mode, "create") == 0 ) {
fprintf(stderr, "Create mode requires a third arguemnt for the difficulty\n");
free(mode);
return 2;
}
// seed the randomization for debugging purposes
time_t t;
srand((unsigned) time(&t));
//check the mode
if(strcmp(mode, "create") == 0)
{
puzzle_t* puzzle = puzzle_new(size); // create new puzzle w/ entries initialized to zero
if(puzzle == NULL) {
printf("Unsuccessful creating puzzle\n");
free(mode);
if(argc >= 3) {free(difficulty);}
return 5;
}
//build a full, complete sudoku
build_full_sudoku(puzzle, difficulty);
// delete points from fully built sudoku
create_sudoku(puzzle, difficulty);
//Print up the created sudoku
puzzle_print_formated(stdout, puzzle);
//Clean up for create:
free(mode);
if(argc >= 3) {free(difficulty);}
puzzle_delete(puzzle);
}
else
{
// initialize sudoku to new one
puzzle_t* parsed = puzzle_new(size);
if(parsed == NULL) {
printf("Unsuccessful creating puzzle");
free(mode);
if(argc >= 3) {free(difficulty);}
return 5;
}
// read from stdin
FILE* file = stdin;
// try parsing puzzle from stdin
bool status = parse_user_puzzle(file, parsed);
//If the parsing fails
if (!status)
{
fprintf(stderr, "Format Error: Could not parsed puzzle.\n");
//Clean up for solve:
free(mode);
if(argc >= 3) {free(difficulty);}
puzzle_delete(parsed);
fclose(file);
return 3;
}
else{
//If the given puzzle is not valid (less than 25 numbers or doesn't follow rules)
if(!is_valid_unsolved(parsed)) {
fprintf(stderr, "Invalid sudoku provided.\n");
//Clean up for solve:
free(mode);
if(argc == 3) { free(difficulty); }
puzzle_delete(parsed);
fclose(file);
return 4;
}
solve_sudoku(parsed,0 ,0 ,"easy");
puzzle_print_formated(stdout, parsed);
}
//Clean up for solve:
free(mode);
if(argc >= 3) { free(difficulty); }
puzzle_delete(parsed);
fclose(file);
}
return 0;
}