-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.cpp
1124 lines (1072 loc) · 22.5 KB
/
Sudoku.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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <cstdlib>
#include <numeric>
#include <windows.h>
#include <string>
#define UNASSIGNED 0
using namespace std;
//-------------------------------------------
//Class Declaration
class Sudoku
{
private:
int size; //size of array - usualy 9
int delim; //deliminator - usually 3
int count;//Counter
int count1; //Alternate counter
int count2; //Alternate counter
int **arr; //Main array for sudoku
int **temparr; //Set equal to arr for solving purposes
int *nums; //Values in the sudoku - usually 1-9
int temp; //Temp
int temp1; //Alt temp
int rando; //Random val - usually 1-9
bool status; //True - Solved | False - Not Solved
bool playable; //True - Playable | False - Not Playable
public:
Sudoku(); //Constructor
~Sudoku(); //Deconstructor
//Getter and Setter
void setSize(int n); //Setter for Size
void setStatus(bool b); //Setter for Status
void integrateCount(); //Count++
int getSize(); //Getter for Size
int getCount(); //Getter for Count
bool getStatus(); //Getter for Status
bool getPlayable(); //Getter for Playable
//Setup Methods
void createboard(int k); //Creates 9x9 board
void displayBoard(int row, int column); //Display for Input - Places X at current
void displayBoard(); //Displays Board
void fillBoard(); //Fills board with a full working sudoku
void degrade(int option); //Degrades board to playable point
//Checker Methods
bool checkForInRow(int row, int num); //Checks if num is in row
bool checkForInColumn(int column, int num); //Checks if num is in Column
bool checkForInBox(int row, int column, int num); //Checks if num is in Box
bool UsedInBox(int boxRow, int boxCol, int num); //Checks if num is in Box - Alternate
bool tempCheckForInRow(int row, int n); //Checks if num is in row
bool tempCheckForInColumn(int column, int n); //Checks if num is in Column
bool tempCheckForInBox(int row, int column, int n); //Checks if num is in Box
bool checkNotFull(int &row, int &col); //Checks if the Sudoku is Full
bool checkSudoku(); //Checks if the Sudoku is Complete
bool isWorking(int row, int col, int num); //Checks if the Sudoku is Complete - Alternate
bool checkPlayable(); //Checks if the Sudoku is Playable
//Solve Methods
void guessSudoku(); //Lets the User guess a value
bool solveSudoku(); //Solves the Sudoku for the User
//Input Method
void inputSudoku(); //Lets the User Input a Sudoku
//File IO
void importSudoku(); //Lets the User Import a Sudoku
void importSudoku(int option); //Lets the User Import a premade Sudoku
void exportSudoku(); //Lets the User Save their Sudoku
};
//------------------------------------------
//Constructor|Destructor
Sudoku::Sudoku()
{
std::cout << "Welcome to Sudoku" << endl;
}
Sudoku::~Sudoku()
{
std::cout << "Sudoku is being Deleted" << endl;
}
// -----------------------------------------
//Reminders
/*---------------------------------------------
-- Reminders --
-- --
-- Be careful with system("cls") --
-- hints not working - Fixed --
-- Fill Bord might not work - Fixed --
-- --
-- --
-- --
--------------------------------------------- */
//-----------------------------------------
//Main
void main()
{
int input; //Used for input
bool created = false; //Checks if Board was Created
bool c1 = false; // c1-c5 bools for completed methods
bool c2 = false;
bool c3 = false;
bool c4 = false;
bool c5 = false;
bool saved = false; //Bool for saved
Sudoku sudo; //Class instantiation
//WINDOWS EXCLUSIVE
#ifdef _WIN32
system("color 02"); //Oooh Shiny colors
#endif
sudo.setSize(9); //Sets size to 9
sudo.createboard(9); //Creates a board 9x9
created = true;
c1 = true;
start: //Main Menu Loop
input = 0;
system("cls");
//Shows Progress
std::cout << "------------------------\n";
std::cout << "-- Completed Tasks \n";
std::cout << "-- \n";
if (c2 == true && c3 != true)
{
std::cout << "-- Generated Sudoku \n";
std::cout << "-- \n";
}
if (c3 == true)
{
std::cout << "-- Input Sudoku:: \n";
std::cout << "-- *Size = " << sudo.getSize() << "\n";
std::cout << "-- *Nums = " << "1-" << sudo.getSize() << "\n";
std::cout << "-- \n";
}
if (c4 == true)
{
std::cout << "-- Checked if Sudoku is working:: \n";
if (sudo.getStatus() == true)
{
std::cout << "-- *Status = Working\n";
}
else
{
std::cout << "-- *Status = Not Working\n";
}
std::cout << "-- \n";
}
if (c5 == true)
{
std::cout << "-- Checked if Sudoku is playable:: \n";
if (sudo.getPlayable() == true)
{
std::cout << "-- *Status = Playable\n";
}
else
{
std::cout << "-- *Status = Not Playable\n";
}
std::cout << "-- \n";
}
std::cout << "----------------------------------\n";
if (created == false)
{
std::cout << "*Make sure to Create Sudoku first*\n\n";
}
//MAIN MENU
std::cout << "1:Generate Sudoku\n";
std::cout << "2:Input Sudoku\n";
std::cout << "3:Check Sudoku\n";
std::cout << "4:Solve it yourself\n";
std::cout << "5:Computer Solve\n";
std::cout << "6:Save your Sudoku\n";
std::cout << "7:Exit\n";
std::cout << " :";
std::cin >> input;
//Switch for Option Inputs
switch (input) {
case 1:
system("cls");
sudo.fillBoard();
int input;
std::cout << "Input your Difficulty\n" << endl;
std::cout << " -1 to exit \n\n";
std::cout << "1 - Easy - 2 Hard) \n\n";
std::cout << "------------------\n";
std::cout << "::";
cin >> input;
sudo.degrade(input);
c2 = true;
goto start;
break;
case 2:
int inp;
system("cls");
std::cout << "Input Sudoku\n-------------\n";
std::cout << "\n";
std::cout << "Enter your option\n";
std::cout << "1:Keep your Current Sudoku\n";
std::cout << "2:Create a new one\n";
std::cout << "3:Import a Sudoku from a file\n";
std::cout << " :";
cin >> inp;
if (inp == 2){
sudo.setSize(9);
sudo.createboard(9);
sudo.inputSudoku();
c3 = true;
goto start;
break;
}
else if (inp == 1)
{
sudo.inputSudoku();
c3 = true;
goto start;
break;
}
else if (inp == 3)
{
system("cls");
std::cout << "Import Sudoku\n-------------\n";
std::cout << "\n";
std::cout << "Enter your option\n";
std::cout << "1:Import your own\n";
std::cout << "2:Import a prebuilt\n";
std::cout << " :";
cin >> inp;
if (inp == 1)
{
sudo.importSudoku();
}
else
{
system("cls");
std::cout << "Prebuilt Sudoku\n-------------\n";
std::cout << "\n";
std::cout << "Enter your option\n";
std::cout << "1:Easy\n";
std::cout << "2:Meduim\n";
std::cout << "3:Hard\n";
std::cout << " :";
cin >> inp;
sudo.importSudoku(inp);
}
c3 = true;
goto start;
break;
}
else{
c3 = true;
goto start;
break;
}
case 3:
system("cls");
std::cout << "Check Sudoku\n-------------\n";
std::cout << "\n";
std::cout << "Enter your option\n";
std::cout << "1:Check if playable\n";
std::cout << "2:Check if solved\n";
std::cout << " :";
cin >> inp;
if (inp == 1){
sudo.checkPlayable();
c5 = true;
goto start;
break;
}
else if (inp == 2)
{
sudo.checkSudoku();
c4 = true;
goto start;
break;
}
else{
goto start;
break;
}
case 4:
system("cls");
std::cout << "Solve your Sudoku\n-------------\n";
std::cout << "\n";
sudo.guessSudoku();
goto start;
break;
case 5:
system("cls");
std::cout << "Computer Solve\n-------------\n";
std::cout << "\n";
if (sudo.checkPlayable())
{
sudo.solveSudoku();
}
else
{
std::cout << "Not Solvable\n";
system("pause");
goto start;
break;
}
sudo.displayBoard();
system("pause");
goto start;
break;
case 6:
system("cls");
std::cout << "Save your Sudoku\n-------------\n";
std::cout << "\n";
sudo.exportSudoku();
saved = true;
goto start;
break;
case 7:
system("cls");
std::cout << "Exit\n-------------\n";
std::cout << "Are you sure you want to exit\n";
std::cout << "1:Yes\n";
std::cout << "2:No\n";
std::cout << "::";
cin >> inp;
//Exit Loop
if (inp == 1 && saved == false)
{
system("cls");
std::cout << "Do you want to save before you exit?\n";
std::cout << "1:Yes\n";
std::cout << "2:No\n";
std::cout << "::";
cin >> inp;
if (inp == 2)
{
sudo.~Sudoku();
exit(EXIT_FAILURE);
}
if (inp == 1)
{
sudo.exportSudoku();
sudo.~Sudoku();
exit(EXIT_FAILURE);
}
}
if (inp == 1 && saved == true)
{
sudo.~Sudoku();
exit(EXIT_FAILURE);
}
goto start;
break;
default:
system("cls");
goto start;
}
}
//-----------------------------------------
//Setter|Getter
void Sudoku::setSize(int n)//Sets Size
{
size = n;
delim = sqrt(size);
}
void Sudoku::setStatus(bool b)//Sets Status
{
status = b;
}
void Sudoku::integrateCount() //Integrates Count
{
count++;
}
int Sudoku::getSize() //Gets Size
{
return size;
}
int Sudoku::getCount() //Gets Count
{
return count;
}
bool Sudoku::getStatus() //Gets Status
{
return status;
}
bool Sudoku::getPlayable() //Gets Playable
{
return playable;
}
//-----------------------------------------
//Create Board
void Sudoku::createboard(int k) //Creates a board for KxK
{
srand(time(NULL)); //Sets new Random Seed
arr = new int *[k];
temparr = new int*[k];
// memory allocated for elements of each column.
for (int i = 0; i < k; i++)
{
arr[i] = new int[k];
temparr[i] = new int[k];
}
//Makes 9 by 9
for (int i = 0; i < k; i++)
{
for (int j = 0; j < k; j++)
{
temparr[i][j] = 0;
arr[i][j] = 0;
}
}
}
//-----------------------------------------
//Show Board
void Sudoku::displayBoard(int row,int column) //Displays Board for Input Function - X where current Val is
{
//Diplays the Sudoku
//All of this is just special formatting for how it displays
for (int i = 0; i < size; i++)
{
if (i % 3 == 0){
for (int k = 0; k <= ((size/delim)*8); k++)
{
std::cout << "-";
}
}
std::cout << "\n";
for (int j = 0; j < size; j++)
{
if (j % 3 == 0)
{
std::cout << "| ";
}
if (i == row && j == column)
{
std::cout << "X ";
}
else
{
std::cout << arr[i][j] << " ";
}
}
std::cout << "|";
std::cout << "\n";
}
for (int k = 0; k <= ((size / delim) * 8); k++)
{
std::cout << "-";
}
std::cout << "\n";
}
void Sudoku::displayBoard() //Displays Board
{
//Printer
// All of this is just special formatting for how it displays
std::cout << " ";
for (int k = 0; k < size; k++)
{
if (k % 3 == 0 && k != 0)
{
std::cout << " ";
std::cout << " ";
}
std::cout << " ";
std::cout << k;
}
std::cout << "\n ";
for (int i = 0; i < size; i++)
{
if (i % 3 == 0)
{
for (int k = 0; k <= ((size / delim) * 8); k++)
{
std::cout << "-";
}
}
std::cout << "\n";
std::cout << i;
for (int j = 0; j < size; j++)
{
if (j % 3 == 0)
{
std::cout << "| ";
}
std::cout << arr[i][j] << " ";
}
std::cout << "|";
std::cout << "\n ";
}
for (int k = 0; k <= ((size / delim) * 8); k++)
{
std::cout << "-";
}
std::cout << "\n ";
}
//-----------------------------------------
//Fill Board
void Sudoku::fillBoard() //Fills the Board with a working Sudoku then generates a unique Sudoku
{
int numA1, numA2, numB1, numB2, numC1, numC2, numE1, numE2;
int i;
std::ifstream is("startingboard.txt"); // open file
//Fills arr with values from the sudoku file
char c;
for (int m = 0; m < 9; m++){
for (int n = 0; n < 9; n++)
{
is.get(c);
i = c - '0';
arr[m][n] = i;
}
}
is.close(); // close file
srand(time(NULL));
// pick four number pairs to be swapped
do {
numA1 = ((rand() % 8) + 1);
numA2 = ((rand() % 8) + 1);
} while (numA1 == 0 || numA2 == 0 || numA1 == numA2);
do {
numB1 = ((rand() % 8) + 1);
numB2 = ((rand() % 8) + 1);
} while (numB1 == numA1 || numB1 == numA2 || numB2 == numA1 || numB2 == numA2);
do {
numC1 = ((rand() % 8) + 1);
numC2 = ((rand() % 8) + 1);
} while (numC1 == numC2 || numC1 == numB1 || numC1 == numB2 || numC2 == numB1 || numC2 == numB2 || numC1 == numA1 || numC1 == numA2 || numC2 == numA1 || numC2 == numA2);
do {
numE1 =((rand() % 8) + 1);
numE2 = ((rand() % 8) + 1);
} while (numE1 == numE2 || numE1 == numC1 || numE1 == numC2 || numE2 == numC1 || numE2 == numC2 || numE1 == numB1 || numE1 == numB2 || numE2 == numB1 || numE2 == numB2 || numE1 == numA1 || numE1 == numA2 || numE2 == numA1 ||numE2 == numA2);
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
if (arr[i][j] == numA1){
arr[i][j] = numA2;
}
else if (arr[i][j] == numA2){
arr[i][j] = numA1;
}
}
}
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
if (arr[i][j] == numB1){
arr[i][j] = numB2;
}
else if (arr[i][j] == numB2){
arr[i][j] = numB1;
}
}
}
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
if (arr[i][j] == numC1){
arr[i][j] = numC2;
}
else if (arr[i][j] == numC2){
arr[i][j] = numC1;
}
}
}
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
if (arr[i][j] == numE1){
arr[i][j] = numE2;
}
else if (arr[i][j] == numE2){
arr[i][j] = numE1;
}
}
}
//Refills if the sudoku isn't working
if (checkSudoku() == false)
{
std::cout << "Not working" << endl;
system("pause");
fillBoard();
}
}
void Sudoku::degrade(int option) //Degrades the Sudoku until it is playable
{
srand(time(NULL));
int i = 0;
int x = 0;
int temp;
switch (option)
{
case 0:
{
do
{
rando = (rand() % 9);
int rando1 = (rand() % 9);
if (arr[rando][rando1] != 0)
{
temp = arr[rando][rando1];
arr[rando][rando1] = 0;
if (checkPlayable())
i++;
else
arr[rando][rando1] = temp;
}
x++;
} while (i < 10 || x < 120);
break;
}
case 1:
{
do
{
rando = (rand() % 9);
int rando1 = (rand() % 9);
if (arr[rando][rando1] != 0)
{
temp = arr[rando][rando1];
arr[rando][rando1] = 0;
if (checkPlayable())
i++;
else
arr[rando][rando1] = temp;
}
x++;
} while (i < 45 || x < 120);
break;
}
case 2:
{
do
{
rando = (rand() % 9);
int rando1 = (rand() % 9);
if (arr[rando][rando1] != 0)
{
temp = arr[rando][rando1];
arr[rando][rando1] = 0;
if (checkPlayable())
i++;
else
arr[rando][rando1] = temp;
}
x++;
} while (i < 52 || x < 120);
break;
}
default:
{
break;
}
}
}
//-----------------------------------------
//Generate Board
bool Sudoku::checkForInRow(int row, int num) //Checks for Num
{
for (int col = 0; col < 9; col++){
if (arr[row][col] == num){
return true;
}
}
return false;
}
bool Sudoku::checkForInColumn(int column, int num) //Checks column for num
{
for (int row = 0; row < 9; row++){
if (arr[row][column] == num){
return true;
}
}
return false;
}
bool Sudoku::checkForInBox(int row, int column, int n) //Checks Box for Num
{
for (int i = row; i < (row + delim); i++)
{
for (int j = column; j < (column+delim); j++)
{
if (arr[i][j] == n){
return true;
}
}
}
return false;
}
bool Sudoku::tempCheckForInRow(int row, int n)
{
for (int j = 0; j < size; j++)
{
if (temparr[row][j] == n)
{
temp = j;
temp1 = row;
return true;
}
}
return false;
}
bool Sudoku::tempCheckForInColumn(int column, int n)
{
for (int j = 0; j < size; j++)
{
if (temparr[j][column] == n)
{
temp = column;
temp1 = j;
return true;
}
}
return false;
}
bool Sudoku::tempCheckForInBox(int row, int column, int n)
{
for (int i = row; i < (row + delim); i++)
{
for (int j = column; j < (column + delim); j++)
{
if (temparr[i][j] == n){
temp = i;
temp1 = j;
return true;
}
}
}
return false;
}
bool Sudoku::checkPlayable() //Checks if Sudoku is playable
{
int tempcheck = 0;
//Checking numbers 1-9
for (int i = 1; i <= size; i++)
{
for (int j = 0; j < size; j++)
{
for (int k = 0; k < size; k++)
{
//Sets the temp array to the num for solving
if (arr[j][k] == i)
{
//Rows
for (int n = 0; n < size; n++)
{
temparr[n][k] = 10;
}
//Column
for (int n = 0; n < size; n++)
{
temparr[j][n] = 10;
}
//Box
for (int n = ((j / 3) * 3); n < (((j / 3) * 3) + 3); n++)
{
for (int m = ((k / 3) * 3); m < (((k / 3) * 3) + 3); m++)
{
temparr[n][m] = 10;
}
}
//Checks where a solution might be
//Rows Check
tempcheck = 0;
string c = "";
int g = 0;
for (int n = 0; n < size; n++)
{
if (tempCheckForInRow(n, 0) == true)
{
g = n;
tempcheck++;
}
}
if (tempcheck == 1)
{
playable = true;
return true;
}
tempcheck = 0;
//Column Check
for (int n = 0; n < size; n++)
{
if (tempCheckForInColumn(n, 0) == true)
{
g = n;
tempcheck++;
}
}
if (tempcheck == 1)
{
playable = true;
return true;
}
tempcheck = 0;
//Box
int h = 0;
for (int n = 0; n < size; n = n + delim)
{
for (int m = 0; m < size; m = m + delim)
{
if (tempCheckForInBox(n, m, 0) == true)
{
g = n;
h = m;
tempcheck++;
}
}
}
if (tempcheck == 1)
{
playable = true;
return true;
}
}//end-if
}
}
}
playable = false;
return false;
}
bool Sudoku::checkSudoku() //Checks if Sudoku is complete
{
bool overall = false;
count = 0;
int tempcount = 0;
int totalcheck = 0;
//1st Check Box
for (int i = 0; i < size; i = i + delim)
{
for (int n = 0; n < size; n = n + delim)
{
for (int j = 1; j <= size; j++)
{
tempcount++;
if (checkForInBox(i, n, j) == true)
{
integrateCount();
}
}
}
}
//2nd Check Row
for (int n = 0; n < size; n++)
{
for (int j = 1; j <= size; j++)
{
tempcount++;
if (checkForInRow(n, j) == true)
{
integrateCount();
}
}
}
//3rd Check Column
for (int n = 0; n < size; n++)
{
for (int j = 1; j <= size; j++)
{
tempcount++;
if (checkForInColumn(n, j) == true)
{
integrateCount();
}
}
}
if (count == 243)
{
setStatus(true);
return true;
}
else
{
setStatus(false);
return false;
}
}
bool Sudoku::isWorking(int row, int col, int num) //Checks if the Sudoku is Working
{
return !checkForInRow(row, num) && !checkForInColumn(col, num) && !UsedInBox(row - row % 3, col - col % 3, num);
}
bool Sudoku::UsedInBox(int boxRow, int boxCol, int num) //Checks if the Num was Used in the Box - ALT
{
for (int row = 0; row < 3; row++){
for (int col = 0; col < 3; col++){
if (arr[row + boxRow][col + boxCol] == num){
return true;
}
}
}
return false;
}
bool Sudoku::checkNotFull(int &row, int &col) //Checks if the Sudoku isn't full
{
for (row = 0; row < 9; row++){
for (col = 0; col < 9; col++){
if (arr[row][col] == UNASSIGNED){
return true;
}
}
}
return false;
}
//-----------------------------------------
//Input a Sudoku
void Sudoku::inputSudoku() // Lets the User input a Sudoku
{
int input;
std::cout << "Input your Sudoku\n" << endl;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
top:
system("cls");
std::cout << " -1 to exit \n\n";
displayBoard(i,j);
std::cout << "------------------\n";
std::cout << "Input: " << i << "," << j << " \n";
std::cout << "::";
cin >> input;
if (input == -1)
{
break;
}
else if (input<=9 && input>=0)
{
arr[i][j] = input;
}
else
{
cout << "Not in Nums Range\n";
system("pause");
goto top;
}
}
if (input == -1)
{
break;
}
}
}
//-----------------------------------------
//Input Values into the Sudoku
void Sudoku::guessSudoku() // Lets the User input values
{
int x = 0;
int y = 0;
int z = 0;
std::cout << "Solve your Sudoku\n" << endl;
while (x != -1 && y != -1 && z != -1)
{
system("cls");
std::cout << " -1 to exit \n\n";
displayBoard();
std::cout << "------------------\n";
std::cout << "Rows:0-8" << endl;
std::cout << "Columns:0-8" << endl;
std::cout << "Input: (x,y)" << endl;
std::cout << "";
cin >> x;
if (x == -1)
{
break;
}
std::cout << ",\n";
cin >> y;
if (y == -1)
{
break;
}
std::cout << " = ";
cin >> z;
if (z == -1)
{
break;
}