| 1 | /* |
| 2 | * qqwing - A Sudoku solver and generator |
| 3 | * Copyright (C) 2006-2014 Stephen Ostermiller http://ostermiller.org/ |
| 4 | * Copyright (C) 2007 Jacques Bensimon ([email protected]) |
| 5 | * Copyright (C) 2011 Jean Guillerez (j.guillerez - orange.fr) |
| 6 | * Copyright (C) 2014 Michael Catanzaro ([email protected]) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | #ifndef QQWING_HPP |
| 23 | #define QQWING_HPP |
| 24 | |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace qqwing { |
| 29 | |
| 30 | using namespace std; |
| 31 | |
| 32 | class LogItem; |
| 33 | |
| 34 | const int GRID_SIZE = 3; |
| 35 | const int ROW_COL_SEC_SIZE = GRID_SIZE*GRID_SIZE; |
| 36 | const int SEC_GROUP_SIZE = ROW_COL_SEC_SIZE*GRID_SIZE; |
| 37 | const int BOARD_SIZE = ROW_COL_SEC_SIZE*ROW_COL_SEC_SIZE; |
| 38 | const int POSSIBILITY_SIZE = BOARD_SIZE*ROW_COL_SEC_SIZE; |
| 39 | |
| 40 | /** |
| 41 | * The version of QQwing, e.g. 1.2.3 |
| 42 | */ |
| 43 | string getVersion(); |
| 44 | |
| 45 | /** |
| 46 | * The board containing all the memory structures and |
| 47 | * methods for solving or generating sudoku puzzles. |
| 48 | */ |
| 49 | class SudokuBoard { |
| 50 | public: |
| 51 | enum PrintStyle { |
| 52 | ONE_LINE, |
| 53 | COMPACT, |
| 54 | READABLE, |
| 55 | CSV |
| 56 | }; |
| 57 | enum Difficulty { |
| 58 | UNKNOWN, |
| 59 | SIMPLE, |
| 60 | EASY, |
| 61 | INTERMEDIATE, |
| 62 | EXPERT |
| 63 | }; |
| 64 | enum Symmetry { |
| 65 | NONE, |
| 66 | ROTATE90, |
| 67 | ROTATE180, |
| 68 | MIRROR, |
| 69 | FLIP, |
| 70 | RANDOM |
| 71 | }; |
| 72 | SudokuBoard(); |
| 73 | bool setPuzzle(int* initPuzzle); |
| 74 | const int* getPuzzle(); |
| 75 | const int* getSolution(); |
| 76 | void printPuzzle(); |
| 77 | void printSolution(); |
| 78 | bool solve(); |
| 79 | |
| 80 | /** |
| 81 | * Count the number of solutions to the puzzle |
| 82 | */ |
| 83 | int countSolutions(); |
| 84 | |
| 85 | /** |
| 86 | * Count the number of solutions to the puzzle |
| 87 | * but return two any time there are two or |
| 88 | * more solutions. This method will run much |
| 89 | * falter than countSolutions() when there |
| 90 | * are many possible solutions and can be used |
| 91 | * when you are interested in knowing if the |
| 92 | * puzzle has zero, one, or multiple solutions. |
| 93 | */ |
| 94 | int countSolutionsLimited(); |
| 95 | |
| 96 | /** |
| 97 | * return true if the puzzle has a solution |
| 98 | * and only a single solution |
| 99 | */ |
| 100 | bool hasUniqueSolution(); |
| 101 | bool isSolved(); |
| 102 | void printSolveHistory(); |
| 103 | void setRecordHistory(bool recHistory); |
| 104 | void setLogHistory(bool logHist); |
| 105 | void setPrintStyle(PrintStyle ps); |
| 106 | bool generatePuzzle(); |
| 107 | bool generatePuzzleSymmetry(SudokuBoard::Symmetry symmetry); |
| 108 | int getGivenCount(); |
| 109 | int getSingleCount(); |
| 110 | int getHiddenSingleCount(); |
| 111 | int getNakedPairCount(); |
| 112 | int getHiddenPairCount(); |
| 113 | int getBoxLineReductionCount(); |
| 114 | int getPointingPairTripleCount(); |
| 115 | int getGuessCount(); |
| 116 | int getBacktrackCount(); |
| 117 | void printSolveInstructions(); |
| 118 | SudokuBoard::Difficulty getDifficulty(); |
| 119 | string getDifficultyAsString(); |
| 120 | ~SudokuBoard(); |
| 121 | |
| 122 | private: |
| 123 | /** |
| 124 | * The 81 integers that make up a sudoku puzzle. |
| 125 | * Givens are 1-9, unknowns are 0. |
| 126 | * Once initialized, this puzzle remains as is. |
| 127 | * The answer is worked out in "solution". |
| 128 | */ |
| 129 | int* puzzle; |
| 130 | |
| 131 | /** |
| 132 | * The 81 integers that make up a sudoku puzzle. |
| 133 | * The solution is built here, after completion |
| 134 | * all will be 1-9. |
| 135 | */ |
| 136 | int* solution; |
| 137 | |
| 138 | /** |
| 139 | * Recursion depth at which each of the numbers |
| 140 | * in the solution were placed. Useful for backing |
| 141 | * out solve branches that don't lead to a solution. |
| 142 | */ |
| 143 | int* solutionRound; |
| 144 | |
| 145 | /** |
| 146 | * The 729 integers that make up a the possible |
| 147 | * values for a Sudoku puzzle. (9 possibilities |
| 148 | * for each of 81 squares). If possibilities[i] |
| 149 | * is zero, then the possibility could still be |
| 150 | * filled in according to the Sudoku rules. When |
| 151 | * a possibility is eliminated, possibilities[i] |
| 152 | * is assigned the round (recursion level) at |
| 153 | * which it was determined that it could not be |
| 154 | * a possibility. |
| 155 | */ |
| 156 | int* possibilities; |
| 157 | |
| 158 | /** |
| 159 | * An array the size of the board (81) containing each |
| 160 | * of the numbers 0-n exactly once. This array may |
| 161 | * be shuffled so that operations that need to |
| 162 | * look at each cell can do so in a random order. |
| 163 | */ |
| 164 | int* randomBoardArray; |
| 165 | |
| 166 | /** |
| 167 | * An array with one element for each position (9), in |
| 168 | * some random order to be used when trying each |
| 169 | * position in turn during guesses. |
| 170 | */ |
| 171 | int* randomPossibilityArray; |
| 172 | |
| 173 | /** |
| 174 | * Whether or not to record history |
| 175 | */ |
| 176 | bool recordHistory; |
| 177 | |
| 178 | /** |
| 179 | * Whether or not to print history as it happens |
| 180 | */ |
| 181 | bool logHistory; |
| 182 | |
| 183 | /** |
| 184 | * A list of moves used to solve the puzzle. |
| 185 | * This list contains all moves, even on solve |
| 186 | * branches that did not lead to a solution. |
| 187 | */ |
| 188 | vector<LogItem*>* solveHistory; |
| 189 | |
| 190 | /** |
| 191 | * A list of moves used to solve the puzzle. |
| 192 | * This list contains only the moves needed |
| 193 | * to solve the puzzle, but doesn't contain |
| 194 | * information about bad guesses. |
| 195 | */ |
| 196 | vector<LogItem*>* solveInstructions; |
| 197 | |
| 198 | /** |
| 199 | * The style with which to print puzzles and solutions |
| 200 | */ |
| 201 | PrintStyle printStyle; |
| 202 | |
| 203 | /** |
| 204 | * The last round of solving |
| 205 | */ |
| 206 | int lastSolveRound; |
| 207 | bool reset(); |
| 208 | bool singleSolveMove(int round); |
| 209 | bool onlyPossibilityForCell(int round); |
| 210 | bool onlyValueInRow(int round); |
| 211 | bool onlyValueInColumn(int round); |
| 212 | bool onlyValueInSection(int round); |
| 213 | bool solve(int round); |
| 214 | int countSolutions(bool limitToTwo); |
| 215 | int countSolutions(int round, bool limitToTwo); |
| 216 | bool guess(int round, int guessNumber); |
| 217 | bool isImpossible(); |
| 218 | void rollbackRound(int round); |
| 219 | bool pointingRowReduction(int round); |
| 220 | bool rowBoxReduction(int round); |
| 221 | bool colBoxReduction(int round); |
| 222 | bool pointingColumnReduction(int round); |
| 223 | bool hiddenPairInRow(int round); |
| 224 | bool hiddenPairInColumn(int round); |
| 225 | bool hiddenPairInSection(int round); |
| 226 | void mark(int position, int round, int value); |
| 227 | int findPositionWithFewestPossibilities(); |
| 228 | bool handleNakedPairs(int round); |
| 229 | int countPossibilities(int position); |
| 230 | bool arePossibilitiesSame(int position1, int position2); |
| 231 | void addHistoryItem(LogItem* l); |
| 232 | void shuffleRandomArrays(); |
| 233 | void print(int* sudoku); |
| 234 | void rollbackNonGuesses(); |
| 235 | void clearPuzzle(); |
| 236 | void printHistory(vector<LogItem*>* v); |
| 237 | bool removePossibilitiesInOneFromTwo(int position1, int position2, int round); |
| 238 | }; |
| 239 | } |
| 240 | #endif |