-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuSolver.cpp
688 lines (651 loc) · 16.1 KB
/
SudokuSolver.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#include <iostream>
#include <cstring>
#include <windows.h>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Sudoku
{
int sqr[9][9];
int solved ;
public:
Sudoku()
{
int i, j;
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
sqr[i][j] = 0;
}
}
solved = 0;
}
Sudoku(int a[][9])
{
int i, j;
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
sqr[i][j] = a[i][j];
}
}
solved = 0;
}
void input()
{
int i, j;
cout<<endl<<"Enter the values for the Sudoku ROW by ROW: "<<endl;
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
cout<<" ("<<i+1<<", "<<j+1<<"): ";
cin>>sqr[i][j];
}
}
}
int getel(int a, int b) //[a][b] is the matrix number for the element
{
return sqr[a][b];
}
void getAllEl(int a[][9])
{
int i, j;
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
a[i][j] = sqr[i][j];
}
}
}
int checkrow(int el, int r, int c)
{
int i, j;
for(i=0; i<=8; i++)
{
if (el == sqr[r][i])
{
return 1; //returns 1 if it's already present
}
}
return 0; //returns 0 if it's not in that row
}
int checkcolumn(int el, int r, int c)
{ //Takes in an element and check if it's already present in row or column
int i, j;
for(i=0; i<=8; i++)
{
if (el == sqr[i][c])
{
return 1;
}
}
return 0; //returns 0 if element is not in the column
}
void setel(int el, int r, int c) //Setter for any position
{
sqr[r][c] = el;
}
void display() //Display function
{
int i, j;
setcolor(5);
cout<<endl<<"----------------------------"<<endl;
setcolor(15);
for(i=0; i<9; i++)
{
setcolor(5);
cout<<"|";
setcolor(15);
for(j=0; j<9; j++)
{
if (sqr[i][j] != 0)
{
cout<<" "<<sqr[i][j];
if(j==2 || j==5 || j==8)
{
setcolor(5);
cout<<"|";
setcolor(15);
}
else
{
cout<<"|";
}
}
else
{
if(j==2 || j==5 || j==8)
{
setcolor(5);
cout<<" "<<"|";
setcolor(15);
}
else
{
cout<<" "<<"|";
}
}
}
if(i==2 || i==5 || i==8)
{
setcolor(5);
cout<<endl<<"----------------------------"<<endl;
setcolor(15);
}
else
{
cout<<endl<<"----------------------------"<<endl;
}
}
}
void setcolor(int k)
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, k);
}
int done()
{
int i, j;
int flag = 1;
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
if (sqr[i][j] == 0)
{
flag = 0;
}
}
}
return flag;
}
void getbox1(int a[][3])
{ //(1,1) to (3,3)
int i, j;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
a[i][j] = sqr[i][j];
}
}
}
void getbox2(int a[][3])
{ //(1,4) to (3,6)
int i, j, c=0;
for(i=0; i<3; i++)
{
for(j=3; j<6; j++)
{
c = j-3;
a[i][c] = sqr[i][j];
}
}
}
void getbox3(int a[][3])
{ //(1,7) to (3,9)
int i, j, c=0;
for(i=0; i<3; i++)
{
for(j=6; j<9; j++)
{
c = j-6;
a[i][c] = sqr[i][j];
}
}
}
void getbox4(int a[][3])
{ //(4,1) to (6,3)
int i, j, r;
for(i=3; i<6; i++)
{
for(j=0; j<3; j++)
{
r = i-3;
a[r][j] = sqr[i][j];
}
}
}
void getbox5(int a[][3])
{ //(4,4) to (6,6)
int i, j, r, c;
for(i=3; i<6; i++)
{
for(j=3; j<6; j++)
{
r = i-3;
c = j-3;
a[r][c] = sqr[i][j];
}
}
}
void getbox6(int a[][3])
{ //(4,7) to (6,9)
int i, j, r, c;
for(i=3; i<6; i++)
{
for(j=6; j<9; j++)
{
r = i-3;
c = j-6;
a[r][c] = sqr[i][j];
}
}
}
void getbox7(int a[][3])
{ //(7,1) to (9,3)
int i, j, r, c;
for(i=6; i<9; i++)
{
for(j=0; j<3; j++)
{
r = i-6;
a[r][j] = sqr[i][j];
}
}
}
void getbox8(int a[][3])
{ //(7,4) to (9,6)
int i, j, r, c;
for(i=6; i<9; i++)
{
for(j=3; j<6; j++)
{
r = i-6;
c = j-3;
a[r][c] = sqr[i][j];
}
}
}
void getbox9(int a[][3])
{ //(7,7) to (9,9)
int i, j, r, c;
for(i=6; i<9; i++)
{
for(j=6; j<9; j++)
{
r = i-6;
c = j-6;
a[r][c] = sqr[i][j];
}
}
}
};
struct sudoku
{
int mat[9][9];
};
void setcolor(int k)
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, k);
}
int searchbox(int a[][3], int num) //returns 1 if the number is already in the box
{
for(int i=0; i<3;i++)
{
for(int j=0; j<3; j++)
{
if(a[i][j] == num)
{
return 1;
}
}
}
return 0; //returns 0 if the number was not found in the box
}
bool issafe(Sudoku S1, int row, int column, int num) //returns True if safe and False if not
{
int BOX;
int a[3][3];
int flag=0;
if(S1.checkrow(num, row, column) != 0) //checks the row
{
flag = 1; //flag becomes 1 if it's NOT safe
return false;
}
if(S1.checkcolumn(num, row, column) != 0) //checks the column
{
flag = 1;
return false;
}
if(row<=2 && column<=2)
{
BOX = 1;
S1.getbox1(a);
int x = searchbox(a, num); //searchbox should return 1 if num is there in the box and 0 if it's not
if(x==1)
{
return false;
}
}
else if(row<=2 && column<=5)
{
BOX = 2;
S1.getbox2(a);
int x = searchbox(a, num);
if(x == 1)
{
return false;
}
}
else if(row<=2 && column<=8)
{
BOX = 3;
S1.getbox3(a);
int x = searchbox(a, num);
if(x == 1)
{
return false;
}
}
else if(row<=5 && column<=2)
{
BOX = 4;
S1.getbox4(a);
int x = searchbox(a, num);
if(x == 1)
{
return false;
}
}
else if(row<=5 && column<=5)
{
BOX = 5;
S1.getbox5(a);
int x = searchbox(a, num);
if(x == 1)
{
return false;
}
}
else if(row<=5 && column<=8)
{
BOX = 6;
S1.getbox6(a);
int x = searchbox(a, num);
if(x == 1)
{
return false;
}
}
else if(row<=8 && column<=2)
{
BOX = 7;
S1.getbox7(a);
int x = searchbox(a, num);
if(x == 1)
{
return false;
}
}
else if(row<=8 && column<=5)
{
BOX = 8;
S1.getbox8(a);
int x = searchbox(a, num);
if(x == 1)
{
return false;
}
}
else
{
BOX = 9;
S1.getbox9(a);
int x = searchbox(a, num);
if(x == 1)
{
return false;
}
}
return true;
}
void findunassigned(int a[9][9], int &r, int &c) //finds position where no number is assigned
{
int i, j;
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
if(a[i][j] == 0)
{
r = i;
c = j;
break;
}
}
}
}
bool SolveSudoku(Sudoku &S1) //Solves Sudoku using Backtracking
{
int i, j;
int a[9][9];
S1.getAllEl(a);
int k;
for(k=1; k<=9; k++)
{
findunassigned(a, i, j);
if(issafe(S1, i, j, k) && a[i][j] == 0 )
{
a[i][j] = k;
S1.setel(k, i, j);
if(SolveSudoku(S1))
{
return true;
}
a[i][j] = 0;
S1.setel(0, i, j);
}
if(S1.done() == 1) //All positions are filled
{
return true;
}
}
return false;
}
int CheckUserSolution(Sudoku S1)
{
int i, j, el;
int a[9][9], b[9][9];
Sudoku copy;
copy = S1;
bool x = SolveSudoku(copy); //creates a copy containing the right solution
do //gets all values for empty spots
{
S1.getAllEl(a);
findunassigned(a, i, j);
cout<<"\nElement ("<<i+1<<", "<<j+1<<"): ";
cin>>el;
a[i][j] = el;
S1.setel(el, i, j);
}
while(S1.done() != 1);
copy.getAllEl(b);
S1.display();
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
if(a[i][j] != b[i][j])
{
return 0;
}
}
}
return 1;
}
void intro() //Decoration purpose
{
for (int i=0; i<3; i++)
{
cout<<endl;
}
cout<<" _____________________ _______ _______ _____________________ _____________________ _______ ________ _______ _______"<<endl;
Sleep(300);
system("COLOR 01");
cout<<" | | | | | | | \\ | | | | / / | | | |"<<endl;
Sleep(300);
system("COLOR 02");
cout<<" | | | | | | | \\ | | | | / / | | | |"<<endl;
Sleep(300);
system("COLOR 03");
cout<<" | ____________| | | | | | ________ \\ | _______ | | | / / | | | | "<<endl;
Sleep(300);
system("COLOR 04");
cout<<" | | | | | | | | \\ \\ | | | | | | / / | | | |"<<endl;
Sleep(300);
system("COLOR 05");
cout<<" | | | | | | | | \\ \\ | | | | | | / / | | | |"<<endl;
Sleep(300);
system("COLOR 06");
cout<<" | |____________ | | | | | | | | | | | | | |/ / | | | |"<<endl;
Sleep(300);
system("COLOR 07");
cout<<" | | | | | | | | | | | | | | | / | | | |"<<endl;
Sleep(300);
system("COLOR 08");
cout<<" | | | | | | | | | | | | | | | | | | | |"<<endl;
Sleep(300);
system("COLOR 09");
cout<<" |____________ | | | | | | | | | | | | | | \\ | | | |"<<endl;
Sleep(300);
system("COLOR 01");
cout<<" | | | | | | | | | | | | | | | |\\ \\ | | | |"<<endl;
Sleep(300);
system("COLOR 02");
cout<<" | | | | | | | | / / | | | | | | \\ \\ | | | |"<<endl;
Sleep(300);
system("COLOR 06");
cout<<" ____________| | | |_______| | | |________/ / | |_______| | | | \\ \\ | |_______| |"<<endl;
Sleep(300);
system("COLOR 04");
cout<<" | | | | | / | | | | \\ \\ | |"<<endl;
Sleep(300);
system("COLOR 05");
cout<<" | | | | | / | | | | \\ \\ | |"<<endl;
Sleep(300);
system("COLOR 03");
cout<<" |_____________________| |_______________________| |_____________________/ |_____________________| |_______| \\_______\\ |_______________________|"<<endl;
cout<<endl;
cout<<endl;
}
main()
{
Sudoku S1, copy; //Main sudoku is S1, copy is just to experiment with
int solved = 0, i, j, val; //final value to check
int a[3][3];
bool find;
int choice=0;
char diff;
srand(time(NULL));
int n = rand()%3;
char filestringsE[3][20]={"E:\\Easy1.dat", "E:\\Easy2.dat", "E:\\Easy3.dat"}; //array of easy sudoku
char filestringsM[3][20]={"E:\\Medium1.dat", "E:\\Medium2.dat", "E:\\Medium3.dat"}; //array of moderate sudoku
char filestringsH[3][20]={"E:\\Hard1.dat", "E:\\Hard2.dat", "E:\\Hard3.dat"}; //array of hard sudoku
intro();
do
{
setcolor(15);
cout<<" Enter: "<<endl;
cout<<" 1 to Enter a New Sudoku. \n";
cout<<" 2 to Solve a Sudoku from Database. \n";
cout<<" 3 to Quit. \n\n";
cout<<" Enter choice: ";
cin>>choice;
if(choice == 1) //TO GET A NEW SUDOKU
{
cout<<"\n Enter Sudoku: ";
S1.input(); //gets input
S1.display(); //displays the sudoku
cout<<" CALCULATING...";
Sleep(1000);
find = SolveSudoku(S1); //Starts Solving
if(find == true) //solution found
{
S1.display();
}
else //solution not found
{
cout<<" No solution found. ";
}
}
else if(choice == 2) //CHOOSE FROM EXISTING DATABASE
{
cout<<"\n Select Difficulty: \n";
cout<<" E = Easy \n M = Moderate \n H = Hard ";
cout<<endl;
cout<<" Enter choice: ";
cin>>diff;
if(diff == 'E')
{
fstream file;
file.open(filestringsE[n], ios::in|ios::binary);
if(file) //picks a random sudoku from selected difficulty
{
file.read((char*)&S1, sizeof(Sudoku));
}
}
else if(diff == 'M')
{
fstream file;
file.open(filestringsM[n], ios::in|ios::binary);
if(file) //picks a random sudoku from selected difficulty
{
file.read((char*)&S1, sizeof(Sudoku));
}
}
else
{
fstream file;
file.open(filestringsH[n], ios::in|ios::binary);
if(file) //picks a random sudoku from selected difficulty
{
file.read((char*)&S1, sizeof(Sudoku));
}
}
S1.display(); //displays the sudoku
cout<<"\n Enter: "<<endl; //prompts user for solution
cout<<" 1 to Check your Solution. \n";
cout<<" 2 to let the program Solve the Sudoku. \n";
cout<<" Enter choice: ";
cin>>n;
cout<<endl;
if(n == 1)
{
val = CheckUserSolution(S1); //gets user's solution and checks it
if(val == 1)
{
cout<<" Correct Solution!"<<endl;;
}
else
{
cout<<" Incorrect Solution."<<endl;;
}
}
else //else, let the algorithm solve it
{
cout<<" CALCULATING..."<<endl;
Sleep(1000);
find = SolveSudoku(S1); //Solves it
if(find == true) //solution found
{
S1.display();
}
else //solution not found
{
cout<<" No solution found."<<endl;
}
}
}
}
while (choice!=3); //End of loop. Checks if user wishes to quit the program
return 0;
}