-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewsudoku.cpp
346 lines (307 loc) · 10.7 KB
/
newsudoku.cpp
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
/*****************************************************************Sudoku Solver**************************************************************/
#include<simplecpp>
struct block { //define a structure block for storing information about the block
int finalValue,possibleValueCount; //value of block and number of possible values
bool possibleValues[10]; //values possible for the block
block() { };
void initializeBlock() { //initialize the block
finalValue = 0;
possibleValues[0] = false;
for(int i=1;i<10;i++) possibleValues[i] = true;
possibleValueCount = 9;
}
void takeInput(int inputNumber) { //take input in block
if(finalValue==0 && inputNumber>0 && inputNumber<10) {
finalValue = inputNumber;
for(int i=1;i<10;i++) possibleValues[i] = false;
possibleValues[inputNumber] = true;
possibleValueCount = 1;
}
}
};
bool checkBlock(int rowNumber,int columnNumber,block SudokuGrid[][10]) {//check the block if it has only one possible value
if(SudokuGrid[rowNumber][columnNumber].possibleValueCount==1) {
for(int i=1;i<10;i++) {
if(SudokuGrid[rowNumber][columnNumber].possibleValues[i]) {
SudokuGrid[rowNumber][columnNumber].finalValue = i;
return true;
}
}
}
return false;
}
bool sudokuSolved(block SudokuGrid[][10]) { //check if the sudoku has been solved
for(int i=1;i<10;i++) {
for(int j=1;j<10;j++) {
if(SudokuGrid[i][j].finalValue==0) return false;
}
}
return true;
}
bool rowElimination(int rowNumber,block SudokuGrid[][10]) { //check row and modify possible values for each block
bool change = false;;
for(int i=1;i<10;i++) {
if(SudokuGrid[rowNumber][i].finalValue!=0) {
int temp = SudokuGrid[rowNumber][i].finalValue;
for(int j=1;j<10;j++) {
if(SudokuGrid[rowNumber][j].possibleValues[temp] && SudokuGrid[rowNumber][j].finalValue==0) {
SudokuGrid[rowNumber][j].possibleValues[temp] = false;
SudokuGrid[rowNumber][j].possibleValueCount = SudokuGrid[rowNumber][j].possibleValueCount - 1;
}
}
}
}
for(int i=1;i<10;i++) { //check row for confirmed blocks
if(SudokuGrid[rowNumber][i].finalValue==0) {
change = change || checkBlock(rowNumber,i,SudokuGrid);
}
}
return change;
}
bool columnElimination(int columnNumber,block SudokuGrid[][10]) { //check column and modify possible values for each block
bool change = false;;
for(int i=1;i<10;i++) {
if(SudokuGrid[i][columnNumber].finalValue!=0) {
int temp = SudokuGrid[i][columnNumber].finalValue;
for(int j=1;j<10;j++) {
if(SudokuGrid[j][columnNumber].possibleValues[temp] && SudokuGrid[j][columnNumber].finalValue==0) {
SudokuGrid[j][columnNumber].possibleValues[temp] = false;
SudokuGrid[j][columnNumber].possibleValueCount = SudokuGrid[j][columnNumber].possibleValueCount - 1;
}
}
}
}
for(int i=1;i<10;i++) { //check column for confirmed blocks
if(SudokuGrid[i][columnNumber].finalValue==0) {
change = change || checkBlock(i,columnNumber,SudokuGrid);
}
}
return change;
}
bool minigridElimination(int gridStartRow,int gridStartColumn,block SudokuGrid[][10]) {//check minigrid and modify possible values for each block
bool change = false;
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(SudokuGrid[gridStartRow+i][gridStartColumn+j].finalValue!=0) {
int temp = SudokuGrid[gridStartRow+i][gridStartColumn+j].finalValue;
for(int k=0;k<3;k++) {
for(int l=0;l<3;l++) {
if(SudokuGrid[gridStartRow+k][gridStartColumn+l].possibleValues[temp] && SudokuGrid[gridStartRow+k][gridStartColumn+l].finalValue==0) {
SudokuGrid[gridStartRow+k][gridStartColumn+l].possibleValues[temp] = false;
SudokuGrid[gridStartRow+k][gridStartColumn+l].possibleValueCount = SudokuGrid[gridStartRow+k][gridStartColumn+l].possibleValueCount - 1;
}
}
}
}
}
}
for(int i=0;i<3;i++) { //check minigrid for confirmed blocks
for(int j=0;j<3;j++) {
if(SudokuGrid[gridStartRow+i][gridStartColumn+j].finalValue==0) {
change = change || checkBlock(gridStartRow+i,gridStartColumn+j,SudokuGrid);
}
}
}
return change;
}
bool applyRowColumnMinigridElimination(block SudokuGrid[][10]) { //apply above three functions for the entire grid
bool change = false;
for(int i=1;i<10;i++) change = change || rowElimination(i,SudokuGrid);
for(int j=1;j<10;j++) change = change || columnElimination(j,SudokuGrid);
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) change = change || minigridElimination((i*3)+1,(j*3)+1,SudokuGrid);
}
return change;
}
/*A lone ranger is a value for a block which does not occur in any other block's possible value in a row/column/minigrid*/
bool scanLoneRangerInRows(int rowNumber,block SudokuGrid[][10]) { //scan for lone ranger in rows
bool change = false;
for(int i=1;i<10;i++) {
if(SudokuGrid[rowNumber][i].finalValue==0) {
for(int j=1;j<10 && (SudokuGrid[rowNumber][i].finalValue==0);j++) {
if(SudokuGrid[rowNumber][i].possibleValues[j]) {
bool loneRangerFound = true;
for(int k=1;k<10 && loneRangerFound;k++) {
if(k!=i) loneRangerFound = loneRangerFound && (SudokuGrid[rowNumber][k].possibleValues[j]==false);
}
if(loneRangerFound) {
SudokuGrid[rowNumber][i].takeInput(j);
change = true;
}
}
}
}
}
return change;
}
bool scanLoneRangerInColumns(int columnNumber,block SudokuGrid[][10]) { //scan for lone ranger in column
bool change = false;
for(int i=1;i<10;i++) {
if(SudokuGrid[i][columnNumber].finalValue==0) {
for(int j=1;j<10 && (SudokuGrid[i][columnNumber].finalValue==0);j++) {
if(SudokuGrid[i][columnNumber].possibleValues[j]) {
bool loneRangerFound = true;
for(int k=1;k<10 && loneRangerFound;k++) {
if(k!=i) loneRangerFound = loneRangerFound && (SudokuGrid[k][columnNumber].possibleValues[j]==false);
}
if(loneRangerFound) {
SudokuGrid[i][columnNumber].takeInput(j);
change = true;
}
}
}
}
}
return change;
}
bool scanLoneRangerInMinigrids(int gridStartRow,int gridStartColumn,block SudokuGrid[][10]) { //scan for lone ranger in minigrids
bool change = false;
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(SudokuGrid[gridStartRow+i][gridStartColumn+j].finalValue==0) {
for(int k=1;k<10 && (SudokuGrid[gridStartRow+i][gridStartColumn+j].finalValue==0);k++) {
if(SudokuGrid[gridStartRow+i][gridStartColumn+j].possibleValues[k]) {
bool loneRangerFound = true;
for(int l=0;l<3;l++) {
for(int m=0;m<3 && loneRangerFound;m++) {
if(l!=i || m!=j) {
loneRangerFound = loneRangerFound && (SudokuGrid[gridStartRow+l][gridStartColumn+m].possibleValues[k]==false);
}
}
}
if(loneRangerFound) {
SudokuGrid[gridStartRow+i][gridStartColumn+j].takeInput(k);
change = true;
}
}
}
}
}
}
return change;
}
bool solveSudokuByLogic(block SudokuGrid[][10]) { //use all the above functions to solve the sudoku
bool change = true;
while(true) {
while(change) {
change = applyRowColumnMinigridElimination(SudokuGrid);
}
for(int i=1;i<10;i++) change = change || scanLoneRangerInRows(i,SudokuGrid);
if(change) continue;
for(int i=1;i<10;i++) change = change || scanLoneRangerInColumns(i,SudokuGrid);
if(change) continue;
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) change = change || scanLoneRangerInMinigrids((i*3)+1,(j*3)+1,SudokuGrid);
}
if(change) continue;
if(!change) break;
}
if(sudokuSolved(SudokuGrid)) return true;
return false;
}
bool solveSudokuByBruteForce(block SudokuGrid[][10]) {//if the sudoku is not solvable by logic solve it by guessing and subsequent solving by logic
for(int i=1;i<10;i++) {
for(int j=1;j<10;j++) {
if(SudokuGrid[i][j].possibleValueCount==2) {
for(int m=1;m<10;m++) {
if(SudokuGrid[i][j].possibleValues[m]) {
block gridcopy[10][10];
for(int k=1;k<10;k++) {
for(int l=1;l<10;l++) {
gridcopy[k][l] = SudokuGrid[k][l];
}
}
gridcopy[i][j].takeInput(m);
bool solved = solveSudokuByLogic(gridcopy);
if(solved) {
for(int k=1;k<10;k++) {
for(int l=1;l<10;l++) {
SudokuGrid[k][l] = gridcopy[k][l];
}
}
return true;
}
}
}
}
}
}
return false;
}
void rectangle(int &cx,int &cy,int l,int h,int c1,int c2,int c3) {
Rectangle r(cx,cy,l,h);
r.setColor(COLOR(c1,c2,c3));
r.setFill(); r.imprint();
return;
}
void drawGrid(int n) {
for(int i=0;i<=n;i++) {
Line l(40,40+40*i,40+40*n,40+40*i);
l.setColor(COLOR("WHITE"));
l.imprint();
l.reset(40+40*i,40,40+40*i,40+40*n);
l.imprint();
}
}
int main() {
block blocksOfSudokuGrid[10][10];
for(int i=1;i<10;i++) {
for(int j=1;j<10;j++) {
blocksOfSudokuGrid[i][j].initializeBlock();
}
}
initCanvas("Sudoku Solver",500,500);
Rectangle interface(0,0,0,0);
for(int x=0;x<251;x++) {
interface.reset(250,250,500-2*x,500-2*x);
interface.setColor(COLOR(0,x,x/2.0));
interface.imprint();
}
int m,n;
drawGrid(9);
int p=450,q=100;
rectangle(p,q,80,40,255,255,255);
Text t(p,q,"EXIT");
t.imprint();
p=450; q=200;
rectangle(p,q,80,40,255,255,255);
t.reset(p,q,"DONE");
t.imprint();
XEvent event; {
while(true) {
nextEvent(&event);
int x,y;
if(mouseButtonPressEvent(&event)) {
int coordinate = getClick();
x = coordinate/65536; y = coordinate%65536;
n = (x - 40)/40 ; m = (y-40) / 40;
x = 40+(n+0.5)*40;
y = 40+(m+0.5)*40;
if(x<=400 && x>=40 && y<=400 && y>=40) rectangle(x,y,38,38,255,70,0);
if(x<=490 && x>=410 && y<=220 && y>=180) {
bool yourSudokuSolved = solveSudokuByLogic(blocksOfSudokuGrid);
if(!yourSudokuSolved) solveSudokuByBruteForce(blocksOfSudokuGrid);
break;
}
}
if(keyPressEvent(&event)) {
char inputNumber = charFromEvent(&event);
rectangle(x,y,38,38,0,0,255);
t.reset(x,y,inputNumber-48);
t.setColor(COLOR(255,255,255));
t.imprint();
if(inputNumber==48) t.hide();
blocksOfSudokuGrid[n+1][m+1].takeInput(inputNumber-48);
}
}
}
for(int i=1;i<10;i++) {
for(int j=1;j<10;j++) {
char c = blocksOfSudokuGrid[i][j].finalValue;
Text t(20+40*i,20+40*j,c); t.setColor(COLOR(255,255,255)); t.imprint();
}
}
int coordinate = getClick();
int a=coordinate/65536; int b=coordinate%65536;
if (a<=490 && a>=410 && b<=110 && b>=80) closeCanvas();
}