-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.c
187 lines (152 loc) · 4.94 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
#include "./src/core/sudoku.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void RemoveSpace(char* input) {
char temp[MAX_SIZE + 1];
int j = 0;
for (int i = 0; input[i] != '\0'; i++) {
if (input[i] != ' ') {
temp[j++] = input[i];
}
}
temp[j] = '\0';
strcpy(input, temp);
}
void inputBoard(Sudoku *s) {
printf("Enter the size of your Sudoku puzzle (4, 9, 16, 25): ");
scanf("%d", &s->size);
while (s->size != 4 && s->size != 9 && s->size != 16 && s->size != 25) {
printf("Error: Unsupported size. Please enter 4, 9, 16, or 25: ");
scanf("%d", &s->size);
}
while (getchar() != '\n');
printf("Enter your Sudoku puzzle (use 0 for empty cells):\n");
for (int i = 0; i < s->size; i++) {
int validRow = 0;
while (!validRow) {
printf("Row %d: ", i + 1);
char line[MAX_SIZE + 1];
fgets(line, sizeof(line), stdin);
line[strcspn(line, "\n")] = 0;
RemoveSpace(line);
if (strlen(line) != s->size) {
printf("Error: Each line must contain exactly %d digits.\n", s->size);
continue;
}
validRow = 1;
for (int j = 0; j < s->size; j++) {
char input = line[j];
if (input == '0') {
s->board[i][j] = ' ';
} else if (input >= '1' && input <= '0' + s->size) {
s->board[i][j] = input;
} else {
printf("Error: Invalid input '%c'. Please enter numbers between 1 and %d, or 0 for empty.\n", input, s->size);
validRow = 0;
break;
}
}
}
}
if (!validateInitialBoard(s)) {
printf("Error: Invalid Sudoku puzzle detected. Please check your input.\n");
exit(1);
}
}
int inputBoardFromFile(Sudoku *s, char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Error: Could not open the file. Ensure the file exists and is accessible.\n");
return 0;
}
// Determine grid size
char line[MAX_SIZE + 1];
int rowCount = 0;
int colCount = 0;
// First count the number of rows and find the longest line to determine column count
while (fgets(line, sizeof(line), file)) {
rowCount++;
int length = strcspn(line, "\n");
if (length > colCount) {
colCount = length;
}
}
fseek(file, 0, SEEK_SET);
if (rowCount != colCount || (rowCount != 4 && rowCount != 9 && rowCount != 16 && rowCount != 25)) {
printf("Error: Invalid grid size detected. Expected 4, 9, 16, or 25, but got %d.\n", rowCount);
fclose(file);
return 0;
}
// Set the size of Sudoku
s->size = rowCount;
// Read the rest of the file to initialize the board
for (int i = 0; i < s->size; i++) {
fgets(line, sizeof(line), file);
line[strcspn(line, "\n")] = 0;
for (int j = 0; j < s->size; j++) {
char input = line[j];
if (input == '0' || input == ' ') {
s->board[i][j] = ' ';
} else if (input >= '1' && input <= '0' + s->size) {
s->board[i][j] = input;
} else {
printf("Error: Invalid character '%c' found in file. Only digits 1-%d and spaces/0 are allowed.\n", input, s->size);
fclose(file);
return 0;
}
}
}
fclose(file);
if (!validateInitialBoard(s)) {
printf("Error: The puzzle from the file contains conflicts.\n");
return 0;
}
return 1;
}
int countClues(Sudoku *s) {
int clueCount = 0;
for (int i = 0; i < s->size; i++) {
for (int j = 0; j < s->size; j++) {
if (s->board[i][j] != ' ') {
clueCount++;
}
}
}
return clueCount;
}
int main(int argc, char *argv[]) {
int playing = 1;
while(playing) {
Sudoku s;
int steps = 0;
int choice;
memset(rowMask, 0, sizeof(rowMask));
memset(colMask, 0, sizeof(colMask));
memset(boxMask, 0, sizeof(boxMask));
if (argc == 2) {
if (!inputBoardFromFile(&s, argv[1])) {
return 0;
}
} else {
inputBoard(&s);
}
int clueCount = countClues(&s);
if (optimizedSolveSudoku(&s, &steps)) {
printGrid(&s);
printf("Sudoku solved in %d steps.\n", steps);
const char *difficulty = classifyDifficulty(clueCount, steps);
printf("Puzzle difficulty: %s\n", difficulty);
} else {
printf("No solution exists.\n");
}
printf("Do you want to restart the game or exit?\n");
printf("Press 1 to restart or 0 to exit: ");
scanf("%d", &choice);
if (choice == 0) {
playing = 0;
}
}
printf("Thanks for playing!\n");
return 0;
}