-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqqwing.cpp
2471 lines (2274 loc) · 85.8 KB
/
qqwing.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
/*
* qqwing - A Sudoku solver and generator
* Copyright (C) 2006-2011 Stephen Ostermiller
* http://ostermiller.org/qqwing/
* Copyright (C) 2007 Jacques Bensimon (jacques@ipm.com)
* Copyright (C) 2011 Jean Guillerez (j.guillerez - orange.fr)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <iostream>
#if HAVE_STRING_H == 1
#include <string.h>
#else
#include <string>
#endif
#include <stdio.h>
#include <vector>
#if HAVE_STDLIB_H == 1
#include <stdlib.h>
#else
#include <stdlib>
#endif
#if HAVE_GETTIMEOFDAY == 1
#include <sys/time.h>
#else
#include <time.h>
#endif
using namespace std;
class SuddokuBoard;
class LogItem;
/**
* The board containing all the memory structures and
* methods for solving or generating sudoku puzzles.
*/
class SudokuBoard {
public:
enum PrintStyle {
ONE_LINE,
COMPACT,
READABLE,
CSV,
};
enum Difficulty {
UNKNOWN,
SIMPLE,
EASY,
INTERMEDIATE,
EXPERT,
};
SudokuBoard();
bool setPuzzle(int* initPuzzle);
void setHighLights(bool printHighLights);
void printPuzzle();
void printSolution();
void printHighlights();
void printPhrase();
bool solve();
int countSolutions();
void printPossibilities();
bool isSolved();
void printSolveHistory();
void setRecordHistory(bool recHistory);
void setLogHistory(bool logHist);
void setPrintStyle(PrintStyle ps);
void setPhrase(string* phraseString) ;
// void fillUnused() ;
bool generatePuzzle();
void findHighLights();
void moveRow(int row, int secondrowpointer) ;
void moveCol(int col, int secondcolpointer) ;
int getGivenCount();
int getSingleCount();
int getHiddenSingleCount();
int getNakedPairCount();
int getHiddenPairCount();
int getBoxLineReductionCount();
int getPointingPairTripleCount();
int getGuessCount();
int getBacktrackCount();
void printSolveInstructions();
SudokuBoard::Difficulty getDifficulty();
string getDifficultyAsString();
~SudokuBoard();
private:
/**
* The 81 integers that make up a sudoku puzzle.
* Givens are 1-9, unknows are 0.
* Once initialized, this puzzle remains as is.
* The answer is worked out in "solution".
*/
int* puzzle;
/**
* The 81 integers that make up a sudoku puzzle.
* The solution is built here, after completion
* all will be 1-9.
*/
int* solution;
/**
* Recursion depth at which each of the numbers
* in the solution were placed. Useful for backing
* out solve branches that don't lead to a solution.
*/
int* solutionRound;
/**
* The 729 integers that make up a the possible
* values for a suduko puzzle. (9 possibilities
* for each of 81 squares). If possibilities[i]
* is zero, then the possibility could still be
* filled in according to the sudoku rules. When
* a possibility is eliminated, possibilities[i]
* is assigned the round (recursion level) at
* which it was determined that it could not be
* a possibility.
*/
int* possibilities;
/**
* An array the size of the board (81) containing each
* of the numbers 0-n exactly once. This array may
* be shuffled so that operations that need to
* look at each cell can do so in a random order.
*/
int* randomBoardArray;
/** kg
* An array the size of the board (81) containing
* 0 or 1 depending on whether the cell is "highlighted."
*/
int* highLites ;
/* kg
* A boolean that tells us that highLights have been set.
*/
bool HighLights ;
/**
* An array with one element for each position (9), in
* some random order to be used when trying each
* position in turn during guesses.
*/
int* randomPossibilityArray;
/**
* Whether or not to record history
*/
bool recordHistory;
/**
* Whether or not to print history as it happens
*/
bool logHistory;
/**
* A list of moves used to solve the puzzle.
* This list contains all moves, even on solve
* branches that did not lead to a solution.
*/
vector<LogItem*>* solveHistory;
/**
* A list of moves used to solve the puzzle.
* This list contains only the moves needed
* to solve the puzzle, but doesn't contain
* information about bad guesses.
*/
vector<LogItem*>* solveInstructions;
/**
* The style with which to print puzzles and solutions
*/
PrintStyle printStyle;
/**
* The last round of solving
*/
int lastSolveRound;
/* The phrase from the command line
*
*/
string* phraseString ;
// The unused letters to fill in phraseString with
string touse ;
/* Two integers to point to 1st and 2nd instances of
* repeated letters in phraseString. If no repeats,
* set to -1 ;
*/
int firstletter ;
int secondletter ;
/* integer array to hold place positions for the phraseString letters */
int lettermap[9];
bool reset();
bool singleSolveMove(int round);
bool onlyPossibilityForCell(int round);
bool onlyValueInRow(int round);
bool onlyValueInColumn(int round);
bool onlyValueInSection(int round);
bool solve(int round);
int countSolutions(int round, bool limitToTwo);
bool guess(int round, int guessNumber);
bool isImpossible();
void rollbackRound(int round);
bool pointingRowReduction(int round);
bool rowBoxReduction(int round);
bool colBoxReduction(int round);
bool pointingColumnReduction(int round);
bool hiddenPairInRow(int round);
bool hiddenPairInColumn(int round);
bool hiddenPairInSection(int round);
void mark(int position, int round, int value);
int findPositionWithFewestPossibilities();
bool handleNakedPairs(int round);
int countPossibilities(int position);
bool arePossibilitiesSame(int position1, int position2);
void addHistoryItem(LogItem* l);
void markRandomPossibility(int round);
void shuffleRandomArrays();
void print(int* sudoku);
void rollbackNonGuesses();
void clearPuzzle();
void printHistory(vector<LogItem*>* v);
bool removePossibilitiesInOneFromTwo(int position1, int position2, int round);
};
/**
* While solving the puzzle, log steps taken in a log item.
* This is useful for later printing out the solve history
* or gathering statistics about how hard the puzzle was to
* solve.
*/
class LogItem {
public:
enum LogType {
GIVEN,
SINGLE,
HIDDEN_SINGLE_ROW,
HIDDEN_SINGLE_COLUMN,
HIDDEN_SINGLE_SECTION,
GUESS,
ROLLBACK,
NAKED_PAIR_ROW,
NAKED_PAIR_COLUMN,
NAKED_PAIR_SECTION,
POINTING_PAIR_TRIPLE_ROW,
POINTING_PAIR_TRIPLE_COLUMN,
ROW_BOX,
COLUMN_BOX,
HIDDEN_PAIR_ROW,
HIDDEN_PAIR_COLUMN,
HIDDEN_PAIR_SECTION,
};
LogItem(int round, LogType type);
LogItem(int round, LogType type, int value, int position);
int getRound();
void print();
LogType getType();
~LogItem();
private:
void init(int round, LogType type, int value, int position);
/**
* The recursion level at which this item was gathered.
* Used for backing out log items solve branches that
* don't lead to a solution.
*/
int round;
/**
* The type of log message that will determine the
* message printed.
*/
LogType type;
/**
* Value that was set by the operation (or zero for no value)
*/
int value;
/**
* position on the board at which the value (if any) was set.
*/
int position;
};
//string IntToString(int num);
long getMicroseconds();
void shuffleArray(int* array, int size);
bool readPuzzleFromStdIn(int* puzzle);
int main(int argc, char *argv[]);
void printHelp(char* programName);
void printVersion();
void printAbout();
int getLogCount(vector<LogItem*>* v, LogItem::LogType type);
static inline int cellToColumn(int cell);
static inline int cellToRow(int cell);
static inline int cellToSectionStartCell(int cell);
static inline int cellToSection(int cell);
static inline int rowToFirstCell(int row);
static inline int columnToFirstCell(int column);
static inline int sectionToFirstCell(int section);
static inline int getPossibilityIndex(int valueIndex, int cell);
static inline int rowColumnToCell(int row, int column);
static inline int sectionToCell(int section, int offset);
#define GRID_SIZE 3
#define ROW_LENGTH (GRID_SIZE*GRID_SIZE)
#define COL_HEIGHT (GRID_SIZE*GRID_SIZE)
#define SEC_SIZE (GRID_SIZE*GRID_SIZE)
#define SEC_COUNT (GRID_SIZE*GRID_SIZE)
#define SEC_GROUP_SIZE (SEC_SIZE*GRID_SIZE)
#define NUM_POSS (GRID_SIZE*GRID_SIZE)
#define BOARD_SIZE (ROW_LENGTH*COL_HEIGHT)
#define POSSIBILITY_SIZE (BOARD_SIZE*NUM_POSS)
/**
* Main method -- the entry point into the program.
* Run with --help as an argument for usage and documentation
*/
int main(int argc, char *argv[]){
try {
// Start time for the application for timing
long applicationStartTime = getMicroseconds();
// The number of puzzles solved or generated.
int puzzleCount = 0;
enum Action {NONE, GENERATE, SOLVE};
// defaults for options
bool printPuzzle = false;
bool printSolution = false;
bool printHighLights = false;
bool printHistory = false;
bool printInstructions = false;
bool timer = false;
bool countSolutions = false;
Action action = NONE;
bool logHistory = false;
SudokuBoard::PrintStyle printStyle = SudokuBoard::READABLE;
int numberToGenerate = 1;
bool printStats = false;
SudokuBoard::Difficulty difficulty = SudokuBoard::UNKNOWN;
string phraseString[2] ;
phraseString[0] = " " ;
phraseString[1] = " " ;
// Read the arguments and set the options
{for (int i=1; i<argc; i++){
if (!strcmp(argv[i],"--puzzle")){
printPuzzle = true;
} else if (!strcmp(argv[i],"--nopuzzle")){
printPuzzle = false;
} else if (!strcmp(argv[i],"--solution")){
printSolution = true;
} else if (!strcmp(argv[i],"--nosolution")){
printSolution = false;
} else if (!strcmp(argv[i],"--history")){
printHistory = true;
} else if (!strcmp(argv[i],"--nohistory")){
printHistory = false;
} else if (!strcmp(argv[i],"--instructions")){
printInstructions = true;
} else if (!strcmp(argv[i],"--noinstructions")){
printInstructions = false;
} else if (!strcmp(argv[i],"--stats")){
printStats = true;
} else if (!strcmp(argv[i],"--nostats")){
printStats = false;
#if HAVE_GETTIMEOFDAY == 1
} else if (!strcmp(argv[i],"--timer")){
timer = true;
} else if (!strcmp(argv[i],"--notimer")){
timer = false;
#endif
} else if (!strcmp(argv[i],"--count-solutions")){
countSolutions = true;
} else if (!strcmp(argv[i],"--nocount-solutions")){
countSolutions = false;
} else if (!strcmp(argv[i],"--generate")){
action = GENERATE;
printPuzzle = true;
if (i+1 < argc && argv[i+1][0] >= '1' && argv[i+1][0] <= '9'){
numberToGenerate = atoi(argv[i+1]);
i++;
}
} else if (!strcmp(argv[i],"--difficulty")){
if (argc <= i+1){
cout << "Please specify a difficulty." << endl;
return 1;
} else if (!strcmp(argv[i+1],"simple")){
difficulty = SudokuBoard::SIMPLE;
} else if (!strcmp(argv[i+1],"easy")){
difficulty = SudokuBoard::EASY;
} else if (!strcmp(argv[i+1],"intermediate")){
difficulty = SudokuBoard::INTERMEDIATE;
} else if (!strcmp(argv[i+1],"expert")){
difficulty = SudokuBoard::EXPERT;
} else {
cout << "Difficulty expected to be simple, easy, intermediate, or expert, not " << argv[i+1] << endl;
return 1;
}
i++;
} else if (!strcmp(argv[i],"--phrase")) {
phraseString[0] = argv[++i] ;
printHighLights = true;
// cout << phraseString[0] << endl ;
} else if (!strcmp(argv[i],"--solve")){
action = SOLVE;
printSolution = true;
} else if (!strcmp(argv[i],"--log-history")){
logHistory = true;
} else if (!strcmp(argv[i],"--nolog-history")){
logHistory = false;
} else if (!strcmp(argv[i],"--one-line")){
printStyle=SudokuBoard::ONE_LINE;
} else if (!strcmp(argv[i],"--compact")){
printStyle=SudokuBoard::COMPACT;
} else if (!strcmp(argv[i],"--readable")){
printStyle=SudokuBoard::READABLE;
} else if (!strcmp(argv[i],"--csv")){
printStyle=SudokuBoard::CSV;
} else if (!strcmp(argv[i],"-n") || !strcmp(argv[i],"--number")){
if (i+1 < argc){
numberToGenerate = atoi(argv[i+1]);
i++;
} else {
cout << "Please specify a number." << endl;
return 1;
}
} else if (!strcmp(argv[i],"-h") || !strcmp(argv[i],"--help") || !strcmp(argv[i],"help") || !strcmp(argv[i],"?")){
printHelp(argv[0]);
return 0;
} else if (!strcmp(argv[i],"--version")){
printVersion();
return 0;
} else if (!strcmp(argv[i],"--about")){
printAbout();
return 0;
} else {
cout << "Unknown argument: '" << argv[i] << "'" << endl;
printHelp(argv[0]);
return 1;
}
}}
if (action == NONE){
cout << "Either --solve or --generate must be specified." << endl;
printHelp(argv[0]);
return 1;
}
// Initialize the random number generator
int timeSeed = time(NULL);
srand(timeSeed);
// If printing out CSV, print a header
if (printStyle == SudokuBoard::CSV){
/* if (printPuzzle) cout << "Puzzle,";
if (printSolution) cout << "Solution,";
if (printHistory) cout << "Solve History,";
if (printInstructions) cout << "Solve Instructions,";
if (countSolutions) cout << "Solution Count,";
if (timer) cout << "Time (milliseconds),";
if (printStats) cout << "Givens,Singles,Hidden Singles,Naked Pairs,Hidden Pairs,Pointing Pairs/Triples,Box/Line Intersections,Guesses,Backtracks,Difficulty";
cout << "" << endl; */
}
// Create a new puzzle board
// and set the options
SudokuBoard* ss = new SudokuBoard();
ss->setRecordHistory(printHistory || printInstructions || printStats || difficulty!=SudokuBoard::UNKNOWN);
ss->setLogHistory(logHistory);
ss->setPrintStyle(printStyle);
ss->setPhrase(phraseString) ;
ss->setHighLights(printHighLights);
// Solve puzzle or generate puzzles
// until end of input for solving, or
// until we have generated the specified number.
bool done = false;
int numberGenerated = 0;
while (!done){
// record the start time for the timer.
long puzzleStartTime = getMicroseconds();
// iff something has been printed for this particular puzzle
bool printedSomething = false;
// Record whether the puzzle was possible or not,
// so that we don't try to solve impossible givens.
bool havePuzzle = false;
if (action == GENERATE){
// Generate a puzzle
havePuzzle = ss->generatePuzzle();
if (!havePuzzle && printPuzzle){
cout << "Could not generate puzzle.";
if (printStyle==SudokuBoard::CSV){
cout << ",";
} else {
cout << endl;
}
printedSomething = true;
}
} else {
// Read the next puzzle on STDIN
int* puzzle = new int[BOARD_SIZE];
if (readPuzzleFromStdIn(puzzle)){
havePuzzle = ss->setPuzzle(puzzle);
if (!havePuzzle){
if (printPuzzle){
ss->printPuzzle();
printedSomething = true;
}
if (printSolution) {
cout << "Puzzle is not possible.";
if (printStyle==SudokuBoard::CSV){
cout << ",";
} else {
cout << endl;
}
printedSomething = true;
}
}
} else {
// Set loop to terminate when nothing is left on STDIN
havePuzzle = false;
done = true;
}
delete puzzle;
}
int solutions = 0;
if (havePuzzle){
// Count the solutions if requested.
// (Must be done before solving, as it would
// mess up the stats.)
if (countSolutions){
solutions = ss->countSolutions();
}
// Solve the puzzle
if (printSolution || printHistory || printStats || printInstructions
|| printHighLights ||difficulty!=SudokuBoard::UNKNOWN){
ss->solve();
}
// Bail out if it didn't meet the difficulty standards for generation
if (action == GENERATE){
if (difficulty!=SudokuBoard::UNKNOWN && difficulty!=ss->getDifficulty()){
havePuzzle = false;
} else {
numberGenerated++;
// Set loop to terminate if enough have been generated.
if (numberGenerated >= numberToGenerate) done = true;
}
}
}
if (havePuzzle){
// With a puzzle now in hand and possibly solved
// print out the solution, stats, etc.
printedSomething = true;
// Record the end time for the timer.
long puzzleDoneTime = getMicroseconds();
if (printHighLights) ss->printPhrase();
// Print the puzzle itself.
if (printPuzzle) ss->printPuzzle();
if (printHighLights) ss->printHighlights();
// Print the solution if there is one
if (printSolution){
if (ss->isSolved()){
ss->printSolution();
} else {
cout << "Puzzle has no solution.";
if (printStyle==SudokuBoard::CSV){
cout << ",";
} else {
cout << endl;
}
}
}
// Print the steps taken to solve or attempt to solve the puzzle.
if (printHistory) ss->printSolveHistory();
// Print the instructions for solving the puzzle
if (printInstructions) ss->printSolveInstructions();
// Print the number of solutions to the puzzle.
if (countSolutions){
if (printStyle == SudokuBoard::CSV){
cout << solutions << ",";
} else {
if (solutions == 0){
cout << "There are no solutions to the puzzle." << endl;
} else if (solutions == 1){
cout << "The solution to the puzzle is unique." << endl;
} else {
cout << "There are " << solutions << " solutions to the puzzle." << endl;
}
}
}
// Print out the time it took to solve the puzzle.
if (timer){
double t = ((double)(puzzleDoneTime - puzzleStartTime))/1000.0;
if (printStyle == SudokuBoard::CSV){
cout << t << ",";
} else {
cout << "Time: " << t << " milliseconds" << endl;
}
}
// Print any stats we were able to gather while solving the puzzle.
if (printStats){
int givenCount = ss->getGivenCount();
int singleCount = ss->getSingleCount();
int hiddenSingleCount = ss->getHiddenSingleCount();
int nakedPairCount = ss->getNakedPairCount();
int hiddenPairCount = ss->getHiddenPairCount();
int pointingPairTripleCount = ss->getPointingPairTripleCount();
int boxReductionCount = ss->getBoxLineReductionCount();
int guessCount = ss->getGuessCount();
int backtrackCount = ss->getBacktrackCount();
string difficultyString = ss->getDifficultyAsString();
if (printStyle == SudokuBoard::CSV){
cout << givenCount << "," << singleCount << "," << hiddenSingleCount
<< "," << nakedPairCount << "," << hiddenPairCount
<< "," << pointingPairTripleCount << "," << boxReductionCount
<< "," << guessCount << "," << backtrackCount
<< "," << difficultyString << ",";
} else {
cout << "Number of Givens: " << givenCount << endl;
cout << "Number of Singles: " << singleCount << endl;
cout << "Number of Hidden Singles: " << hiddenSingleCount << endl;
cout << "Number of Naked Pairs: " << nakedPairCount << endl;
cout << "Number of Hidden Pairs: " << hiddenPairCount << endl;
cout << "Number of Pointing Pairs/Triples: " << pointingPairTripleCount << endl;
cout << "Number of Box/Line Intersections: " << boxReductionCount << endl;
cout << "Number of Guesses: " << guessCount << endl;
cout << "Number of Backtracks: " << backtrackCount << endl;
cout << "Difficulty: " << difficultyString << endl;
}
}
puzzleCount++;
}
if (printedSomething && printStyle == SudokuBoard::CSV){
cout << endl;
}
}
delete ss;
long applicationDoneTime = getMicroseconds();
// Print out the time it took to do everything
if (timer){
double t = ((double)(applicationDoneTime - applicationStartTime))/1000000.0;
cout << puzzleCount << " puzzle" << ((puzzleCount==1)?"":"s") << " " << (action==GENERATE?"generated":"solved") << " in " << t << " seconds." << endl;
}
} catch (char const* s){
cout << s << endl;
return 1;
}
return 0;
}
void printVersion(){
cout << PACKAGE_STRING << endl;
}
void printAbout(){
cout << PACKAGE_NAME << " - Sudoku solver and generator." << endl;
cout << "Written by Stephen Ostermiller copyright 2006." << endl;
cout << "http://ostermiller.org/qqwing/" << endl;
cout << "" << endl;
cout << "This program is free software; you can redistribute it and/or modify" << endl;
cout << "it under the terms of the GNU General Public License as published by" << endl;
cout << "the Free Software Foundation; either version 2 of the License, or" << endl;
cout << "(at your option) any later version." << endl;
cout << "" << endl;
cout << "This program is distributed in the hope that it will be useful," << endl;
cout << "but WITHOUT ANY WARRANTY; without even the implied warranty of" << endl;
cout << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the" << endl;
cout << "GNU General Public License for more details." << endl;
cout << "" << endl;
cout << "You should have received a copy of the GNU General Public License" << endl;
cout << "along with this program; if not, write to the Free Software" << endl;
cout << "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA" << endl;
}
void printHelp(char* programName){
cout << programName << " <options>" << endl;
cout << "Sudoku solver and generator." << endl;
cout << " --generate <num> Generate new puzzles" << endl;
cout << " --solve Solve all the puzzles from standard input" << endl;
cout << " --difficulty <diff> Generate only simple,easy, intermediate, or expert" << endl;
cout << " --phrase <alphaval> Place the string 'alphaval' in the puzzle" << endl;
cout << " --puzzle Print the puzzle (default when generating)" << endl;
cout << " --nopuzzle Do not print the puzzle (default when solving)" << endl;
cout << " --solution Print the solution (default when solving)" << endl;
cout << " --nosolution Do not print the solution (default when generating)" << endl;
cout << " --stats Print statistics about moves used to solve the puzzle" << endl;
cout << " --nostats Do not print statistics (default)" << endl;
#if HAVE_GETTIMEOFDAY == 1
cout << " --timer Print time to generate or solve each puzzle" << endl;
cout << " --notimer Do not print solve or generation times (default)" << endl;
#endif
cout << " --count-solutions Count the number of solutions to puzzles" << endl;
cout << " --nocount-solutions Do not count the number of solutions (default)" << endl;
cout << " --history Print trial and error used when solving" << endl;
cout << " --nohistory Do not print trial and error to solve (default)" << endl;
cout << " --instructions Print the steps (at least 81) needed to solve the puzzle" << endl;
cout << " --noinstructions Do not print steps to solve (default)" << endl;
cout << " --log-history Print trial and error to solve as it happens" << endl;
cout << " --nolog-history Do not print trial and error to solve as it happens" << endl;
cout << " --one-line Print puzzles on one line of 81 characters" << endl;
cout << " --compact Print puzzles on 9 lines of 9 characters" << endl;
cout << " --readable Print puzzles in human readable form (default)" << endl;
cout << " --csv Ouput CSV format with one line puzzles" << endl;
cout << " --help Print this message" << endl;
cout << " --about Author and license information" << endl;
cout << " --version Display current version number" << endl;
}
/**
* Create a new Sudoku board
*/
SudokuBoard::SudokuBoard(){
puzzle = new int[BOARD_SIZE];
solution = new int[BOARD_SIZE];
solutionRound = new int[BOARD_SIZE];
highLites = new int[BOARD_SIZE]; // kg
possibilities = new int[POSSIBILITY_SIZE];
recordHistory = false;
HighLights = false;
firstletter = -1;
secondletter = -1;
printStyle = READABLE;
randomBoardArray = new int[BOARD_SIZE];
randomPossibilityArray = new int[NUM_POSS];
solveHistory = new vector<LogItem*>();
solveInstructions = new vector<LogItem*>();
{for (int i=0; i<BOARD_SIZE; i++){
randomBoardArray[i] = i;
}}
{for (int i=0; i<NUM_POSS; i++){
randomPossibilityArray[i] = i;
}}
}
SudokuBoard::~SudokuBoard(){
clearPuzzle();
delete puzzle;
delete solution;
delete solutionRound;
delete highLites ;
delete possibilities;
delete randomBoardArray;
delete randomPossibilityArray;
delete solveHistory;
delete solveInstructions;
}
/**
* Get the number of cells that are
* set in the puzzle (as opposed to
* figured out in the solution
*/
int SudokuBoard::getGivenCount(){
int count = 0;
{for (int i=0; i<BOARD_SIZE; i++){
if (puzzle[i] != 0) count++;
}}
return count;
}
/**
* Set the board to the given puzzle.
* The given puzzle must be an array of 81 integers.
*/
bool SudokuBoard::setPuzzle(int* initPuzzle){
{for (int i=0; i<BOARD_SIZE; i++){
puzzle[i] = (initPuzzle==NULL)?0:initPuzzle[i];
}}
return reset();
}
/* kg
* Set the highLights array to all 0s
*/
void SudokuBoard::setHighLights(bool printHighLights) {
HighLights = printHighLights ;
if ( HighLights )
{ for ( int i = 0 ; i < BOARD_SIZE ; i++ )
highLites[i] = 0 ;
}
}
/**
* Reset the board to its initial state with
* only the givens.
* This method clears any solution, resets statistics,
* and clears any history messages.
*/
bool SudokuBoard::reset(){
{for (int i=0; i<BOARD_SIZE; i++){
solution[i] = 0;
}}
{for (int i=0; i<BOARD_SIZE; i++){
solutionRound[i] = 0;
}}
{for (int i=0; i<POSSIBILITY_SIZE; i++){
possibilities[i] = 0;
}}
{for (int i=0;i<solveHistory->size();i++){
delete solveHistory->at(i);
}}
solveHistory->clear();
solveInstructions->clear();
int round = 1;
for (int position=0; position<BOARD_SIZE; position++){
if (puzzle[position] > 0){
int valIndex = puzzle[position]-1;
int valPos = getPossibilityIndex(valIndex,position);
int value = puzzle[position];
if (possibilities[valPos] != 0) return false;
mark(position,round,value);
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::GIVEN, value, position));
}
}
return true;
}
/**
* Get the difficulty rating.
*/
SudokuBoard::Difficulty SudokuBoard::getDifficulty(){
if (getGuessCount() > 0) return SudokuBoard::EXPERT;
if (getBoxLineReductionCount() > 0) return SudokuBoard::INTERMEDIATE;
if (getPointingPairTripleCount() > 0) return SudokuBoard::INTERMEDIATE;
if (getHiddenPairCount() > 0) return SudokuBoard::INTERMEDIATE;
if (getNakedPairCount() > 0) return SudokuBoard::INTERMEDIATE;
if (getHiddenSingleCount() > 0) return SudokuBoard::EASY;
if (getSingleCount() > 0) return SudokuBoard::SIMPLE;
return SudokuBoard::UNKNOWN;
}
/**
* Get the difficulty rating.
*/
string SudokuBoard::getDifficultyAsString(){
SudokuBoard::Difficulty difficulty = getDifficulty();
switch (difficulty){
case SudokuBoard::EXPERT: return "Expert"; break;
case SudokuBoard::INTERMEDIATE: return "Intermediate"; break;
case SudokuBoard::EASY: return "Easy"; break;
case SudokuBoard::SIMPLE: return "Simple"; break;
default: return "Unknown"; break;
}
return "Unknown";
}
/**
* Get the number of cells for which the solution was determined
* because there was only one possible value for that cell.
*/
int SudokuBoard::getSingleCount(){
return getLogCount(solveInstructions, LogItem::SINGLE);
}
/**
* Get the number of cells for which the solution was determined
* because that cell had the only possibility for some value in
* the row, column, or section.
*/
int SudokuBoard::getHiddenSingleCount(){
return getLogCount(solveInstructions, LogItem::HIDDEN_SINGLE_ROW) +
getLogCount(solveInstructions, LogItem::HIDDEN_SINGLE_COLUMN) +
getLogCount(solveInstructions, LogItem::HIDDEN_SINGLE_SECTION);
}
/**
* Get the number of naked pair reductions that were performed
* in solving this puzzle.
*/
int SudokuBoard::getNakedPairCount(){
return getLogCount(solveInstructions, LogItem::NAKED_PAIR_ROW) +
getLogCount(solveInstructions, LogItem::NAKED_PAIR_COLUMN) +
getLogCount(solveInstructions, LogItem::NAKED_PAIR_SECTION);
}
/**
* Get the number of hidden pair reductions that were performed
* in solving this puzzle.
*/
int SudokuBoard::getHiddenPairCount(){
return getLogCount(solveInstructions, LogItem::HIDDEN_PAIR_ROW) +
getLogCount(solveInstructions, LogItem::HIDDEN_PAIR_COLUMN) +
getLogCount(solveInstructions, LogItem::HIDDEN_PAIR_SECTION);
}
/**
* Get the number of pointing pair/triple reductions that were performed
* in solving this puzzle.
*/
int SudokuBoard::getPointingPairTripleCount(){
return getLogCount(solveInstructions, LogItem::POINTING_PAIR_TRIPLE_ROW)+
getLogCount(solveInstructions, LogItem::POINTING_PAIR_TRIPLE_COLUMN);
}
/**
* Get the number of box/line reductions that were performed
* in solving this puzzle.
*/
int SudokuBoard::getBoxLineReductionCount(){
return getLogCount(solveInstructions, LogItem::ROW_BOX)+
getLogCount(solveInstructions, LogItem::COLUMN_BOX);
}
/**
* Get the number lucky guesses in solving this puzzle.
*/
int SudokuBoard::getGuessCount(){
return getLogCount(solveInstructions, LogItem::GUESS);
}
/**
* Get the number of backtracks (unlucky guesses) required
* when solving this puzzle.
*/
int SudokuBoard::getBacktrackCount(){
return getLogCount(solveHistory, LogItem::ROLLBACK);
}
void SudokuBoard::markRandomPossibility(int round){
int remainingPossibilities = 0;
{for (int i=0; i<POSSIBILITY_SIZE; i++){
if (possibilities[i] == 0) remainingPossibilities++;
}}
int randomPossibility = rand()%remainingPossibilities;
int possibilityToMark = 0;
{for (int i=0; i<POSSIBILITY_SIZE; i++){
if (possibilities[i] == 0){
if (possibilityToMark == randomPossibility){
int position = i/NUM_POSS;
int value = i%NUM_POSS+1;
mark(position, round, value);
return;
}
possibilityToMark++;
}
}}
}
void SudokuBoard::shuffleRandomArrays(){
shuffleArray(randomBoardArray, BOARD_SIZE);
shuffleArray(randomPossibilityArray, NUM_POSS);
}
void SudokuBoard::clearPuzzle(){
// Clear any existing puzzle