| 1 | /* |
| 2 | * qqwing - 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 | #include "config.h" |
| 23 | |
| 24 | #include <cstdlib> |
| 25 | #include <iostream> |
| 26 | |
| 27 | #include "qqwing.hpp" |
| 28 | |
| 29 | namespace qqwing { |
| 30 | |
| 31 | string getVersion(){ |
| 32 | return VERSION; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * While solving the puzzle, log steps taken in a log item. |
| 37 | * This is useful for later printing out the solve history |
| 38 | * or gathering statistics about how hard the puzzle was to |
| 39 | * solve. |
| 40 | */ |
| 41 | class LogItem { |
| 42 | public: |
| 43 | enum LogType { |
| 44 | GIVEN, |
| 45 | SINGLE, |
| 46 | HIDDEN_SINGLE_ROW, |
| 47 | HIDDEN_SINGLE_COLUMN, |
| 48 | HIDDEN_SINGLE_SECTION, |
| 49 | GUESS, |
| 50 | ROLLBACK, |
| 51 | NAKED_PAIR_ROW, |
| 52 | NAKED_PAIR_COLUMN, |
| 53 | NAKED_PAIR_SECTION, |
| 54 | POINTING_PAIR_TRIPLE_ROW, |
| 55 | POINTING_PAIR_TRIPLE_COLUMN, |
| 56 | ROW_BOX, |
| 57 | COLUMN_BOX, |
| 58 | HIDDEN_PAIR_ROW, |
| 59 | HIDDEN_PAIR_COLUMN, |
| 60 | HIDDEN_PAIR_SECTION |
| 61 | }; |
| 62 | LogItem(int round, LogType type); |
| 63 | LogItem(int round, LogType type, int value, int position); |
| 64 | int getRound(); |
| 65 | void print(); |
| 66 | LogType getType(); |
| 67 | ~LogItem(); |
| 68 | private: |
| 69 | void init(int round, LogType type, int value, int position); |
| 70 | /** |
| 71 | * The recursion level at which this item was gathered. |
| 72 | * Used for backing out log items solve branches that |
| 73 | * don't lead to a solution. |
| 74 | */ |
| 75 | int round; |
| 76 | |
| 77 | /** |
| 78 | * The type of log message that will determine the |
| 79 | * message printed. |
| 80 | */ |
| 81 | LogType type; |
| 82 | |
| 83 | /** |
| 84 | * Value that was set by the operation (or zero for no value) |
| 85 | */ |
| 86 | int value; |
| 87 | |
| 88 | /** |
| 89 | * position on the board at which the value (if any) was set. |
| 90 | */ |
| 91 | int position; |
| 92 | }; |
| 93 | |
| 94 | void shuffleArray(int* array, int size); |
| 95 | SudokuBoard::Symmetry getRandomSymmetry(); |
| 96 | int getLogCount(vector<LogItem*>* v, LogItem::LogType type); |
| 97 | static inline int cellToColumn(int cell); |
| 98 | static inline int cellToRow(int cell); |
| 99 | static inline int cellToSectionStartCell(int cell); |
| 100 | static inline int cellToSection(int cell); |
| 101 | static inline int rowToFirstCell(int row); |
| 102 | static inline int columnToFirstCell(int column); |
| 103 | static inline int sectionToFirstCell(int section); |
| 104 | static inline int getPossibilityIndex(int valueIndex, int cell); |
| 105 | static inline int rowColumnToCell(int row, int column); |
| 106 | static inline int sectionToCell(int section, int offset); |
| 107 | |
| 108 | /** |
| 109 | * Create a new Sudoku board |
| 110 | */ |
| 111 | SudokuBoard::SudokuBoard() : |
| 112 | puzzle ( new int[BOARD_SIZE] ), |
| 113 | solution ( new int[BOARD_SIZE] ), |
| 114 | solutionRound ( new int[BOARD_SIZE] ), |
| 115 | possibilities ( new int[POSSIBILITY_SIZE] ), |
| 116 | randomBoardArray ( new int[BOARD_SIZE] ), |
| 117 | randomPossibilityArray ( new int[ROW_COL_SEC_SIZE] ), |
| 118 | recordHistory ( false ), |
| 119 | logHistory( false ), |
| 120 | solveHistory ( new vector<LogItem*>() ), |
| 121 | solveInstructions ( new vector<LogItem*>() ), |
| 122 | printStyle ( READABLE ), |
| 123 | lastSolveRound (0) |
| 124 | { |
| 125 | {for (int i=0; i<BOARD_SIZE; i++){ |
| 126 | randomBoardArray[i] = i; |
| 127 | }} |
| 128 | {for (int i=0; i<ROW_COL_SEC_SIZE; i++){ |
| 129 | randomPossibilityArray[i] = i; |
| 130 | }} |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get the number of cells that are |
| 135 | * set in the puzzle (as opposed to |
| 136 | * figured out in the solution |
| 137 | */ |
| 138 | int SudokuBoard::getGivenCount(){ |
| 139 | int count = 0; |
| 140 | {for (int i=0; i<BOARD_SIZE; i++){ |
| 141 | if (puzzle[i] != 0) count++; |
| 142 | }} |
| 143 | return count; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Set the board to the given puzzle. |
| 148 | * The given puzzle must be an array of 81 integers. |
| 149 | */ |
| 150 | bool SudokuBoard::setPuzzle(int* initPuzzle){ |
| 151 | {for (int i=0; i<BOARD_SIZE; i++){ |
| 152 | puzzle[i] = (initPuzzle==NULL)?0:initPuzzle[i]; |
| 153 | }} |
| 154 | return reset(); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Retrieves the puzzle as an unmodifiable array of 81 integers. |
| 159 | */ |
| 160 | const int* SudokuBoard::getPuzzle(){ |
| 161 | return puzzle; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Retrieves the puzzle's solution as an unmodifiable array of 81 integers. |
| 166 | */ |
| 167 | const int* SudokuBoard::getSolution(){ |
| 168 | return solution; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Reset the board to its initial state with |
| 173 | * only the givens. |
| 174 | * This method clears any solution, resets statistics, |
| 175 | * and clears any history messages. |
| 176 | */ |
| 177 | bool SudokuBoard::reset(){ |
| 178 | {for (int i=0; i<BOARD_SIZE; i++){ |
| 179 | solution[i] = 0; |
| 180 | }} |
| 181 | {for (int i=0; i<BOARD_SIZE; i++){ |
| 182 | solutionRound[i] = 0; |
| 183 | }} |
| 184 | {for (int i=0; i<POSSIBILITY_SIZE; i++){ |
| 185 | possibilities[i] = 0; |
| 186 | }} |
| 187 | |
| 188 | {for (unsigned int i=0; i<solveHistory->size(); i++){ |
| 189 | delete solveHistory->at(i); |
| 190 | }} |
| 191 | solveHistory->clear(); |
| 192 | solveInstructions->clear(); |
| 193 | |
| 194 | int round = 1; |
| 195 | for (int position=0; position<BOARD_SIZE; position++){ |
| 196 | if (puzzle[position] > 0){ |
| 197 | int valIndex = puzzle[position]-1; |
| 198 | int valPos = getPossibilityIndex(valIndex,position); |
| 199 | int value = puzzle[position]; |
| 200 | if (possibilities[valPos] != 0) return false; |
| 201 | mark(position,round,value); |
| 202 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::GIVEN, value, position)); |
| 203 | } |
| 204 | } |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Get the difficulty rating. |
| 210 | */ |
| 211 | SudokuBoard::Difficulty SudokuBoard::getDifficulty(){ |
| 212 | if (getGuessCount() > 0) return SudokuBoard::EXPERT; |
| 213 | if (getBoxLineReductionCount() > 0) return SudokuBoard::INTERMEDIATE; |
| 214 | if (getPointingPairTripleCount() > 0) return SudokuBoard::INTERMEDIATE; |
| 215 | if (getHiddenPairCount() > 0) return SudokuBoard::INTERMEDIATE; |
| 216 | if (getNakedPairCount() > 0) return SudokuBoard::INTERMEDIATE; |
| 217 | if (getHiddenSingleCount() > 0) return SudokuBoard::EASY; |
| 218 | if (getSingleCount() > 0) return SudokuBoard::SIMPLE; |
| 219 | return SudokuBoard::UNKNOWN; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Get the difficulty rating. |
| 224 | */ |
| 225 | string SudokuBoard::getDifficultyAsString(){ |
| 226 | SudokuBoard::Difficulty difficulty = getDifficulty(); |
| 227 | switch (difficulty){ |
| 228 | case SudokuBoard::EXPERT: return "Expert"; |
| 229 | case SudokuBoard::INTERMEDIATE: return "Intermediate"; |
| 230 | case SudokuBoard::EASY: return "Easy"; |
| 231 | case SudokuBoard::SIMPLE: return "Simple"; |
| 232 | default: return "Unknown"; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Get the number of cells for which the solution was determined |
| 238 | * because there was only one possible value for that cell. |
| 239 | */ |
| 240 | int SudokuBoard::getSingleCount(){ |
| 241 | return getLogCount(solveInstructions, LogItem::SINGLE); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Get the number of cells for which the solution was determined |
| 246 | * because that cell had the only possibility for some value in |
| 247 | * the row, column, or section. |
| 248 | */ |
| 249 | int SudokuBoard::getHiddenSingleCount(){ |
| 250 | return getLogCount(solveInstructions, LogItem::HIDDEN_SINGLE_ROW) + |
| 251 | getLogCount(solveInstructions, LogItem::HIDDEN_SINGLE_COLUMN) + |
| 252 | getLogCount(solveInstructions, LogItem::HIDDEN_SINGLE_SECTION); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Get the number of naked pair reductions that were performed |
| 257 | * in solving this puzzle. |
| 258 | */ |
| 259 | int SudokuBoard::getNakedPairCount(){ |
| 260 | return getLogCount(solveInstructions, LogItem::NAKED_PAIR_ROW) + |
| 261 | getLogCount(solveInstructions, LogItem::NAKED_PAIR_COLUMN) + |
| 262 | getLogCount(solveInstructions, LogItem::NAKED_PAIR_SECTION); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Get the number of hidden pair reductions that were performed |
| 267 | * in solving this puzzle. |
| 268 | */ |
| 269 | int SudokuBoard::getHiddenPairCount(){ |
| 270 | return getLogCount(solveInstructions, LogItem::HIDDEN_PAIR_ROW) + |
| 271 | getLogCount(solveInstructions, LogItem::HIDDEN_PAIR_COLUMN) + |
| 272 | getLogCount(solveInstructions, LogItem::HIDDEN_PAIR_SECTION); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Get the number of pointing pair/triple reductions that were performed |
| 277 | * in solving this puzzle. |
| 278 | */ |
| 279 | int SudokuBoard::getPointingPairTripleCount(){ |
| 280 | return getLogCount(solveInstructions, LogItem::POINTING_PAIR_TRIPLE_ROW)+ |
| 281 | getLogCount(solveInstructions, LogItem::POINTING_PAIR_TRIPLE_COLUMN); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Get the number of box/line reductions that were performed |
| 286 | * in solving this puzzle. |
| 287 | */ |
| 288 | int SudokuBoard::getBoxLineReductionCount(){ |
| 289 | return getLogCount(solveInstructions, LogItem::ROW_BOX)+ |
| 290 | getLogCount(solveInstructions, LogItem::COLUMN_BOX); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Get the number lucky guesses in solving this puzzle. |
| 295 | */ |
| 296 | int SudokuBoard::getGuessCount(){ |
| 297 | return getLogCount(solveInstructions, LogItem::GUESS); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Get the number of backtracks (unlucky guesses) required |
| 302 | * when solving this puzzle. |
| 303 | */ |
| 304 | int SudokuBoard::getBacktrackCount(){ |
| 305 | return getLogCount(solveHistory, LogItem::ROLLBACK); |
| 306 | } |
| 307 | |
| 308 | void SudokuBoard::shuffleRandomArrays(){ |
| 309 | shuffleArray(randomBoardArray, BOARD_SIZE); |
| 310 | shuffleArray(randomPossibilityArray, ROW_COL_SEC_SIZE); |
| 311 | } |
| 312 | |
| 313 | void SudokuBoard::clearPuzzle(){ |
| 314 | // Clear any existing puzzle |
| 315 | {for (int i=0; i<BOARD_SIZE; i++){ |
| 316 | puzzle[i] = 0; |
| 317 | }} |
| 318 | reset(); |
| 319 | } |
| 320 | |
| 321 | bool SudokuBoard::generatePuzzle(){ |
| 322 | return generatePuzzleSymmetry(SudokuBoard::NONE); |
| 323 | } |
| 324 | |
| 325 | bool SudokuBoard::generatePuzzleSymmetry(SudokuBoard::Symmetry symmetry){ |
| 326 | |
| 327 | if (symmetry == SudokuBoard::RANDOM) symmetry = getRandomSymmetry(); |
| 328 | |
| 329 | // Don't record history while generating. |
| 330 | bool recHistory = recordHistory; |
| 331 | setRecordHistory(false); |
| 332 | bool lHistory = logHistory; |
| 333 | setLogHistory(false); |
| 334 | |
| 335 | clearPuzzle(); |
| 336 | |
| 337 | // Start by getting the randomness in order so that |
| 338 | // each puzzle will be different from the last. |
| 339 | shuffleRandomArrays(); |
| 340 | |
| 341 | // Now solve the puzzle the whole way. The solve |
| 342 | // uses random algorithms, so we should have a |
| 343 | // really randomly totally filled sudoku |
| 344 | // Even when starting from an empty grid |
| 345 | solve(); |
| 346 | |
| 347 | if (symmetry == SudokuBoard::NONE){ |
| 348 | // Rollback any square for which it is obvious that |
| 349 | // the square doesn't contribute to a unique solution |
| 350 | // (ie, squares that were filled by logic rather |
| 351 | // than by guess) |
| 352 | rollbackNonGuesses(); |
| 353 | } |
| 354 | |
| 355 | // Record all marked squares as the puzzle so |
| 356 | // that we can call countSolutions without losing it. |
| 357 | {for (int i=0; i<BOARD_SIZE; i++){ |
| 358 | puzzle[i] = solution[i]; |
| 359 | }} |
| 360 | |
| 361 | // Rerandomize everything so that we test squares |
| 362 | // in a different order than they were added. |
| 363 | shuffleRandomArrays(); |
| 364 | |
| 365 | // Remove one value at a time and see if |
| 366 | // the puzzle still has only one solution. |
| 367 | // If it does, leave it out the point because |
| 368 | // it is not needed. |
| 369 | {for (int i=0; i<BOARD_SIZE; i++){ |
| 370 | // check all the positions, but in shuffled order |
| 371 | int position = randomBoardArray[i]; |
| 372 | if (puzzle[position] > 0){ |
| 373 | int positionsym1 = -1; |
| 374 | int positionsym2 = -1; |
| 375 | int positionsym3 = -1; |
| 376 | switch (symmetry){ |
| 377 | case ROTATE90: |
| 378 | positionsym2 = rowColumnToCell(ROW_COL_SEC_SIZE-1-cellToColumn(position),cellToRow(position)); |
| 379 | positionsym3 = rowColumnToCell(cellToColumn(position),ROW_COL_SEC_SIZE-1-cellToRow(position)); |
| 380 | case ROTATE180: |
| 381 | positionsym1 = rowColumnToCell(ROW_COL_SEC_SIZE-1-cellToRow(position),ROW_COL_SEC_SIZE-1-cellToColumn(position)); |
| 382 | break; |
| 383 | case MIRROR: |
| 384 | positionsym1 = rowColumnToCell(cellToRow(position),ROW_COL_SEC_SIZE-1-cellToColumn(position)); |
| 385 | break; |
| 386 | case FLIP: |
| 387 | positionsym1 = rowColumnToCell(ROW_COL_SEC_SIZE-1-cellToRow(position),cellToColumn(position)); |
| 388 | break; |
| 389 | case RANDOM: // NOTE: Should never happen |
| 390 | break; |
| 391 | case NONE: // NOTE: No need to do anything |
| 392 | break; |
| 393 | } |
| 394 | // try backing out the value and |
| 395 | // counting solutions to the puzzle |
| 396 | int savedValue = puzzle[position]; |
| 397 | puzzle[position] = 0; |
| 398 | int savedSym1 = 0; |
| 399 | if (positionsym1 >= 0){ |
| 400 | savedSym1 = puzzle[positionsym1]; |
| 401 | puzzle[positionsym1] = 0; |
| 402 | } |
| 403 | int savedSym2 = 0; |
| 404 | if (positionsym2 >= 0){ |
| 405 | savedSym2 = puzzle[positionsym2]; |
| 406 | puzzle[positionsym2] = 0; |
| 407 | } |
| 408 | int savedSym3 = 0; |
| 409 | if (positionsym3 >= 0){ |
| 410 | savedSym3 = puzzle[positionsym3]; |
| 411 | puzzle[positionsym3] = 0; |
| 412 | } |
| 413 | reset(); |
| 414 | if (countSolutions(2, true) > 1){ |
| 415 | // Put it back in, it is needed |
| 416 | puzzle[position] = savedValue; |
| 417 | if (positionsym1 >= 0 && savedSym1 != 0) puzzle[positionsym1] = savedSym1; |
| 418 | if (positionsym2 >= 0 && savedSym2 != 0) puzzle[positionsym2] = savedSym2; |
| 419 | if (positionsym3 >= 0 && savedSym3 != 0) puzzle[positionsym3] = savedSym3; |
| 420 | } |
| 421 | } |
| 422 | }} |
| 423 | |
| 424 | // Clear all solution info, leaving just the puzzle. |
| 425 | reset(); |
| 426 | |
| 427 | // Restore recording history. |
| 428 | setRecordHistory(recHistory); |
| 429 | setLogHistory(lHistory); |
| 430 | |
| 431 | return true; |
| 432 | |
| 433 | } |
| 434 | |
| 435 | void SudokuBoard::rollbackNonGuesses(){ |
| 436 | // Guesses are odd rounds |
| 437 | // Non-guesses are even rounds |
| 438 | {for (int i=2; i<=lastSolveRound; i+=2){ |
| 439 | rollbackRound(i); |
| 440 | }} |
| 441 | } |
| 442 | |
| 443 | void SudokuBoard::setPrintStyle(PrintStyle ps){ |
| 444 | printStyle = ps; |
| 445 | } |
| 446 | |
| 447 | void SudokuBoard::setRecordHistory(bool recHistory){ |
| 448 | recordHistory = recHistory; |
| 449 | } |
| 450 | |
| 451 | void SudokuBoard::setLogHistory(bool logHist){ |
| 452 | logHistory = logHist; |
| 453 | } |
| 454 | |
| 455 | void SudokuBoard::addHistoryItem(LogItem* l){ |
| 456 | if (logHistory){ |
| 457 | l->print(); |
| 458 | cout << endl; |
| 459 | } |
| 460 | if (recordHistory){ |
| 461 | solveHistory->push_back(l); |
| 462 | solveInstructions->push_back(l); |
| 463 | } else { |
| 464 | delete l; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | void SudokuBoard::printHistory(vector<LogItem*>* v){ |
| 469 | if (!recordHistory){ |
| 470 | cout << "History was not recorded."; |
| 471 | if (printStyle == CSV){ |
| 472 | cout << " -- "; |
| 473 | } else { |
| 474 | cout << endl; |
| 475 | } |
| 476 | } |
| 477 | {for (unsigned int i=0;i<v->size();i++){ |
| 478 | cout << i+1 << ". "; |
| 479 | v->at(i)->print(); |
| 480 | if (printStyle == CSV){ |
| 481 | cout << " -- "; |
| 482 | } else { |
| 483 | cout << endl; |
| 484 | } |
| 485 | }} |
| 486 | if (printStyle == CSV){ |
| 487 | cout << ","; |
| 488 | } else { |
| 489 | cout << endl; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | void SudokuBoard::printSolveInstructions(){ |
| 494 | if (isSolved()){ |
| 495 | printHistory(solveInstructions); |
| 496 | } else { |
| 497 | cout << "No solve instructions - Puzzle is not possible to solve." << endl; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | void SudokuBoard::printSolveHistory(){ |
| 502 | printHistory(solveHistory); |
| 503 | } |
| 504 | |
| 505 | bool SudokuBoard::solve(){ |
| 506 | reset(); |
| 507 | shuffleRandomArrays(); |
| 508 | return solve(2); |
| 509 | } |
| 510 | |
| 511 | bool SudokuBoard::solve(int round){ |
| 512 | lastSolveRound = round; |
| 513 | |
| 514 | while (singleSolveMove(round)){ |
| 515 | if (isSolved()) return true; |
| 516 | if (isImpossible()) return false; |
| 517 | } |
| 518 | |
| 519 | int nextGuessRound = round+1; |
| 520 | int nextRound = round+2; |
| 521 | for (int guessNumber=0; guess(nextGuessRound, guessNumber); guessNumber++){ |
| 522 | if (isImpossible() || !solve(nextRound)){ |
| 523 | rollbackRound(nextRound); |
| 524 | rollbackRound(nextGuessRound); |
| 525 | } else { |
| 526 | return true; |
| 527 | } |
| 528 | } |
| 529 | return false; |
| 530 | } |
| 531 | |
| 532 | bool SudokuBoard::hasUniqueSolution(){ |
| 533 | return countSolutionsLimited() == 1; |
| 534 | } |
| 535 | |
| 536 | int SudokuBoard::countSolutions(){ |
| 537 | return countSolutions(false); |
| 538 | } |
| 539 | |
| 540 | int SudokuBoard::countSolutionsLimited(){ |
| 541 | return countSolutions(true); |
| 542 | } |
| 543 | |
| 544 | int SudokuBoard::countSolutions(bool limitToTwo){ |
| 545 | // Don't record history while generating. |
| 546 | bool recHistory = recordHistory; |
| 547 | setRecordHistory(false); |
| 548 | bool lHistory = logHistory; |
| 549 | setLogHistory(false); |
| 550 | |
| 551 | reset(); |
| 552 | int solutionCount = countSolutions(2, limitToTwo); |
| 553 | |
| 554 | // Restore recording history. |
| 555 | setRecordHistory(recHistory); |
| 556 | setLogHistory(lHistory); |
| 557 | |
| 558 | return solutionCount; |
| 559 | } |
| 560 | |
| 561 | int SudokuBoard::countSolutions(int round, bool limitToTwo){ |
| 562 | while (singleSolveMove(round)){ |
| 563 | if (isSolved()){ |
| 564 | rollbackRound(round); |
| 565 | return 1; |
| 566 | } |
| 567 | if (isImpossible()){ |
| 568 | rollbackRound(round); |
| 569 | return 0; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | int solutions = 0; |
| 574 | int nextRound = round+1; |
| 575 | for (int guessNumber=0; guess(nextRound, guessNumber); guessNumber++){ |
| 576 | solutions += countSolutions(nextRound, limitToTwo); |
| 577 | if (limitToTwo && solutions >=2){ |
| 578 | rollbackRound(round); |
| 579 | return solutions; |
| 580 | } |
| 581 | } |
| 582 | rollbackRound(round); |
| 583 | return solutions; |
| 584 | } |
| 585 | |
| 586 | void SudokuBoard::rollbackRound(int round){ |
| 587 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::ROLLBACK)); |
| 588 | {for (int i=0; i<BOARD_SIZE; i++){ |
| 589 | if (solutionRound[i] == round){ |
| 590 | solutionRound[i] = 0; |
| 591 | solution[i] = 0; |
| 592 | } |
| 593 | }} |
| 594 | {for (int i=0; i<POSSIBILITY_SIZE; i++){ |
| 595 | if (possibilities[i] == round){ |
| 596 | possibilities[i] = 0; |
| 597 | } |
| 598 | }} |
| 599 | |
| 600 | while(solveInstructions->size() > 0 && solveInstructions->back()->getRound() == round){ |
| 601 | solveInstructions->pop_back(); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | bool SudokuBoard::isSolved(){ |
| 606 | {for (int i=0; i<BOARD_SIZE; i++){ |
| 607 | if (solution[i] == 0){ |
| 608 | return false; |
| 609 | } |
| 610 | }} |
| 611 | return true; |
| 612 | } |
| 613 | |
| 614 | bool SudokuBoard::isImpossible(){ |
| 615 | for (int position=0; position<BOARD_SIZE; position++){ |
| 616 | if (solution[position] == 0){ |
| 617 | int count = 0; |
| 618 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 619 | int valPos = getPossibilityIndex(valIndex,position); |
| 620 | if (possibilities[valPos] == 0) count++; |
| 621 | } |
| 622 | if (count == 0) { |
| 623 | return true; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | return false; |
| 628 | } |
| 629 | |
| 630 | int SudokuBoard::findPositionWithFewestPossibilities(){ |
| 631 | int minPossibilities = 10; |
| 632 | int bestPosition = 0; |
| 633 | {for (int i=0; i<BOARD_SIZE; i++){ |
| 634 | int position = randomBoardArray[i]; |
| 635 | if (solution[position] == 0){ |
| 636 | int count = 0; |
| 637 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 638 | int valPos = getPossibilityIndex(valIndex,position); |
| 639 | if (possibilities[valPos] == 0) count++; |
| 640 | } |
| 641 | if (count < minPossibilities){ |
| 642 | minPossibilities = count; |
| 643 | bestPosition = position; |
| 644 | } |
| 645 | } |
| 646 | }} |
| 647 | return bestPosition; |
| 648 | } |
| 649 | |
| 650 | bool SudokuBoard::guess(int round, int guessNumber){ |
| 651 | int localGuessCount = 0; |
| 652 | int position = findPositionWithFewestPossibilities(); |
| 653 | {for (int i=0; i<ROW_COL_SEC_SIZE; i++){ |
| 654 | int valIndex = randomPossibilityArray[i]; |
| 655 | int valPos = getPossibilityIndex(valIndex,position); |
| 656 | if (possibilities[valPos] == 0){ |
| 657 | if (localGuessCount == guessNumber){ |
| 658 | int value = valIndex+1; |
| 659 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::GUESS, value, position)); |
| 660 | mark(position, round, value); |
| 661 | return true; |
| 662 | } |
| 663 | localGuessCount++; |
| 664 | } |
| 665 | }} |
| 666 | return false; |
| 667 | } |
| 668 | |
| 669 | bool SudokuBoard::singleSolveMove(int round){ |
| 670 | if (onlyPossibilityForCell(round)) return true; |
| 671 | if (onlyValueInSection(round)) return true; |
| 672 | if (onlyValueInRow(round)) return true; |
| 673 | if (onlyValueInColumn(round)) return true; |
| 674 | if (handleNakedPairs(round)) return true; |
| 675 | if (pointingRowReduction(round)) return true; |
| 676 | if (pointingColumnReduction(round)) return true; |
| 677 | if (rowBoxReduction(round)) return true; |
| 678 | if (colBoxReduction(round)) return true; |
| 679 | if (hiddenPairInRow(round)) return true; |
| 680 | if (hiddenPairInColumn(round)) return true; |
| 681 | if (hiddenPairInSection(round)) return true; |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | bool SudokuBoard::colBoxReduction(int round){ |
| 686 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 687 | for (int col=0; col<ROW_COL_SEC_SIZE; col++){ |
| 688 | int colStart = columnToFirstCell(col); |
| 689 | bool inOneBox = true; |
| 690 | int colBox = -1; |
| 691 | {for (int i=0; i<GRID_SIZE; i++){ |
| 692 | for (int j=0; j<GRID_SIZE; j++){ |
| 693 | int row = i*GRID_SIZE+j; |
| 694 | int position = rowColumnToCell(row, col); |
| 695 | int valPos = getPossibilityIndex(valIndex,position); |
| 696 | if(possibilities[valPos] == 0){ |
| 697 | if (colBox == -1 || colBox == i){ |
| 698 | colBox = i; |
| 699 | } else { |
| 700 | inOneBox = false; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | } |
| 705 | }} |
| 706 | if (inOneBox && colBox != -1){ |
| 707 | bool doneSomething = false; |
| 708 | int row = GRID_SIZE*colBox; |
| 709 | int secStart = cellToSectionStartCell(rowColumnToCell(row, col)); |
| 710 | int secStartRow = cellToRow(secStart); |
| 711 | int secStartCol = cellToColumn(secStart); |
| 712 | {for (int i=0; i<GRID_SIZE; i++){ |
| 713 | for (int j=0; j<GRID_SIZE; j++){ |
| 714 | int row2 = secStartRow+i; |
| 715 | int col2 = secStartCol+j; |
| 716 | int position = rowColumnToCell(row2, col2); |
| 717 | int valPos = getPossibilityIndex(valIndex,position); |
| 718 | if (col != col2 && possibilities[valPos] == 0){ |
| 719 | possibilities[valPos] = round; |
| 720 | doneSomething = true; |
| 721 | } |
| 722 | } |
| 723 | }} |
| 724 | if (doneSomething){ |
| 725 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::COLUMN_BOX, valIndex+1, colStart)); |
| 726 | return true; |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | return false; |
| 732 | } |
| 733 | |
| 734 | bool SudokuBoard::rowBoxReduction(int round){ |
| 735 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 736 | for (int row=0; row<ROW_COL_SEC_SIZE; row++){ |
| 737 | int rowStart = rowToFirstCell(row); |
| 738 | bool inOneBox = true; |
| 739 | int rowBox = -1; |
| 740 | {for (int i=0; i<GRID_SIZE; i++){ |
| 741 | for (int j=0; j<GRID_SIZE; j++){ |
| 742 | int column = i*GRID_SIZE+j; |
| 743 | int position = rowColumnToCell(row, column); |
| 744 | int valPos = getPossibilityIndex(valIndex,position); |
| 745 | if(possibilities[valPos] == 0){ |
| 746 | if (rowBox == -1 || rowBox == i){ |
| 747 | rowBox = i; |
| 748 | } else { |
| 749 | inOneBox = false; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | } |
| 754 | }} |
| 755 | if (inOneBox && rowBox != -1){ |
| 756 | bool doneSomething = false; |
| 757 | int column = GRID_SIZE*rowBox; |
| 758 | int secStart = cellToSectionStartCell(rowColumnToCell(row, column)); |
| 759 | int secStartRow = cellToRow(secStart); |
| 760 | int secStartCol = cellToColumn(secStart); |
| 761 | {for (int i=0; i<GRID_SIZE; i++){ |
| 762 | for (int j=0; j<GRID_SIZE; j++){ |
| 763 | int row2 = secStartRow+i; |
| 764 | int col2 = secStartCol+j; |
| 765 | int position = rowColumnToCell(row2, col2); |
| 766 | int valPos = getPossibilityIndex(valIndex,position); |
| 767 | if (row != row2 && possibilities[valPos] == 0){ |
| 768 | possibilities[valPos] = round; |
| 769 | doneSomething = true; |
| 770 | } |
| 771 | } |
| 772 | }} |
| 773 | if (doneSomething){ |
| 774 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::ROW_BOX, valIndex+1, rowStart)); |
| 775 | return true; |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | return false; |
| 781 | } |
| 782 | |
| 783 | bool SudokuBoard::pointingRowReduction(int round){ |
| 784 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 785 | for (int section=0; section<ROW_COL_SEC_SIZE; section++){ |
| 786 | int secStart = sectionToFirstCell(section); |
| 787 | bool inOneRow = true; |
| 788 | int boxRow = -1; |
| 789 | for (int j=0; j<GRID_SIZE; j++){ |
| 790 | {for (int i=0; i<GRID_SIZE; i++){ |
| 791 | int secVal=secStart+i+(ROW_COL_SEC_SIZE*j); |
| 792 | int valPos = getPossibilityIndex(valIndex,secVal); |
| 793 | if(possibilities[valPos] == 0){ |
| 794 | if (boxRow == -1 || boxRow == j){ |
| 795 | boxRow = j; |
| 796 | } else { |
| 797 | inOneRow = false; |
| 798 | } |
| 799 | } |
| 800 | }} |
| 801 | } |
| 802 | if (inOneRow && boxRow != -1){ |
| 803 | bool doneSomething = false; |
| 804 | int row = cellToRow(secStart) + boxRow; |
| 805 | int rowStart = rowToFirstCell(row); |
| 806 | |
| 807 | {for (int i=0; i<ROW_COL_SEC_SIZE; i++){ |
| 808 | int position = rowStart+i; |
| 809 | int section2 = cellToSection(position); |
| 810 | int valPos = getPossibilityIndex(valIndex,position); |
| 811 | if (section != section2 && possibilities[valPos] == 0){ |
| 812 | possibilities[valPos] = round; |
| 813 | doneSomething = true; |
| 814 | } |
| 815 | }} |
| 816 | if (doneSomething){ |
| 817 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::POINTING_PAIR_TRIPLE_ROW, valIndex+1, rowStart)); |
| 818 | return true; |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | return false; |
| 824 | } |
| 825 | |
| 826 | bool SudokuBoard::pointingColumnReduction(int round){ |
| 827 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 828 | for (int section=0; section<ROW_COL_SEC_SIZE; section++){ |
| 829 | int secStart = sectionToFirstCell(section); |
| 830 | bool inOneCol = true; |
| 831 | int boxCol = -1; |
| 832 | {for (int i=0; i<GRID_SIZE; i++){ |
| 833 | for (int j=0; j<GRID_SIZE; j++){ |
| 834 | int secVal=secStart+i+(ROW_COL_SEC_SIZE*j); |
| 835 | int valPos = getPossibilityIndex(valIndex,secVal); |
| 836 | if(possibilities[valPos] == 0){ |
| 837 | if (boxCol == -1 || boxCol == i){ |
| 838 | boxCol = i; |
| 839 | } else { |
| 840 | inOneCol = false; |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | }} |
| 845 | if (inOneCol && boxCol != -1){ |
| 846 | bool doneSomething = false; |
| 847 | int col = cellToColumn(secStart) + boxCol; |
| 848 | int colStart = columnToFirstCell(col); |
| 849 | |
| 850 | {for (int i=0; i<ROW_COL_SEC_SIZE; i++){ |
| 851 | int position = colStart+(ROW_COL_SEC_SIZE*i); |
| 852 | int section2 = cellToSection(position); |
| 853 | int valPos = getPossibilityIndex(valIndex,position); |
| 854 | if (section != section2 && possibilities[valPos] == 0){ |
| 855 | possibilities[valPos] = round; |
| 856 | doneSomething = true; |
| 857 | } |
| 858 | }} |
| 859 | if (doneSomething){ |
| 860 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::POINTING_PAIR_TRIPLE_COLUMN, valIndex+1, colStart)); |
| 861 | return true; |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | return false; |
| 867 | } |
| 868 | |
| 869 | int SudokuBoard::countPossibilities(int position){ |
| 870 | int count = 0; |
| 871 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 872 | int valPos = getPossibilityIndex(valIndex,position); |
| 873 | if (possibilities[valPos] == 0) count++; |
| 874 | } |
| 875 | return count; |
| 876 | } |
| 877 | |
| 878 | bool SudokuBoard::arePossibilitiesSame(int position1, int position2){ |
| 879 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 880 | int valPos1 = getPossibilityIndex(valIndex,position1); |
| 881 | int valPos2 = getPossibilityIndex(valIndex,position2); |
| 882 | if ((possibilities[valPos1] == 0 || possibilities[valPos2] == 0) && (possibilities[valPos1] != 0 || possibilities[valPos2] != 0)){ |
| 883 | return false; |
| 884 | } |
| 885 | } |
| 886 | return true; |
| 887 | } |
| 888 | |
| 889 | bool SudokuBoard::removePossibilitiesInOneFromTwo(int position1, int position2, int round){ |
| 890 | bool doneSomething = false; |
| 891 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 892 | int valPos1 = getPossibilityIndex(valIndex,position1); |
| 893 | int valPos2 = getPossibilityIndex(valIndex,position2); |
| 894 | if (possibilities[valPos1] == 0 && possibilities[valPos2] == 0){ |
| 895 | possibilities[valPos2] = round; |
| 896 | doneSomething = true; |
| 897 | } |
| 898 | } |
| 899 | return doneSomething; |
| 900 | } |
| 901 | |
| 902 | bool SudokuBoard::hiddenPairInColumn(int round){ |
| 903 | for (int column=0; column<ROW_COL_SEC_SIZE; column++){ |
| 904 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 905 | int r1 = -1; |
| 906 | int r2 = -1; |
| 907 | int valCount = 0; |
| 908 | for (int row=0; row<ROW_COL_SEC_SIZE; row++){ |
| 909 | int position = rowColumnToCell(row,column); |
| 910 | int valPos = getPossibilityIndex(valIndex,position); |
| 911 | if (possibilities[valPos] == 0){ |
| 912 | if (r1 == -1 || r1 == row){ |
| 913 | r1 = row; |
| 914 | } else if (r2 == -1 || r2 == row){ |
| 915 | r2 = row; |
| 916 | } |
| 917 | valCount++; |
| 918 | } |
| 919 | } |
| 920 | if (valCount==2){ |
| 921 | for (int valIndex2=valIndex+1; valIndex2<ROW_COL_SEC_SIZE; valIndex2++){ |
| 922 | int r3 = -1; |
| 923 | int r4 = -1; |
| 924 | int valCount2 = 0; |
| 925 | for (int row=0; row<ROW_COL_SEC_SIZE; row++){ |
| 926 | int position = rowColumnToCell(row,column); |
| 927 | int valPos = getPossibilityIndex(valIndex2,position); |
| 928 | if (possibilities[valPos] == 0){ |
| 929 | if (r3 == -1 || r3 == row){ |
| 930 | r3 = row; |
| 931 | } else if (r4 == -1 || r4 == row){ |
| 932 | r4 = row; |
| 933 | } |
| 934 | valCount2++; |
| 935 | } |
| 936 | } |
| 937 | if (valCount2==2 && r1==r3 && r2==r4){ |
| 938 | bool doneSomething = false; |
| 939 | for (int valIndex3=0; valIndex3<ROW_COL_SEC_SIZE; valIndex3++){ |
| 940 | if (valIndex3 != valIndex && valIndex3 != valIndex2){ |
| 941 | int position1 = rowColumnToCell(r1,column); |
| 942 | int position2 = rowColumnToCell(r2,column); |
| 943 | int valPos1 = getPossibilityIndex(valIndex3,position1); |
| 944 | int valPos2 = getPossibilityIndex(valIndex3,position2); |
| 945 | if (possibilities[valPos1] == 0){ |
| 946 | possibilities[valPos1] = round; |
| 947 | doneSomething = true; |
| 948 | } |
| 949 | if (possibilities[valPos2] == 0){ |
| 950 | possibilities[valPos2] = round; |
| 951 | doneSomething = true; |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | if (doneSomething){ |
| 956 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::HIDDEN_PAIR_COLUMN, valIndex+1, rowColumnToCell(r1,column))); |
| 957 | return true; |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | return false; |
| 965 | } |
| 966 | |
| 967 | bool SudokuBoard::hiddenPairInSection(int round){ |
| 968 | for (int section=0; section<ROW_COL_SEC_SIZE; section++){ |
| 969 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 970 | int si1 = -1; |
| 971 | int si2 = -1; |
| 972 | int valCount = 0; |
| 973 | for (int secInd=0; secInd<ROW_COL_SEC_SIZE; secInd++){ |
| 974 | int position = sectionToCell(section,secInd); |
| 975 | int valPos = getPossibilityIndex(valIndex,position); |
| 976 | if (possibilities[valPos] == 0){ |
| 977 | if (si1 == -1 || si1 == secInd){ |
| 978 | si1 = secInd; |
| 979 | } else if (si2 == -1 || si2 == secInd){ |
| 980 | si2 = secInd; |
| 981 | } |
| 982 | valCount++; |
| 983 | } |
| 984 | } |
| 985 | if (valCount==2){ |
| 986 | for (int valIndex2=valIndex+1; valIndex2<ROW_COL_SEC_SIZE; valIndex2++){ |
| 987 | int si3 = -1; |
| 988 | int si4 = -1; |
| 989 | int valCount2 = 0; |
| 990 | for (int secInd=0; secInd<ROW_COL_SEC_SIZE; secInd++){ |
| 991 | int position = sectionToCell(section,secInd); |
| 992 | int valPos = getPossibilityIndex(valIndex2,position); |
| 993 | if (possibilities[valPos] == 0){ |
| 994 | if (si3 == -1 || si3 == secInd){ |
| 995 | si3 = secInd; |
| 996 | } else if (si4 == -1 || si4 == secInd){ |
| 997 | si4 = secInd; |
| 998 | } |
| 999 | valCount2++; |
| 1000 | } |
| 1001 | } |
| 1002 | if (valCount2==2 && si1==si3 && si2==si4){ |
| 1003 | bool doneSomething = false; |
| 1004 | for (int valIndex3=0; valIndex3<ROW_COL_SEC_SIZE; valIndex3++){ |
| 1005 | if (valIndex3 != valIndex && valIndex3 != valIndex2){ |
| 1006 | int position1 = sectionToCell(section,si1); |
| 1007 | int position2 = sectionToCell(section,si2); |
| 1008 | int valPos1 = getPossibilityIndex(valIndex3,position1); |
| 1009 | int valPos2 = getPossibilityIndex(valIndex3,position2); |
| 1010 | if (possibilities[valPos1] == 0){ |
| 1011 | possibilities[valPos1] = round; |
| 1012 | doneSomething = true; |
| 1013 | } |
| 1014 | if (possibilities[valPos2] == 0){ |
| 1015 | possibilities[valPos2] = round; |
| 1016 | doneSomething = true; |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | if (doneSomething){ |
| 1021 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::HIDDEN_PAIR_SECTION, valIndex+1, sectionToCell(section,si1))); |
| 1022 | return true; |
| 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | return false; |
| 1030 | } |
| 1031 | |
| 1032 | bool SudokuBoard::hiddenPairInRow(int round){ |
| 1033 | for (int row=0; row<ROW_COL_SEC_SIZE; row++){ |
| 1034 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 1035 | int c1 = -1; |
| 1036 | int c2 = -1; |
| 1037 | int valCount = 0; |
| 1038 | for (int column=0; column<ROW_COL_SEC_SIZE; column++){ |
| 1039 | int position = rowColumnToCell(row,column); |
| 1040 | int valPos = getPossibilityIndex(valIndex,position); |
| 1041 | if (possibilities[valPos] == 0){ |
| 1042 | if (c1 == -1 || c1 == column){ |
| 1043 | c1 = column; |
| 1044 | } else if (c2 == -1 || c2 == column){ |
| 1045 | c2 = column; |
| 1046 | } |
| 1047 | valCount++; |
| 1048 | } |
| 1049 | } |
| 1050 | if (valCount==2){ |
| 1051 | for (int valIndex2=valIndex+1; valIndex2<ROW_COL_SEC_SIZE; valIndex2++){ |
| 1052 | int c3 = -1; |
| 1053 | int c4 = -1; |
| 1054 | int valCount2 = 0; |
| 1055 | for (int column=0; column<ROW_COL_SEC_SIZE; column++){ |
| 1056 | int position = rowColumnToCell(row,column); |
| 1057 | int valPos = getPossibilityIndex(valIndex2,position); |
| 1058 | if (possibilities[valPos] == 0){ |
| 1059 | if (c3 == -1 || c3 == column){ |
| 1060 | c3 = column; |
| 1061 | } else if (c4 == -1 || c4 == column){ |
| 1062 | c4 = column; |
| 1063 | } |
| 1064 | valCount2++; |
| 1065 | } |
| 1066 | } |
| 1067 | if (valCount2==2 && c1==c3 && c2==c4){ |
| 1068 | bool doneSomething = false; |
| 1069 | for (int valIndex3=0; valIndex3<ROW_COL_SEC_SIZE; valIndex3++){ |
| 1070 | if (valIndex3 != valIndex && valIndex3 != valIndex2){ |
| 1071 | int position1 = rowColumnToCell(row,c1); |
| 1072 | int position2 = rowColumnToCell(row,c2); |
| 1073 | int valPos1 = getPossibilityIndex(valIndex3,position1); |
| 1074 | int valPos2 = getPossibilityIndex(valIndex3,position2); |
| 1075 | if (possibilities[valPos1] == 0){ |
| 1076 | possibilities[valPos1] = round; |
| 1077 | doneSomething = true; |
| 1078 | } |
| 1079 | if (possibilities[valPos2] == 0){ |
| 1080 | possibilities[valPos2] = round; |
| 1081 | doneSomething = true; |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | if (doneSomething){ |
| 1086 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::HIDDEN_PAIR_ROW, valIndex+1, rowColumnToCell(row,c1))); |
| 1087 | return true; |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | return false; |
| 1095 | } |
| 1096 | |
| 1097 | bool SudokuBoard::handleNakedPairs(int round){ |
| 1098 | for (int position=0; position<BOARD_SIZE; position++){ |
| 1099 | int possibilities = countPossibilities(position); |
| 1100 | if (possibilities == 2){ |
| 1101 | int row = cellToRow(position); |
| 1102 | int column = cellToColumn(position); |
| 1103 | int section = cellToSectionStartCell(position); |
| 1104 | for (int position2=position; position2<BOARD_SIZE; position2++){ |
| 1105 | if (position != position2){ |
| 1106 | int possibilities2 = countPossibilities(position2); |
| 1107 | if (possibilities2 == 2 && arePossibilitiesSame(position, position2)){ |
| 1108 | if (row == cellToRow(position2)){ |
| 1109 | bool doneSomething = false; |
| 1110 | for (int column2=0; column2<ROW_COL_SEC_SIZE; column2++){ |
| 1111 | int position3 = rowColumnToCell(row,column2); |
| 1112 | if (position3 != position && position3 != position2 && removePossibilitiesInOneFromTwo(position, position3, round)){ |
| 1113 | doneSomething = true; |
| 1114 | } |
| 1115 | } |
| 1116 | if (doneSomething){ |
| 1117 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::NAKED_PAIR_ROW, 0, position)); |
| 1118 | return true; |
| 1119 | } |
| 1120 | } |
| 1121 | if (column == cellToColumn(position2)){ |
| 1122 | bool doneSomething = false; |
| 1123 | for (int row2=0; row2<ROW_COL_SEC_SIZE; row2++){ |
| 1124 | int position3 = rowColumnToCell(row2,column); |
| 1125 | if (position3 != position && position3 != position2 && removePossibilitiesInOneFromTwo(position, position3, round)){ |
| 1126 | doneSomething = true; |
| 1127 | } |
| 1128 | } |
| 1129 | if (doneSomething){ |
| 1130 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::NAKED_PAIR_COLUMN, 0, position)); |
| 1131 | return true; |
| 1132 | } |
| 1133 | } |
| 1134 | if (section == cellToSectionStartCell(position2)){ |
| 1135 | bool doneSomething = false; |
| 1136 | int secStart = cellToSectionStartCell(position); |
| 1137 | {for (int i=0; i<GRID_SIZE; i++){ |
| 1138 | for (int j=0; j<GRID_SIZE; j++){ |
| 1139 | int position3=secStart+i+(ROW_COL_SEC_SIZE*j); |
| 1140 | if (position3 != position && position3 != position2 && removePossibilitiesInOneFromTwo(position, position3, round)){ |
| 1141 | doneSomething = true; |
| 1142 | } |
| 1143 | } |
| 1144 | }} |
| 1145 | if (doneSomething){ |
| 1146 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::NAKED_PAIR_SECTION, 0, position)); |
| 1147 | return true; |
| 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | return false; |
| 1156 | } |
| 1157 | |
| 1158 | /** |
| 1159 | * Mark exactly one cell which is the only possible value for some row, if |
| 1160 | * such a cell exists. |
| 1161 | * This method will look in a row for a possibility that is only listed |
| 1162 | * for one cell. This type of cell is often called a "hidden single" |
| 1163 | */ |
| 1164 | bool SudokuBoard::onlyValueInRow(int round){ |
| 1165 | for (int row=0; row<ROW_COL_SEC_SIZE; row++){ |
| 1166 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 1167 | int count = 0; |
| 1168 | int lastPosition = 0; |
| 1169 | for (int col=0; col<ROW_COL_SEC_SIZE; col++){ |
| 1170 | int position = (row*ROW_COL_SEC_SIZE)+col; |
| 1171 | int valPos = getPossibilityIndex(valIndex,position); |
| 1172 | if (possibilities[valPos] == 0){ |
| 1173 | count++; |
| 1174 | lastPosition = position; |
| 1175 | } |
| 1176 | } |
| 1177 | if (count == 1){ |
| 1178 | int value = valIndex+1; |
| 1179 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::HIDDEN_SINGLE_ROW, value, lastPosition)); |
| 1180 | mark(lastPosition, round, value); |
| 1181 | return true; |
| 1182 | } |
| 1183 | } |
| 1184 | } |
| 1185 | return false; |
| 1186 | } |
| 1187 | |
| 1188 | /** |
| 1189 | * Mark exactly one cell which is the only possible value for some column, if |
| 1190 | * such a cell exists. |
| 1191 | * This method will look in a column for a possibility that is only listed |
| 1192 | * for one cell. This type of cell is often called a "hidden single" |
| 1193 | */ |
| 1194 | bool SudokuBoard::onlyValueInColumn(int round){ |
| 1195 | for (int col=0; col<ROW_COL_SEC_SIZE; col++){ |
| 1196 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 1197 | int count = 0; |
| 1198 | int lastPosition = 0; |
| 1199 | for (int row=0; row<ROW_COL_SEC_SIZE; row++){ |
| 1200 | int position = rowColumnToCell(row,col); |
| 1201 | int valPos = getPossibilityIndex(valIndex,position); |
| 1202 | if (possibilities[valPos] == 0){ |
| 1203 | count++; |
| 1204 | lastPosition = position; |
| 1205 | } |
| 1206 | } |
| 1207 | if (count == 1){ |
| 1208 | int value = valIndex+1; |
| 1209 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::HIDDEN_SINGLE_COLUMN, value, lastPosition)); |
| 1210 | mark(lastPosition, round, value); |
| 1211 | return true; |
| 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | return false; |
| 1216 | } |
| 1217 | |
| 1218 | /** |
| 1219 | * Mark exactly one cell which is the only possible value for some section, if |
| 1220 | * such a cell exists. |
| 1221 | * This method will look in a section for a possibility that is only listed |
| 1222 | * for one cell. This type of cell is often called a "hidden single" |
| 1223 | */ |
| 1224 | bool SudokuBoard::onlyValueInSection(int round){ |
| 1225 | for (int sec=0; sec<ROW_COL_SEC_SIZE; sec++){ |
| 1226 | int secPos = sectionToFirstCell(sec); |
| 1227 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 1228 | int count = 0; |
| 1229 | int lastPosition = 0; |
| 1230 | {for (int i=0; i<GRID_SIZE; i++){ |
| 1231 | for (int j=0; j<GRID_SIZE; j++){ |
| 1232 | int position = secPos + i + ROW_COL_SEC_SIZE*j; |
| 1233 | int valPos = getPossibilityIndex(valIndex,position); |
| 1234 | if (possibilities[valPos] == 0){ |
| 1235 | count++; |
| 1236 | lastPosition = position; |
| 1237 | } |
| 1238 | } |
| 1239 | }} |
| 1240 | if (count == 1){ |
| 1241 | int value = valIndex+1; |
| 1242 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::HIDDEN_SINGLE_SECTION, value, lastPosition)); |
| 1243 | mark(lastPosition, round, value); |
| 1244 | return true; |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | return false; |
| 1249 | } |
| 1250 | |
| 1251 | /** |
| 1252 | * Mark exactly one cell that has a single possibility, if such a cell exists. |
| 1253 | * This method will look for a cell that has only one possibility. This type |
| 1254 | * of cell is often called a "single" |
| 1255 | */ |
| 1256 | bool SudokuBoard::onlyPossibilityForCell(int round){ |
| 1257 | for (int position=0; position<BOARD_SIZE; position++){ |
| 1258 | if (solution[position] == 0){ |
| 1259 | int count = 0; |
| 1260 | int lastValue = 0; |
| 1261 | for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 1262 | int valPos = getPossibilityIndex(valIndex,position); |
| 1263 | if (possibilities[valPos] == 0){ |
| 1264 | count++; |
| 1265 | lastValue=valIndex+1; |
| 1266 | } |
| 1267 | } |
| 1268 | if (count == 1){ |
| 1269 | mark(position, round, lastValue); |
| 1270 | if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::SINGLE, lastValue, position)); |
| 1271 | return true; |
| 1272 | } |
| 1273 | } |
| 1274 | } |
| 1275 | return false; |
| 1276 | } |
| 1277 | |
| 1278 | /** |
| 1279 | * Mark the given value at the given position. Go through |
| 1280 | * the row, column, and section for the position and remove |
| 1281 | * the value from the possibilities. |
| 1282 | * |
| 1283 | * @param position Position into the board (0-80) |
| 1284 | * @param round Round to mark for rollback purposes |
| 1285 | * @param value The value to go in the square at the given position |
| 1286 | */ |
| 1287 | void SudokuBoard::mark(int position, int round, int value){ |
| 1288 | if (solution[position] != 0) throw ("Marking position that already has been marked."); |
| 1289 | if (solutionRound[position] !=0) throw ("Marking position that was marked another round."); |
| 1290 | int valIndex = value-1; |
| 1291 | solution[position] = value; |
| 1292 | |
| 1293 | int possInd = getPossibilityIndex(valIndex,position); |
| 1294 | if (possibilities[possInd] != 0) throw ("Marking impossible position."); |
| 1295 | |
| 1296 | // Take this value out of the possibilities for everything in the row |
| 1297 | solutionRound[position] = round; |
| 1298 | int rowStart = cellToRow(position)*ROW_COL_SEC_SIZE; |
| 1299 | for (int col=0; col<ROW_COL_SEC_SIZE; col++){ |
| 1300 | int rowVal=rowStart+col; |
| 1301 | int valPos = getPossibilityIndex(valIndex,rowVal); |
| 1302 | //cout << "Row Start: " << rowStart << " Row Value: " << rowVal << " Value Position: " << valPos << endl; |
| 1303 | if (possibilities[valPos] == 0){ |
| 1304 | possibilities[valPos] = round; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | // Take this value out of the possibilities for everything in the column |
| 1309 | int colStart = cellToColumn(position); |
| 1310 | {for (int i=0; i<ROW_COL_SEC_SIZE; i++){ |
| 1311 | int colVal=colStart+(ROW_COL_SEC_SIZE*i); |
| 1312 | int valPos = getPossibilityIndex(valIndex,colVal); |
| 1313 | //cout << "Col Start: " << colStart << " Col Value: " << colVal << " Value Position: " << valPos << endl; |
| 1314 | if (possibilities[valPos] == 0){ |
| 1315 | possibilities[valPos] = round; |
| 1316 | } |
| 1317 | }} |
| 1318 | |
| 1319 | // Take this value out of the possibilities for everything in section |
| 1320 | int secStart = cellToSectionStartCell(position); |
| 1321 | {for (int i=0; i<GRID_SIZE; i++){ |
| 1322 | for (int j=0; j<GRID_SIZE; j++){ |
| 1323 | int secVal=secStart+i+(ROW_COL_SEC_SIZE*j); |
| 1324 | int valPos = getPossibilityIndex(valIndex,secVal); |
| 1325 | //cout << "Sec Start: " << secStart << " Sec Value: " << secVal << " Value Position: " << valPos << endl; |
| 1326 | if (possibilities[valPos] == 0){ |
| 1327 | possibilities[valPos] = round; |
| 1328 | } |
| 1329 | } |
| 1330 | }} |
| 1331 | |
| 1332 | //This position itself is determined, it should have possibilities. |
| 1333 | {for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){ |
| 1334 | int valPos = getPossibilityIndex(valIndex,position); |
| 1335 | if (possibilities[valPos] == 0){ |
| 1336 | possibilities[valPos] = round; |
| 1337 | } |
| 1338 | }} |
| 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * print the given BOARD_SIZEd array of ints |
| 1343 | * as a sudoku puzzle. Use print options from |
| 1344 | * member variables. |
| 1345 | */ |
| 1346 | void SudokuBoard::print(int* sudoku){ |
| 1347 | for(int i=0; i<BOARD_SIZE; i++){ |
| 1348 | if (printStyle == READABLE){ |
| 1349 | cout << " "; |
| 1350 | } |
| 1351 | if (sudoku[i]==0){ |
| 1352 | cout << '.'; |
| 1353 | } else { |
| 1354 | cout << sudoku[i]; |
| 1355 | } |
| 1356 | if (i == BOARD_SIZE-1){ |
| 1357 | if (printStyle == CSV){ |
| 1358 | cout << ","; |
| 1359 | } else { |
| 1360 | cout << endl; |
| 1361 | } |
| 1362 | if (printStyle == READABLE || printStyle == COMPACT){ |
| 1363 | cout << endl; |
| 1364 | } |
| 1365 | } else if (i%ROW_COL_SEC_SIZE==ROW_COL_SEC_SIZE-1){ |
| 1366 | if (printStyle == READABLE || printStyle == COMPACT){ |
| 1367 | cout << endl; |
| 1368 | } |
| 1369 | if (i%SEC_GROUP_SIZE==SEC_GROUP_SIZE-1){ |
| 1370 | if (printStyle == READABLE){ |
| 1371 | cout << "-------|-------|-------" << endl; |
| 1372 | } |
| 1373 | } |
| 1374 | } else if (i%GRID_SIZE==GRID_SIZE-1){ |
| 1375 | if (printStyle == READABLE){ |
| 1376 | cout << " |"; |
| 1377 | } |
| 1378 | } |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | /** |
| 1383 | * Print the sudoku puzzle. |
| 1384 | */ |
| 1385 | void SudokuBoard::printPuzzle(){ |
| 1386 | print(puzzle); |
| 1387 | } |
| 1388 | |
| 1389 | /** |
| 1390 | * Print the sudoku solution. |
| 1391 | */ |
| 1392 | void SudokuBoard::printSolution(){ |
| 1393 | print(solution); |
| 1394 | } |
| 1395 | |
| 1396 | SudokuBoard::~SudokuBoard(){ |
| 1397 | clearPuzzle(); |
| 1398 | delete[] puzzle; |
| 1399 | delete[] solution; |
| 1400 | delete[] possibilities; |
| 1401 | delete[] solutionRound; |
| 1402 | delete[] randomBoardArray; |
| 1403 | delete[] randomPossibilityArray; |
| 1404 | delete solveHistory; |
| 1405 | delete solveInstructions; |
| 1406 | } |
| 1407 | |
| 1408 | LogItem::LogItem(int r, LogType t){ |
| 1409 | init(r,t,0,-1); |
| 1410 | } |
| 1411 | |
| 1412 | LogItem::LogItem(int r, LogType t, int v, int p){ |
| 1413 | init(r,t,v,p); |
| 1414 | } |
| 1415 | |
| 1416 | void LogItem::init(int r, LogType t, int v, int p){ |
| 1417 | round = r; |
| 1418 | type = t; |
| 1419 | value = v; |
| 1420 | position = p; |
| 1421 | } |
| 1422 | |
| 1423 | LogItem::~LogItem(){ |
| 1424 | } |
| 1425 | |
| 1426 | int LogItem::getRound(){ |
| 1427 | return round; |
| 1428 | } |
| 1429 | |
| 1430 | /** |
| 1431 | * Get the type of this log item. |
| 1432 | */ |
| 1433 | LogItem::LogType LogItem::getType(){ |
| 1434 | return type; |
| 1435 | } |
| 1436 | |
| 1437 | /** |
| 1438 | * Print the current log item. The message used is |
| 1439 | * determined by the type of log item. |
| 1440 | */ |
| 1441 | void LogItem::print(){ |
| 1442 | cout << "Round: " << getRound() << " - "; |
| 1443 | switch(type){ |
| 1444 | case GIVEN:{ |
| 1445 | cout << "Mark given"; |
| 1446 | } break; |
| 1447 | case ROLLBACK:{ |
| 1448 | cout << "Roll back round"; |
| 1449 | } break; |
| 1450 | case GUESS:{ |
| 1451 | cout << "Mark guess (start round)"; |
| 1452 | } break; |
| 1453 | case HIDDEN_SINGLE_ROW:{ |
| 1454 | cout << "Mark single possibility for value in row"; |
| 1455 | } break; |
| 1456 | case HIDDEN_SINGLE_COLUMN:{ |
| 1457 | cout << "Mark single possibility for value in column"; |
| 1458 | } break; |
| 1459 | case HIDDEN_SINGLE_SECTION:{ |
| 1460 | cout << "Mark single possibility for value in section"; |
| 1461 | } break; |
| 1462 | case SINGLE:{ |
| 1463 | cout << "Mark only possibility for cell"; |
| 1464 | } break; |
| 1465 | case NAKED_PAIR_ROW:{ |
| 1466 | cout << "Remove possibilities for naked pair in row"; |
| 1467 | } break; |
| 1468 | case NAKED_PAIR_COLUMN:{ |
| 1469 | cout << "Remove possibilities for naked pair in column"; |
| 1470 | } break; |
| 1471 | case NAKED_PAIR_SECTION:{ |
| 1472 | cout << "Remove possibilities for naked pair in section"; |
| 1473 | } break; |
| 1474 | case POINTING_PAIR_TRIPLE_ROW: { |
| 1475 | cout << "Remove possibilities for row because all values are in one section"; |
| 1476 | } break; |
| 1477 | case POINTING_PAIR_TRIPLE_COLUMN: { |
| 1478 | cout << "Remove possibilities for column because all values are in one section"; |
| 1479 | } break; |
| 1480 | case ROW_BOX: { |
| 1481 | cout << "Remove possibilities for section because all values are in one row"; |
| 1482 | } break; |
| 1483 | case COLUMN_BOX: { |
| 1484 | cout << "Remove possibilities for section because all values are in one column"; |
| 1485 | } break; |
| 1486 | case HIDDEN_PAIR_ROW: { |
| 1487 | cout << "Remove possibilities from hidden pair in row"; |
| 1488 | } break; |
| 1489 | case HIDDEN_PAIR_COLUMN: { |
| 1490 | cout << "Remove possibilities from hidden pair in column"; |
| 1491 | } break; |
| 1492 | case HIDDEN_PAIR_SECTION: { |
| 1493 | cout << "Remove possibilities from hidden pair in section"; |
| 1494 | } break; |
| 1495 | default:{ |
| 1496 | cout << "!!! Performed unknown optimization !!!"; |
| 1497 | } break; |
| 1498 | } |
| 1499 | if (value > 0 || position > -1){ |
| 1500 | cout << " ("; |
| 1501 | bool printed = false; |
| 1502 | if (position > -1){ |
| 1503 | if (printed) cout << " - "; |
| 1504 | cout << "Row: " << cellToRow(position)+1 << " - Column: " << cellToColumn(position)+1; |
| 1505 | printed = true; |
| 1506 | } |
| 1507 | if (value > 0){ |
| 1508 | if (printed) cout << " - "; |
| 1509 | cout << "Value: " << value; |
| 1510 | printed = true; |
| 1511 | } |
| 1512 | cout << ")"; |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | /** |
| 1517 | * Given a vector of LogItems, determine how many |
| 1518 | * log items in the vector are of the specified type. |
| 1519 | */ |
| 1520 | int getLogCount(vector<LogItem*>* v, LogItem::LogType type){ |
| 1521 | unsigned int count = 0; |
| 1522 | {for (unsigned int i=0; i<v->size(); i++){ |
| 1523 | if(v->at(i)->getType() == type) count++; |
| 1524 | }} |
| 1525 | return count; |
| 1526 | } |
| 1527 | |
| 1528 | /** |
| 1529 | * Shuffle the values in an array of integers. |
| 1530 | */ |
| 1531 | void shuffleArray(int* array, int size){ |
| 1532 | {for (int i=0; i<size; i++){ |
| 1533 | int tailSize = size-i; |
| 1534 | int randTailPos = rand()%tailSize+i; |
| 1535 | int temp = array[i]; |
| 1536 | array[i] = array[randTailPos]; |
| 1537 | array[randTailPos] = temp; |
| 1538 | }} |
| 1539 | } |
| 1540 | |
| 1541 | SudokuBoard::Symmetry getRandomSymmetry(){ |
| 1542 | switch (rand()%4){ |
| 1543 | case 0: return SudokuBoard::ROTATE90; |
| 1544 | case 1: return SudokuBoard::ROTATE180; |
| 1545 | case 2: return SudokuBoard::MIRROR; |
| 1546 | case 3: return SudokuBoard::FLIP; |
| 1547 | } |
| 1548 | return SudokuBoard::ROTATE90; // NOTE: default action |
| 1549 | } |
| 1550 | |
| 1551 | /** |
| 1552 | * Given the index of a cell (0-80) calculate |
| 1553 | * the column (0-8) in which that cell resides. |
| 1554 | */ |
| 1555 | static inline int cellToColumn(int cell){ |
| 1556 | return cell%ROW_COL_SEC_SIZE; |
| 1557 | } |
| 1558 | |
| 1559 | /** |
| 1560 | * Given the index of a cell (0-80) calculate |
| 1561 | * the row (0-8) in which it resides. |
| 1562 | */ |
| 1563 | static inline int cellToRow(int cell){ |
| 1564 | return cell/ROW_COL_SEC_SIZE; |
| 1565 | } |
| 1566 | |
| 1567 | /** |
| 1568 | * Given the index of a cell (0-80) calculate |
| 1569 | * the section (0-8) in which it resides. |
| 1570 | */ |
| 1571 | static inline int cellToSection(int cell){ |
| 1572 | return (cell/SEC_GROUP_SIZE*GRID_SIZE) |
| 1573 | + (cellToColumn(cell)/GRID_SIZE); |
| 1574 | } |
| 1575 | |
| 1576 | /** |
| 1577 | * Given the index of a cell (0-80) calculate |
| 1578 | * the cell (0-80) that is the upper left start |
| 1579 | * cell of that section. |
| 1580 | */ |
| 1581 | static inline int cellToSectionStartCell(int cell){ |
| 1582 | return (cell/SEC_GROUP_SIZE*SEC_GROUP_SIZE) |
| 1583 | + (cellToColumn(cell)/GRID_SIZE*GRID_SIZE); |
| 1584 | } |
| 1585 | |
| 1586 | /** |
| 1587 | * Given a row (0-8) calculate the first cell (0-80) |
| 1588 | * of that row. |
| 1589 | */ |
| 1590 | static inline int rowToFirstCell(int row){ |
| 1591 | return ROW_COL_SEC_SIZE*row; |
| 1592 | } |
| 1593 | |
| 1594 | /** |
| 1595 | * Given a column (0-8) calculate the first cell (0-80) |
| 1596 | * of that column. |
| 1597 | */ |
| 1598 | static inline int columnToFirstCell(int column){ |
| 1599 | return column; |
| 1600 | } |
| 1601 | |
| 1602 | /** |
| 1603 | * Given a section (0-8) calculate the first cell (0-80) |
| 1604 | * of that section. |
| 1605 | */ |
| 1606 | static inline int sectionToFirstCell(int section){ |
| 1607 | return (section%GRID_SIZE*GRID_SIZE) |
| 1608 | + (section/GRID_SIZE*SEC_GROUP_SIZE); |
| 1609 | } |
| 1610 | |
| 1611 | /** |
| 1612 | * Given a value for a cell (0-8) and a cell (0-80) |
| 1613 | * calculate the offset into the possibility array (0-728). |
| 1614 | */ |
| 1615 | static inline int getPossibilityIndex(int valueIndex, int cell){ |
| 1616 | return valueIndex+(ROW_COL_SEC_SIZE*cell); |
| 1617 | } |
| 1618 | |
| 1619 | /** |
| 1620 | * Given a row (0-8) and a column (0-8) calculate the |
| 1621 | * cell (0-80). |
| 1622 | */ |
| 1623 | static inline int rowColumnToCell(int row, int column){ |
| 1624 | return (row*ROW_COL_SEC_SIZE)+column; |
| 1625 | } |
| 1626 | |
| 1627 | /** |
| 1628 | * Given a section (0-8) and an offset into that section (0-8) |
| 1629 | * calculate the cell (0-80) |
| 1630 | */ |
| 1631 | static inline int sectionToCell(int section, int offset){ |
| 1632 | return sectionToFirstCell(section) |
| 1633 | + ((offset/GRID_SIZE)*ROW_COL_SEC_SIZE) |
| 1634 | + (offset%GRID_SIZE); |
| 1635 | } |
| 1636 | } |