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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
|
/*
* qqwing - Sudoku solver and generator
* Copyright (C) 2006-2014 Stephen Ostermiller http://ostermiller.org/
* Copyright (C) 2007 Jacques Bensimon (jacques@ipm.com)
* Copyright (C) 2011 Jean Guillerez (j.guillerez - orange.fr)
* Copyright (C) 2014 Michael Catanzaro (mcatanzaro@gnome.org)
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include <cstdlib>
#include <iostream>
#include "qqwing.hpp"
namespace qqwing {
string getVersion(){
return VERSION;
}
/**
* 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;
};
void shuffleArray(int* array, int size);
SudokuBoard::Symmetry getRandomSymmetry();
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);
/**
* Create a new Sudoku board
*/
SudokuBoard::SudokuBoard() :
puzzle ( new int[BOARD_SIZE] ),
solution ( new int[BOARD_SIZE] ),
solutionRound ( new int[BOARD_SIZE] ),
possibilities ( new int[POSSIBILITY_SIZE] ),
randomBoardArray ( new int[BOARD_SIZE] ),
randomPossibilityArray ( new int[ROW_COL_SEC_SIZE] ),
recordHistory ( false ),
logHistory( false ),
solveHistory ( new vector<LogItem*>() ),
solveInstructions ( new vector<LogItem*>() ),
printStyle ( READABLE ),
lastSolveRound (0)
{
{for (int i=0; i<BOARD_SIZE; i++){
randomBoardArray[i] = i;
}}
{for (int i=0; i<ROW_COL_SEC_SIZE; i++){
randomPossibilityArray[i] = i;
}}
}
/**
* 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();
}
/**
* Retrieves the puzzle as an unmodifiable array of 81 integers.
*/
const int* SudokuBoard::getPuzzle(){
return puzzle;
}
/**
* Retrieves the puzzle's solution as an unmodifiable array of 81 integers.
*/
const int* SudokuBoard::getSolution(){
return solution;
}
/**
* 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 (unsigned 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";
case SudokuBoard::INTERMEDIATE: return "Intermediate";
case SudokuBoard::EASY: return "Easy";
case SudokuBoard::SIMPLE: return "Simple";
default: 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::shuffleRandomArrays(){
shuffleArray(randomBoardArray, BOARD_SIZE);
shuffleArray(randomPossibilityArray, ROW_COL_SEC_SIZE);
}
void SudokuBoard::clearPuzzle(){
// Clear any existing puzzle
{for (int i=0; i<BOARD_SIZE; i++){
puzzle[i] = 0;
}}
reset();
}
bool SudokuBoard::generatePuzzle(){
return generatePuzzleSymmetry(SudokuBoard::NONE);
}
bool SudokuBoard::generatePuzzleSymmetry(SudokuBoard::Symmetry symmetry){
if (symmetry == SudokuBoard::RANDOM) symmetry = getRandomSymmetry();
// Don't record history while generating.
bool recHistory = recordHistory;
setRecordHistory(false);
bool lHistory = logHistory;
setLogHistory(false);
clearPuzzle();
// Start by getting the randomness in order so that
// each puzzle will be different from the last.
shuffleRandomArrays();
// Now solve the puzzle the whole way. The solve
// uses random algorithms, so we should have a
// really randomly totally filled sudoku
// Even when starting from an empty grid
solve();
if (symmetry == SudokuBoard::NONE){
// Rollback any square for which it is obvious that
// the square doesn't contribute to a unique solution
// (ie, squares that were filled by logic rather
// than by guess)
rollbackNonGuesses();
}
// Record all marked squares as the puzzle so
// that we can call countSolutions without losing it.
{for (int i=0; i<BOARD_SIZE; i++){
puzzle[i] = solution[i];
}}
// Rerandomize everything so that we test squares
// in a different order than they were added.
shuffleRandomArrays();
// Remove one value at a time and see if
// the puzzle still has only one solution.
// If it does, leave it out the point because
// it is not needed.
{for (int i=0; i<BOARD_SIZE; i++){
// check all the positions, but in shuffled order
int position = randomBoardArray[i];
if (puzzle[position] > 0){
int positionsym1 = -1;
int positionsym2 = -1;
int positionsym3 = -1;
switch (symmetry){
case ROTATE90:
positionsym2 = rowColumnToCell(ROW_COL_SEC_SIZE-1-cellToColumn(position),cellToRow(position));
positionsym3 = rowColumnToCell(cellToColumn(position),ROW_COL_SEC_SIZE-1-cellToRow(position));
case ROTATE180:
positionsym1 = rowColumnToCell(ROW_COL_SEC_SIZE-1-cellToRow(position),ROW_COL_SEC_SIZE-1-cellToColumn(position));
break;
case MIRROR:
positionsym1 = rowColumnToCell(cellToRow(position),ROW_COL_SEC_SIZE-1-cellToColumn(position));
break;
case FLIP:
positionsym1 = rowColumnToCell(ROW_COL_SEC_SIZE-1-cellToRow(position),cellToColumn(position));
break;
case RANDOM: // NOTE: Should never happen
break;
case NONE: // NOTE: No need to do anything
break;
}
// try backing out the value and
// counting solutions to the puzzle
int savedValue = puzzle[position];
puzzle[position] = 0;
int savedSym1 = 0;
if (positionsym1 >= 0){
savedSym1 = puzzle[positionsym1];
puzzle[positionsym1] = 0;
}
int savedSym2 = 0;
if (positionsym2 >= 0){
savedSym2 = puzzle[positionsym2];
puzzle[positionsym2] = 0;
}
int savedSym3 = 0;
if (positionsym3 >= 0){
savedSym3 = puzzle[positionsym3];
puzzle[positionsym3] = 0;
}
reset();
if (countSolutions(2, true) > 1){
// Put it back in, it is needed
puzzle[position] = savedValue;
if (positionsym1 >= 0 && savedSym1 != 0) puzzle[positionsym1] = savedSym1;
if (positionsym2 >= 0 && savedSym2 != 0) puzzle[positionsym2] = savedSym2;
if (positionsym3 >= 0 && savedSym3 != 0) puzzle[positionsym3] = savedSym3;
}
}
}}
// Clear all solution info, leaving just the puzzle.
reset();
// Restore recording history.
setRecordHistory(recHistory);
setLogHistory(lHistory);
return true;
}
void SudokuBoard::rollbackNonGuesses(){
// Guesses are odd rounds
// Non-guesses are even rounds
{for (int i=2; i<=lastSolveRound; i+=2){
rollbackRound(i);
}}
}
void SudokuBoard::setPrintStyle(PrintStyle ps){
printStyle = ps;
}
void SudokuBoard::setRecordHistory(bool recHistory){
recordHistory = recHistory;
}
void SudokuBoard::setLogHistory(bool logHist){
logHistory = logHist;
}
void SudokuBoard::addHistoryItem(LogItem* l){
if (logHistory){
l->print();
cout << endl;
}
if (recordHistory){
solveHistory->push_back(l);
solveInstructions->push_back(l);
} else {
delete l;
}
}
void SudokuBoard::printHistory(vector<LogItem*>* v){
if (!recordHistory){
cout << "History was not recorded.";
if (printStyle == CSV){
cout << " -- ";
} else {
cout << endl;
}
}
{for (unsigned int i=0;i<v->size();i++){
cout << i+1 << ". ";
v->at(i)->print();
if (printStyle == CSV){
cout << " -- ";
} else {
cout << endl;
}
}}
if (printStyle == CSV){
cout << ",";
} else {
cout << endl;
}
}
void SudokuBoard::printSolveInstructions(){
if (isSolved()){
printHistory(solveInstructions);
} else {
cout << "No solve instructions - Puzzle is not possible to solve." << endl;
}
}
void SudokuBoard::printSolveHistory(){
printHistory(solveHistory);
}
bool SudokuBoard::solve(){
reset();
shuffleRandomArrays();
return solve(2);
}
bool SudokuBoard::solve(int round){
lastSolveRound = round;
while (singleSolveMove(round)){
if (isSolved()) return true;
if (isImpossible()) return false;
}
int nextGuessRound = round+1;
int nextRound = round+2;
for (int guessNumber=0; guess(nextGuessRound, guessNumber); guessNumber++){
if (isImpossible() || !solve(nextRound)){
rollbackRound(nextRound);
rollbackRound(nextGuessRound);
} else {
return true;
}
}
return false;
}
bool SudokuBoard::hasUniqueSolution(){
return countSolutionsLimited() == 1;
}
int SudokuBoard::countSolutions(){
return countSolutions(false);
}
int SudokuBoard::countSolutionsLimited(){
return countSolutions(true);
}
int SudokuBoard::countSolutions(bool limitToTwo){
// Don't record history while generating.
bool recHistory = recordHistory;
setRecordHistory(false);
bool lHistory = logHistory;
setLogHistory(false);
reset();
int solutionCount = countSolutions(2, limitToTwo);
// Restore recording history.
setRecordHistory(recHistory);
setLogHistory(lHistory);
return solutionCount;
}
int SudokuBoard::countSolutions(int round, bool limitToTwo){
while (singleSolveMove(round)){
if (isSolved()){
rollbackRound(round);
return 1;
}
if (isImpossible()){
rollbackRound(round);
return 0;
}
}
int solutions = 0;
int nextRound = round+1;
for (int guessNumber=0; guess(nextRound, guessNumber); guessNumber++){
solutions += countSolutions(nextRound, limitToTwo);
if (limitToTwo && solutions >=2){
rollbackRound(round);
return solutions;
}
}
rollbackRound(round);
return solutions;
}
void SudokuBoard::rollbackRound(int round){
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::ROLLBACK));
{for (int i=0; i<BOARD_SIZE; i++){
if (solutionRound[i] == round){
solutionRound[i] = 0;
solution[i] = 0;
}
}}
{for (int i=0; i<POSSIBILITY_SIZE; i++){
if (possibilities[i] == round){
possibilities[i] = 0;
}
}}
while(solveInstructions->size() > 0 && solveInstructions->back()->getRound() == round){
solveInstructions->pop_back();
}
}
bool SudokuBoard::isSolved(){
{for (int i=0; i<BOARD_SIZE; i++){
if (solution[i] == 0){
return false;
}
}}
return true;
}
bool SudokuBoard::isImpossible(){
for (int position=0; position<BOARD_SIZE; position++){
if (solution[position] == 0){
int count = 0;
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
int valPos = getPossibilityIndex(valIndex,position);
if (possibilities[valPos] == 0) count++;
}
if (count == 0) {
return true;
}
}
}
return false;
}
int SudokuBoard::findPositionWithFewestPossibilities(){
int minPossibilities = 10;
int bestPosition = 0;
{for (int i=0; i<BOARD_SIZE; i++){
int position = randomBoardArray[i];
if (solution[position] == 0){
int count = 0;
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
int valPos = getPossibilityIndex(valIndex,position);
if (possibilities[valPos] == 0) count++;
}
if (count < minPossibilities){
minPossibilities = count;
bestPosition = position;
}
}
}}
return bestPosition;
}
bool SudokuBoard::guess(int round, int guessNumber){
int localGuessCount = 0;
int position = findPositionWithFewestPossibilities();
{for (int i=0; i<ROW_COL_SEC_SIZE; i++){
int valIndex = randomPossibilityArray[i];
int valPos = getPossibilityIndex(valIndex,position);
if (possibilities[valPos] == 0){
if (localGuessCount == guessNumber){
int value = valIndex+1;
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::GUESS, value, position));
mark(position, round, value);
return true;
}
localGuessCount++;
}
}}
return false;
}
bool SudokuBoard::singleSolveMove(int round){
if (onlyPossibilityForCell(round)) return true;
if (onlyValueInSection(round)) return true;
if (onlyValueInRow(round)) return true;
if (onlyValueInColumn(round)) return true;
if (handleNakedPairs(round)) return true;
if (pointingRowReduction(round)) return true;
if (pointingColumnReduction(round)) return true;
if (rowBoxReduction(round)) return true;
if (colBoxReduction(round)) return true;
if (hiddenPairInRow(round)) return true;
if (hiddenPairInColumn(round)) return true;
if (hiddenPairInSection(round)) return true;
return false;
}
bool SudokuBoard::colBoxReduction(int round){
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
for (int col=0; col<ROW_COL_SEC_SIZE; col++){
int colStart = columnToFirstCell(col);
bool inOneBox = true;
int colBox = -1;
{for (int i=0; i<GRID_SIZE; i++){
for (int j=0; j<GRID_SIZE; j++){
int row = i*GRID_SIZE+j;
int position = rowColumnToCell(row, col);
int valPos = getPossibilityIndex(valIndex,position);
if(possibilities[valPos] == 0){
if (colBox == -1 || colBox == i){
colBox = i;
} else {
inOneBox = false;
}
}
}
}}
if (inOneBox && colBox != -1){
bool doneSomething = false;
int row = GRID_SIZE*colBox;
int secStart = cellToSectionStartCell(rowColumnToCell(row, col));
int secStartRow = cellToRow(secStart);
int secStartCol = cellToColumn(secStart);
{for (int i=0; i<GRID_SIZE; i++){
for (int j=0; j<GRID_SIZE; j++){
int row2 = secStartRow+i;
int col2 = secStartCol+j;
int position = rowColumnToCell(row2, col2);
int valPos = getPossibilityIndex(valIndex,position);
if (col != col2 && possibilities[valPos] == 0){
possibilities[valPos] = round;
doneSomething = true;
}
}
}}
if (doneSomething){
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::COLUMN_BOX, valIndex+1, colStart));
return true;
}
}
}
}
return false;
}
bool SudokuBoard::rowBoxReduction(int round){
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
for (int row=0; row<ROW_COL_SEC_SIZE; row++){
int rowStart = rowToFirstCell(row);
bool inOneBox = true;
int rowBox = -1;
{for (int i=0; i<GRID_SIZE; i++){
for (int j=0; j<GRID_SIZE; j++){
int column = i*GRID_SIZE+j;
int position = rowColumnToCell(row, column);
int valPos = getPossibilityIndex(valIndex,position);
if(possibilities[valPos] == 0){
if (rowBox == -1 || rowBox == i){
rowBox = i;
} else {
inOneBox = false;
}
}
}
}}
if (inOneBox && rowBox != -1){
bool doneSomething = false;
int column = GRID_SIZE*rowBox;
int secStart = cellToSectionStartCell(rowColumnToCell(row, column));
int secStartRow = cellToRow(secStart);
int secStartCol = cellToColumn(secStart);
{for (int i=0; i<GRID_SIZE; i++){
for (int j=0; j<GRID_SIZE; j++){
int row2 = secStartRow+i;
int col2 = secStartCol+j;
int position = rowColumnToCell(row2, col2);
int valPos = getPossibilityIndex(valIndex,position);
if (row != row2 && possibilities[valPos] == 0){
possibilities[valPos] = round;
doneSomething = true;
}
}
}}
if (doneSomething){
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::ROW_BOX, valIndex+1, rowStart));
return true;
}
}
}
}
return false;
}
bool SudokuBoard::pointingRowReduction(int round){
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
for (int section=0; section<ROW_COL_SEC_SIZE; section++){
int secStart = sectionToFirstCell(section);
bool inOneRow = true;
int boxRow = -1;
for (int j=0; j<GRID_SIZE; j++){
{for (int i=0; i<GRID_SIZE; i++){
int secVal=secStart+i+(ROW_COL_SEC_SIZE*j);
int valPos = getPossibilityIndex(valIndex,secVal);
if(possibilities[valPos] == 0){
if (boxRow == -1 || boxRow == j){
boxRow = j;
} else {
inOneRow = false;
}
}
}}
}
if (inOneRow && boxRow != -1){
bool doneSomething = false;
int row = cellToRow(secStart) + boxRow;
int rowStart = rowToFirstCell(row);
{for (int i=0; i<ROW_COL_SEC_SIZE; i++){
int position = rowStart+i;
int section2 = cellToSection(position);
int valPos = getPossibilityIndex(valIndex,position);
if (section != section2 && possibilities[valPos] == 0){
possibilities[valPos] = round;
doneSomething = true;
}
}}
if (doneSomething){
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::POINTING_PAIR_TRIPLE_ROW, valIndex+1, rowStart));
return true;
}
}
}
}
return false;
}
bool SudokuBoard::pointingColumnReduction(int round){
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
for (int section=0; section<ROW_COL_SEC_SIZE; section++){
int secStart = sectionToFirstCell(section);
bool inOneCol = true;
int boxCol = -1;
{for (int i=0; i<GRID_SIZE; i++){
for (int j=0; j<GRID_SIZE; j++){
int secVal=secStart+i+(ROW_COL_SEC_SIZE*j);
int valPos = getPossibilityIndex(valIndex,secVal);
if(possibilities[valPos] == 0){
if (boxCol == -1 || boxCol == i){
boxCol = i;
} else {
inOneCol = false;
}
}
}
}}
if (inOneCol && boxCol != -1){
bool doneSomething = false;
int col = cellToColumn(secStart) + boxCol;
int colStart = columnToFirstCell(col);
{for (int i=0; i<ROW_COL_SEC_SIZE; i++){
int position = colStart+(ROW_COL_SEC_SIZE*i);
int section2 = cellToSection(position);
int valPos = getPossibilityIndex(valIndex,position);
if (section != section2 && possibilities[valPos] == 0){
possibilities[valPos] = round;
doneSomething = true;
}
}}
if (doneSomething){
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::POINTING_PAIR_TRIPLE_COLUMN, valIndex+1, colStart));
return true;
}
}
}
}
return false;
}
int SudokuBoard::countPossibilities(int position){
int count = 0;
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
int valPos = getPossibilityIndex(valIndex,position);
if (possibilities[valPos] == 0) count++;
}
return count;
}
bool SudokuBoard::arePossibilitiesSame(int position1, int position2){
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
int valPos1 = getPossibilityIndex(valIndex,position1);
int valPos2 = getPossibilityIndex(valIndex,position2);
if ((possibilities[valPos1] == 0 || possibilities[valPos2] == 0) && (possibilities[valPos1] != 0 || possibilities[valPos2] != 0)){
return false;
}
}
return true;
}
bool SudokuBoard::removePossibilitiesInOneFromTwo(int position1, int position2, int round){
bool doneSomething = false;
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
int valPos1 = getPossibilityIndex(valIndex,position1);
int valPos2 = getPossibilityIndex(valIndex,position2);
if (possibilities[valPos1] == 0 && possibilities[valPos2] == 0){
possibilities[valPos2] = round;
doneSomething = true;
}
}
return doneSomething;
}
bool SudokuBoard::hiddenPairInColumn(int round){
for (int column=0; column<ROW_COL_SEC_SIZE; column++){
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
int r1 = -1;
int r2 = -1;
int valCount = 0;
for (int row=0; row<ROW_COL_SEC_SIZE; row++){
int position = rowColumnToCell(row,column);
int valPos = getPossibilityIndex(valIndex,position);
if (possibilities[valPos] == 0){
if (r1 == -1 || r1 == row){
r1 = row;
} else if (r2 == -1 || r2 == row){
r2 = row;
}
valCount++;
}
}
if (valCount==2){
for (int valIndex2=valIndex+1; valIndex2<ROW_COL_SEC_SIZE; valIndex2++){
int r3 = -1;
int r4 = -1;
int valCount2 = 0;
for (int row=0; row<ROW_COL_SEC_SIZE; row++){
int position = rowColumnToCell(row,column);
int valPos = getPossibilityIndex(valIndex2,position);
if (possibilities[valPos] == 0){
if (r3 == -1 || r3 == row){
r3 = row;
} else if (r4 == -1 || r4 == row){
r4 = row;
}
valCount2++;
}
}
if (valCount2==2 && r1==r3 && r2==r4){
bool doneSomething = false;
for (int valIndex3=0; valIndex3<ROW_COL_SEC_SIZE; valIndex3++){
if (valIndex3 != valIndex && valIndex3 != valIndex2){
int position1 = rowColumnToCell(r1,column);
int position2 = rowColumnToCell(r2,column);
int valPos1 = getPossibilityIndex(valIndex3,position1);
int valPos2 = getPossibilityIndex(valIndex3,position2);
if (possibilities[valPos1] == 0){
possibilities[valPos1] = round;
doneSomething = true;
}
if (possibilities[valPos2] == 0){
possibilities[valPos2] = round;
doneSomething = true;
}
}
}
if (doneSomething){
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::HIDDEN_PAIR_COLUMN, valIndex+1, rowColumnToCell(r1,column)));
return true;
}
}
}
}
}
}
return false;
}
bool SudokuBoard::hiddenPairInSection(int round){
for (int section=0; section<ROW_COL_SEC_SIZE; section++){
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
int si1 = -1;
int si2 = -1;
int valCount = 0;
for (int secInd=0; secInd<ROW_COL_SEC_SIZE; secInd++){
int position = sectionToCell(section,secInd);
int valPos = getPossibilityIndex(valIndex,position);
if (possibilities[valPos] == 0){
if (si1 == -1 || si1 == secInd){
si1 = secInd;
} else if (si2 == -1 || si2 == secInd){
si2 = secInd;
}
valCount++;
}
}
if (valCount==2){
for (int valIndex2=valIndex+1; valIndex2<ROW_COL_SEC_SIZE; valIndex2++){
int si3 = -1;
int si4 = -1;
int valCount2 = 0;
for (int secInd=0; secInd<ROW_COL_SEC_SIZE; secInd++){
int position = sectionToCell(section,secInd);
int valPos = getPossibilityIndex(valIndex2,position);
if (possibilities[valPos] == 0){
if (si3 == -1 || si3 == secInd){
si3 = secInd;
} else if (si4 == -1 || si4 == secInd){
si4 = secInd;
}
valCount2++;
}
}
if (valCount2==2 && si1==si3 && si2==si4){
bool doneSomething = false;
for (int valIndex3=0; valIndex3<ROW_COL_SEC_SIZE; valIndex3++){
if (valIndex3 != valIndex && valIndex3 != valIndex2){
int position1 = sectionToCell(section,si1);
int position2 = sectionToCell(section,si2);
int valPos1 = getPossibilityIndex(valIndex3,position1);
int valPos2 = getPossibilityIndex(valIndex3,position2);
if (possibilities[valPos1] == 0){
possibilities[valPos1] = round;
doneSomething = true;
}
if (possibilities[valPos2] == 0){
possibilities[valPos2] = round;
doneSomething = true;
}
}
}
if (doneSomething){
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::HIDDEN_PAIR_SECTION, valIndex+1, sectionToCell(section,si1)));
return true;
}
}
}
}
}
}
return false;
}
bool SudokuBoard::hiddenPairInRow(int round){
for (int row=0; row<ROW_COL_SEC_SIZE; row++){
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
int c1 = -1;
int c2 = -1;
int valCount = 0;
for (int column=0; column<ROW_COL_SEC_SIZE; column++){
int position = rowColumnToCell(row,column);
int valPos = getPossibilityIndex(valIndex,position);
if (possibilities[valPos] == 0){
if (c1 == -1 || c1 == column){
c1 = column;
} else if (c2 == -1 || c2 == column){
c2 = column;
}
valCount++;
}
}
if (valCount==2){
for (int valIndex2=valIndex+1; valIndex2<ROW_COL_SEC_SIZE; valIndex2++){
int c3 = -1;
int c4 = -1;
int valCount2 = 0;
for (int column=0; column<ROW_COL_SEC_SIZE; column++){
int position = rowColumnToCell(row,column);
int valPos = getPossibilityIndex(valIndex2,position);
if (possibilities[valPos] == 0){
if (c3 == -1 || c3 == column){
c3 = column;
} else if (c4 == -1 || c4 == column){
c4 = column;
}
valCount2++;
}
}
if (valCount2==2 && c1==c3 && c2==c4){
bool doneSomething = false;
for (int valIndex3=0; valIndex3<ROW_COL_SEC_SIZE; valIndex3++){
if (valIndex3 != valIndex && valIndex3 != valIndex2){
int position1 = rowColumnToCell(row,c1);
int position2 = rowColumnToCell(row,c2);
int valPos1 = getPossibilityIndex(valIndex3,position1);
int valPos2 = getPossibilityIndex(valIndex3,position2);
if (possibilities[valPos1] == 0){
possibilities[valPos1] = round;
doneSomething = true;
}
if (possibilities[valPos2] == 0){
possibilities[valPos2] = round;
doneSomething = true;
}
}
}
if (doneSomething){
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::HIDDEN_PAIR_ROW, valIndex+1, rowColumnToCell(row,c1)));
return true;
}
}
}
}
}
}
return false;
}
bool SudokuBoard::handleNakedPairs(int round){
for (int position=0; position<BOARD_SIZE; position++){
int possibilities = countPossibilities(position);
if (possibilities == 2){
int row = cellToRow(position);
int column = cellToColumn(position);
int section = cellToSectionStartCell(position);
for (int position2=position; position2<BOARD_SIZE; position2++){
if (position != position2){
int possibilities2 = countPossibilities(position2);
if (possibilities2 == 2 && arePossibilitiesSame(position, position2)){
if (row == cellToRow(position2)){
bool doneSomething = false;
for (int column2=0; column2<ROW_COL_SEC_SIZE; column2++){
int position3 = rowColumnToCell(row,column2);
if (position3 != position && position3 != position2 && removePossibilitiesInOneFromTwo(position, position3, round)){
doneSomething = true;
}
}
if (doneSomething){
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::NAKED_PAIR_ROW, 0, position));
return true;
}
}
if (column == cellToColumn(position2)){
bool doneSomething = false;
for (int row2=0; row2<ROW_COL_SEC_SIZE; row2++){
int position3 = rowColumnToCell(row2,column);
if (position3 != position && position3 != position2 && removePossibilitiesInOneFromTwo(position, position3, round)){
doneSomething = true;
}
}
if (doneSomething){
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::NAKED_PAIR_COLUMN, 0, position));
return true;
}
}
if (section == cellToSectionStartCell(position2)){
bool doneSomething = false;
int secStart = cellToSectionStartCell(position);
{for (int i=0; i<GRID_SIZE; i++){
for (int j=0; j<GRID_SIZE; j++){
int position3=secStart+i+(ROW_COL_SEC_SIZE*j);
if (position3 != position && position3 != position2 && removePossibilitiesInOneFromTwo(position, position3, round)){
doneSomething = true;
}
}
}}
if (doneSomething){
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::NAKED_PAIR_SECTION, 0, position));
return true;
}
}
}
}
}
}
}
return false;
}
/**
* Mark exactly one cell which is the only possible value for some row, if
* such a cell exists.
* This method will look in a row for a possibility that is only listed
* for one cell. This type of cell is often called a "hidden single"
*/
bool SudokuBoard::onlyValueInRow(int round){
for (int row=0; row<ROW_COL_SEC_SIZE; row++){
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
int count = 0;
int lastPosition = 0;
for (int col=0; col<ROW_COL_SEC_SIZE; col++){
int position = (row*ROW_COL_SEC_SIZE)+col;
int valPos = getPossibilityIndex(valIndex,position);
if (possibilities[valPos] == 0){
count++;
lastPosition = position;
}
}
if (count == 1){
int value = valIndex+1;
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::HIDDEN_SINGLE_ROW, value, lastPosition));
mark(lastPosition, round, value);
return true;
}
}
}
return false;
}
/**
* Mark exactly one cell which is the only possible value for some column, if
* such a cell exists.
* This method will look in a column for a possibility that is only listed
* for one cell. This type of cell is often called a "hidden single"
*/
bool SudokuBoard::onlyValueInColumn(int round){
for (int col=0; col<ROW_COL_SEC_SIZE; col++){
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
int count = 0;
int lastPosition = 0;
for (int row=0; row<ROW_COL_SEC_SIZE; row++){
int position = rowColumnToCell(row,col);
int valPos = getPossibilityIndex(valIndex,position);
if (possibilities[valPos] == 0){
count++;
lastPosition = position;
}
}
if (count == 1){
int value = valIndex+1;
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::HIDDEN_SINGLE_COLUMN, value, lastPosition));
mark(lastPosition, round, value);
return true;
}
}
}
return false;
}
/**
* Mark exactly one cell which is the only possible value for some section, if
* such a cell exists.
* This method will look in a section for a possibility that is only listed
* for one cell. This type of cell is often called a "hidden single"
*/
bool SudokuBoard::onlyValueInSection(int round){
for (int sec=0; sec<ROW_COL_SEC_SIZE; sec++){
int secPos = sectionToFirstCell(sec);
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
int count = 0;
int lastPosition = 0;
{for (int i=0; i<GRID_SIZE; i++){
for (int j=0; j<GRID_SIZE; j++){
int position = secPos + i + ROW_COL_SEC_SIZE*j;
int valPos = getPossibilityIndex(valIndex,position);
if (possibilities[valPos] == 0){
count++;
lastPosition = position;
}
}
}}
if (count == 1){
int value = valIndex+1;
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::HIDDEN_SINGLE_SECTION, value, lastPosition));
mark(lastPosition, round, value);
return true;
}
}
}
return false;
}
/**
* Mark exactly one cell that has a single possibility, if such a cell exists.
* This method will look for a cell that has only one possibility. This type
* of cell is often called a "single"
*/
bool SudokuBoard::onlyPossibilityForCell(int round){
for (int position=0; position<BOARD_SIZE; position++){
if (solution[position] == 0){
int count = 0;
int lastValue = 0;
for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
int valPos = getPossibilityIndex(valIndex,position);
if (possibilities[valPos] == 0){
count++;
lastValue=valIndex+1;
}
}
if (count == 1){
mark(position, round, lastValue);
if (logHistory || recordHistory) addHistoryItem(new LogItem(round, LogItem::SINGLE, lastValue, position));
return true;
}
}
}
return false;
}
/**
* Mark the given value at the given position. Go through
* the row, column, and section for the position and remove
* the value from the possibilities.
*
* @param position Position into the board (0-80)
* @param round Round to mark for rollback purposes
* @param value The value to go in the square at the given position
*/
void SudokuBoard::mark(int position, int round, int value){
if (solution[position] != 0) throw ("Marking position that already has been marked.");
if (solutionRound[position] !=0) throw ("Marking position that was marked another round.");
int valIndex = value-1;
solution[position] = value;
int possInd = getPossibilityIndex(valIndex,position);
if (possibilities[possInd] != 0) throw ("Marking impossible position.");
// Take this value out of the possibilities for everything in the row
solutionRound[position] = round;
int rowStart = cellToRow(position)*ROW_COL_SEC_SIZE;
for (int col=0; col<ROW_COL_SEC_SIZE; col++){
int rowVal=rowStart+col;
int valPos = getPossibilityIndex(valIndex,rowVal);
//cout << "Row Start: " << rowStart << " Row Value: " << rowVal << " Value Position: " << valPos << endl;
if (possibilities[valPos] == 0){
possibilities[valPos] = round;
}
}
// Take this value out of the possibilities for everything in the column
int colStart = cellToColumn(position);
{for (int i=0; i<ROW_COL_SEC_SIZE; i++){
int colVal=colStart+(ROW_COL_SEC_SIZE*i);
int valPos = getPossibilityIndex(valIndex,colVal);
//cout << "Col Start: " << colStart << " Col Value: " << colVal << " Value Position: " << valPos << endl;
if (possibilities[valPos] == 0){
possibilities[valPos] = round;
}
}}
// Take this value out of the possibilities for everything in section
int secStart = cellToSectionStartCell(position);
{for (int i=0; i<GRID_SIZE; i++){
for (int j=0; j<GRID_SIZE; j++){
int secVal=secStart+i+(ROW_COL_SEC_SIZE*j);
int valPos = getPossibilityIndex(valIndex,secVal);
//cout << "Sec Start: " << secStart << " Sec Value: " << secVal << " Value Position: " << valPos << endl;
if (possibilities[valPos] == 0){
possibilities[valPos] = round;
}
}
}}
//This position itself is determined, it should have possibilities.
{for (int valIndex=0; valIndex<ROW_COL_SEC_SIZE; valIndex++){
int valPos = getPossibilityIndex(valIndex,position);
if (possibilities[valPos] == 0){
possibilities[valPos] = round;
}
}}
}
/**
* print the given BOARD_SIZEd array of ints
* as a sudoku puzzle. Use print options from
* member variables.
*/
void SudokuBoard::print(int* sudoku){
for(int i=0; i<BOARD_SIZE; i++){
if (printStyle == READABLE){
cout << " ";
}
if (sudoku[i]==0){
cout << '.';
} else {
cout << sudoku[i];
}
if (i == BOARD_SIZE-1){
if (printStyle == CSV){
cout << ",";
} else {
cout << endl;
}
if (printStyle == READABLE || printStyle == COMPACT){
cout << endl;
}
} else if (i%ROW_COL_SEC_SIZE==ROW_COL_SEC_SIZE-1){
if (printStyle == READABLE || printStyle == COMPACT){
cout << endl;
}
if (i%SEC_GROUP_SIZE==SEC_GROUP_SIZE-1){
if (printStyle == READABLE){
cout << "-------|-------|-------" << endl;
}
}
} else if (i%GRID_SIZE==GRID_SIZE-1){
if (printStyle == READABLE){
cout << " |";
}
}
}
}
/**
* Print the sudoku puzzle.
*/
void SudokuBoard::printPuzzle(){
print(puzzle);
}
/**
* Print the sudoku solution.
*/
void SudokuBoard::printSolution(){
print(solution);
}
SudokuBoard::~SudokuBoard(){
clearPuzzle();
delete[] puzzle;
delete[] solution;
delete[] possibilities;
delete[] solutionRound;
delete[] randomBoardArray;
delete[] randomPossibilityArray;
delete solveHistory;
delete solveInstructions;
}
LogItem::LogItem(int r, LogType t){
init(r,t,0,-1);
}
LogItem::LogItem(int r, LogType t, int v, int p){
init(r,t,v,p);
}
void LogItem::init(int r, LogType t, int v, int p){
round = r;
type = t;
value = v;
position = p;
}
LogItem::~LogItem(){
}
int LogItem::getRound(){
return round;
}
/**
* Get the type of this log item.
*/
LogItem::LogType LogItem::getType(){
return type;
}
/**
* Print the current log item. The message used is
* determined by the type of log item.
*/
void LogItem::print(){
cout << "Round: " << getRound() << " - ";
switch(type){
case GIVEN:{
cout << "Mark given";
} break;
case ROLLBACK:{
cout << "Roll back round";
} break;
case GUESS:{
cout << "Mark guess (start round)";
} break;
case HIDDEN_SINGLE_ROW:{
cout << "Mark single possibility for value in row";
} break;
case HIDDEN_SINGLE_COLUMN:{
cout << "Mark single possibility for value in column";
} break;
case HIDDEN_SINGLE_SECTION:{
cout << "Mark single possibility for value in section";
} break;
case SINGLE:{
cout << "Mark only possibility for cell";
} break;
case NAKED_PAIR_ROW:{
cout << "Remove possibilities for naked pair in row";
} break;
case NAKED_PAIR_COLUMN:{
cout << "Remove possibilities for naked pair in column";
} break;
case NAKED_PAIR_SECTION:{
cout << "Remove possibilities for naked pair in section";
} break;
case POINTING_PAIR_TRIPLE_ROW: {
cout << "Remove possibilities for row because all values are in one section";
} break;
case POINTING_PAIR_TRIPLE_COLUMN: {
cout << "Remove possibilities for column because all values are in one section";
} break;
case ROW_BOX: {
cout << "Remove possibilities for section because all values are in one row";
} break;
case COLUMN_BOX: {
cout << "Remove possibilities for section because all values are in one column";
} break;
case HIDDEN_PAIR_ROW: {
cout << "Remove possibilities from hidden pair in row";
} break;
case HIDDEN_PAIR_COLUMN: {
cout << "Remove possibilities from hidden pair in column";
} break;
case HIDDEN_PAIR_SECTION: {
cout << "Remove possibilities from hidden pair in section";
} break;
default:{
cout << "!!! Performed unknown optimization !!!";
} break;
}
if (value > 0 || position > -1){
cout << " (";
bool printed = false;
if (position > -1){
if (printed) cout << " - ";
cout << "Row: " << cellToRow(position)+1 << " - Column: " << cellToColumn(position)+1;
printed = true;
}
if (value > 0){
if (printed) cout << " - ";
cout << "Value: " << value;
printed = true;
}
cout << ")";
}
}
/**
* Given a vector of LogItems, determine how many
* log items in the vector are of the specified type.
*/
int getLogCount(vector<LogItem*>* v, LogItem::LogType type){
unsigned int count = 0;
{for (unsigned int i=0; i<v->size(); i++){
if(v->at(i)->getType() == type) count++;
}}
return count;
}
/**
* Shuffle the values in an array of integers.
*/
void shuffleArray(int* array, int size){
{for (int i=0; i<size; i++){
int tailSize = size-i;
int randTailPos = rand()%tailSize+i;
int temp = array[i];
array[i] = array[randTailPos];
array[randTailPos] = temp;
}}
}
SudokuBoard::Symmetry getRandomSymmetry(){
switch (rand()%4){
case 0: return SudokuBoard::ROTATE90;
case 1: return SudokuBoard::ROTATE180;
case 2: return SudokuBoard::MIRROR;
case 3: return SudokuBoard::FLIP;
}
return SudokuBoard::ROTATE90; // NOTE: default action
}
/**
* Given the index of a cell (0-80) calculate
* the column (0-8) in which that cell resides.
*/
static inline int cellToColumn(int cell){
return cell%ROW_COL_SEC_SIZE;
}
/**
* Given the index of a cell (0-80) calculate
* the row (0-8) in which it resides.
*/
static inline int cellToRow(int cell){
return cell/ROW_COL_SEC_SIZE;
}
/**
* Given the index of a cell (0-80) calculate
* the section (0-8) in which it resides.
*/
static inline int cellToSection(int cell){
return (cell/SEC_GROUP_SIZE*GRID_SIZE)
+ (cellToColumn(cell)/GRID_SIZE);
}
/**
* Given the index of a cell (0-80) calculate
* the cell (0-80) that is the upper left start
* cell of that section.
*/
static inline int cellToSectionStartCell(int cell){
return (cell/SEC_GROUP_SIZE*SEC_GROUP_SIZE)
+ (cellToColumn(cell)/GRID_SIZE*GRID_SIZE);
}
/**
* Given a row (0-8) calculate the first cell (0-80)
* of that row.
*/
static inline int rowToFirstCell(int row){
return ROW_COL_SEC_SIZE*row;
}
/**
* Given a column (0-8) calculate the first cell (0-80)
* of that column.
*/
static inline int columnToFirstCell(int column){
return column;
}
/**
* Given a section (0-8) calculate the first cell (0-80)
* of that section.
*/
static inline int sectionToFirstCell(int section){
return (section%GRID_SIZE*GRID_SIZE)
+ (section/GRID_SIZE*SEC_GROUP_SIZE);
}
/**
* Given a value for a cell (0-8) and a cell (0-80)
* calculate the offset into the possibility array (0-728).
*/
static inline int getPossibilityIndex(int valueIndex, int cell){
return valueIndex+(ROW_COL_SEC_SIZE*cell);
}
/**
* Given a row (0-8) and a column (0-8) calculate the
* cell (0-80).
*/
static inline int rowColumnToCell(int row, int column){
return (row*ROW_COL_SEC_SIZE)+column;
}
/**
* Given a section (0-8) and an offset into that section (0-8)
* calculate the cell (0-80)
*/
static inline int sectionToCell(int section, int offset){
return sectionToFirstCell(section)
+ ((offset/GRID_SIZE)*ROW_COL_SEC_SIZE)
+ (offset%GRID_SIZE);
}
}
|