#include #include #include #include #include #define ROW_LIMIT 9 #define COL_LIMIT 9 #define VAL_LIMIT 9 #define TRUE 1 #define FALSE 0 struct cell { int value; int row; int collumn; int box; int candidates[VAL_LIMIT]; }; struct box { struct cell* cells[3][3]; }; struct row { struct cell* cells[ROW_LIMIT]; }; struct col { struct cell* cells[9]; }; struct board { struct box boxes[3][3]; struct row rows[ROW_LIMIT]; struct col cols[COL_LIMIT]; }; /* ANSI function prototypes. */ void stripXFromBoardRowCol(struct board* b, int x, int row, int col); void stripXFromRow(int x, struct row* row); void stripXFromCol(int x, struct col* col); void clearAllValues(struct board* b); void printByRows(struct board* b); void setBoardRowColX(struct board* b, int row, int col, int x); void setBoardColRowX(struct board* b, int row, int col, int x); void usage(char* progName); void initializeCells(struct cell* cells); void populateBoxesFromCells(struct board* b, struct cell* cells); void populateRowsFromCells(struct board* b, struct cell* cells); void populateColsFromCells(struct board* b, struct cell* cells); void populateFromFile(struct board* b, int ifd); void dumpDebugInfo(struct board* b, struct cell* cells); void printCandidateMatrix(struct board* b); int cullRows(struct board* b); int cullCols(struct board* b); int cullBoxes(struct board* b); int findKnownCellByCandidates(struct board* b); int findUniqueInRow(struct board* b); int findUniqueInCol(struct board* b); int findUniqueInBox(struct board* b); int findUniqueBoxInRow(struct board* b); int findUniqueBoxInCol(struct board* b); int findUniqueRowinBox(struct board* b); int findUniqueColinBox(struct board* b); int stripXFromBoxRowsExcept(struct board* b, int x, int boxRow, int boxCol, int row); int stripXFromCell(int x, struct cell* cell); int findMatchedPairInRow(struct board* b); int findMatchedPairInCol(struct board* b); int main(int argc, char* argv[]) { int row = 0; int col = 0; int cellRow = 0; int cellCol = 0; int boxRow = 0; int boxCol = 0; int ifd; int adjustments; int somethingChanged; int oldChange; struct board b; struct cell cells[81]; if (argc != 2) { usage(argv[0]); exit(0); } if ((ifd = open(argv[1], O_RDONLY)) < 0) { perror("Can't open input file."); exit(-1); } initializeCells(&cells); populateBoxesFromCells(&b, &cells); populateRowsFromCells(&b, &cells); populateColsFromCells(&b, &cells); populateFromFile(&b, ifd); printByRows(&b); printCandidateMatrix(&b); adjustments = cullRows(&b); printf("made %d row adjustments.\n", adjustments); printByRows(&b); printCandidateMatrix(&b); adjustments = cullCols(&b); printf("made %d col adjustments.\n", adjustments); printByRows(&b); printCandidateMatrix(&b); adjustments = cullBoxes(&b); printf("made %d box adjustments.\n", adjustments); printf("START\n"); printByRows(&b); printCandidateMatrix(&b); do { somethingChanged = FALSE; oldChange = somethingChanged; somethingChanged += findKnownCellByCandidates(&b); if (somethingChanged != oldChange) { printByRows(&b); printCandidateMatrix(&b); } oldChange = somethingChanged; somethingChanged += findUniqueInRow(&b); if (somethingChanged != oldChange) { printByRows(&b); printCandidateMatrix(&b); } oldChange = somethingChanged; somethingChanged += findUniqueInCol(&b); if (somethingChanged != oldChange) { printByRows(&b); printCandidateMatrix(&b); } oldChange = somethingChanged; somethingChanged += findUniqueInBox(&b); if (somethingChanged != oldChange) { printByRows(&b); printCandidateMatrix(&b); } oldChange = somethingChanged; somethingChanged += findUniqueBoxInRow(&b); if (somethingChanged != oldChange) { printByRows(&b); printCandidateMatrix(&b); } oldChange = somethingChanged; somethingChanged += findUniqueBoxInCol(&b); if (somethingChanged != oldChange) { printByRows(&b); printCandidateMatrix(&b); } oldChange = somethingChanged; somethingChanged += findMatchedPairInRow(&b); if (somethingChanged != oldChange) { printByRows(&b); printCandidateMatrix(&b); } oldChange = somethingChanged; somethingChanged += findMatchedPairInCol(&b); if (somethingChanged != oldChange) { printByRows(&b); printCandidateMatrix(&b); } oldChange = somethingChanged; somethingChanged += findUniqueRowInBox(&b); if (somethingChanged != oldChange) { printByRows(&b); printCandidateMatrix(&b); } oldChange = somethingChanged; somethingChanged += findUniqueColInBox(&b); if (somethingChanged != oldChange) { printByRows(&b); printCandidateMatrix(&b); } oldChange = somethingChanged; adjustments = cullRows(&b); printf("made %d row adjustments.\n", adjustments); somethingChanged += adjustments; adjustments = cullCols(&b); printf("made %d col adjustments.\n", adjustments); somethingChanged += adjustments; adjustments = cullBoxes(&b); printf("made %d box adjustments.\n", adjustments); somethingChanged += adjustments; if (somethingChanged != oldChange) { printByRows(&b); printCandidateMatrix(&b); } } while (somethingChanged); return 0; } void stripXFromBoardRowCol(struct board* b, int x, int row, int col) { int i; int j; int boxRow; int boxCol; //strip from every row at column index for (i = 0; i < ROW_LIMIT; i++) { b->cols[col].cells[i]->candidates[x-1] = 0; } //strip from every column at row index for (i = 0; i < COL_LIMIT; i++) { b->rows[row].cells[i]->candidates[x-1] = 0; } //strip from every element of the box I'm in boxRow = b->rows[row].cells[col]->row/3; boxCol = b->rows[row].cells[col]->collumn/3; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { b->boxes[boxRow][boxCol].cells[i][j]->candidates[x-1] = 0; } } } void stripXFromRow(int x, struct row* row) { int i; for (i=0; i < COL_LIMIT; i++) { row->cells[i]->candidates[x-1] = 0; } } void stripXFromCol(int x, struct col* col) { int i; for (i=0; i < ROW_LIMIT; i++) { col->cells[i]->candidates[x-1] = 0; } } int stripXFromCell(int x, struct cell* cell) { int changes = 0; if (cell->candidates[x-1] != 0) { cell->candidates[x-1] = 0; changes++; } return changes; } void clearAllValues(struct board* b) { int row; int col; for (row = 0; row < ROW_LIMIT; row++) { for (col = 0; col < COL_LIMIT; col++) { b->rows[row].cells[col]->value = 0; } } } void printByRows(struct board* b) { int row; int col; for (row = 0; row < ROW_LIMIT; row++) { for (col = 0; col < COL_LIMIT; col++) { printf("%d ", b->rows[row].cells[col]->value); } printf("\n"); } printf("\n"); } void setBoardRowColX(struct board* b, int row, int col, int x) { int i; b->rows[row].cells[col]->value = x; for (i = 0; i < VAL_LIMIT; i++) { b->rows[row].cells[col]->candidates[i] = 0; } stripXFromBoardRowCol(b, x, row, col); } void setBoardColRowX(struct board* b, int row, int col, int x) { int i; b->cols[col].cells[row]->value = x; for (i = 0; i < VAL_LIMIT; i++) { b->cols[col].cells[row]->candidates[i] = 0; } } void usage(char* progName) { printf("\nUsage:\n\t%s \n\twhere input file is ASCII text representation of start grid in a 9x9 matrix, separated by spaces and carriage returns.\n", progName); } void initializeCells(struct cell* cells) { int boxRow = 0; int boxCol = 0; cells[0].value = 6; cells[0].row = 0; cells[0].collumn = 0; cells[0].box = boxRow * 3 + boxCol; cells[0].candidates[0] = 1; cells[0].candidates[1] = 2; cells[0].candidates[2] = 3; cells[0].candidates[3] = 4; cells[0].candidates[4] = 5; cells[0].candidates[5] = 6; cells[0].candidates[6] = 7; cells[0].candidates[7] = 8; cells[0].candidates[8] = 9; cells[1].value = 4; cells[1].row = 0; cells[1].collumn = 1; cells[1].box = boxRow * 3 + boxCol; cells[1].candidates[0] = 1; cells[1].candidates[1] = 2; cells[1].candidates[2] = 3; cells[1].candidates[3] = 4; cells[1].candidates[4] = 5; cells[1].candidates[5] = 6; cells[1].candidates[6] = 7; cells[1].candidates[7] = 8; cells[1].candidates[8] = 9; cells[2].value = 8; cells[2].row = 0; cells[2].collumn = 2; cells[2].box = boxRow * 3 + boxCol; cells[2].candidates[0] = 1; cells[2].candidates[1] = 2; cells[2].candidates[2] = 3; cells[2].candidates[3] = 4; cells[2].candidates[4] = 5; cells[2].candidates[5] = 6; cells[2].candidates[6] = 7; cells[2].candidates[7] = 8; cells[2].candidates[8] = 9; cells[3].value = 1; cells[3].row = 1; cells[3].collumn = 0; cells[3].box = boxRow * 3 + boxCol; cells[3].candidates[0] = 1; cells[3].candidates[1] = 2; cells[3].candidates[2] = 3; cells[3].candidates[3] = 4; cells[3].candidates[4] = 5; cells[3].candidates[5] = 6; cells[3].candidates[6] = 7; cells[3].candidates[7] = 8; cells[3].candidates[8] = 9; cells[4].value = 5; cells[4].row = 1; cells[4].collumn = 1; cells[4].box = boxRow * 3 + boxCol; cells[4].candidates[0] = 1; cells[4].candidates[1] = 2; cells[4].candidates[2] = 3; cells[4].candidates[3] = 4; cells[4].candidates[4] = 5; cells[4].candidates[5] = 6; cells[4].candidates[6] = 7; cells[4].candidates[7] = 8; cells[4].candidates[8] = 9; cells[5].value = 7; cells[5].row = 1; cells[5].collumn = 2; cells[5].box = boxRow * 3 + boxCol; cells[5].candidates[0] = 1; cells[5].candidates[1] = 2; cells[5].candidates[2] = 3; cells[5].candidates[3] = 4; cells[5].candidates[4] = 5; cells[5].candidates[5] = 6; cells[5].candidates[6] = 7; cells[5].candidates[7] = 8; cells[5].candidates[8] = 9; cells[6].value = 3; cells[6].row = 2; cells[6].collumn = 0; cells[6].box = boxRow * 3 + boxCol; cells[6].candidates[0] = 1; cells[6].candidates[1] = 2; cells[6].candidates[2] = 3; cells[6].candidates[3] = 4; cells[6].candidates[4] = 5; cells[6].candidates[5] = 6; cells[6].candidates[6] = 7; cells[6].candidates[7] = 8; cells[6].candidates[8] = 9; cells[7].value = 2; cells[7].row = 2; cells[7].collumn = 1; cells[7].box = boxRow * 3 + boxCol; cells[7].candidates[0] = 1; cells[7].candidates[1] = 2; cells[7].candidates[2] = 3; cells[7].candidates[3] = 4; cells[7].candidates[4] = 5; cells[7].candidates[5] = 6; cells[7].candidates[6] = 7; cells[7].candidates[7] = 8; cells[7].candidates[8] = 9; cells[8].value = 9; cells[8].row = 2; cells[8].collumn = 2; cells[8].box = boxRow * 3 + boxCol; cells[8].candidates[0] = 1; cells[8].candidates[1] = 2; cells[8].candidates[2] = 3; cells[8].candidates[3] = 4; cells[8].candidates[4] = 5; cells[8].candidates[5] = 6; cells[8].candidates[6] = 7; cells[8].candidates[7] = 8; cells[8].candidates[8] = 9; boxCol = 1; cells[9].value = 7; cells[9].row = 0; cells[9].collumn = 3 * boxCol; cells[9].box = boxRow * 3 + boxCol; cells[9].candidates[0] = 1; cells[9].candidates[1] = 2; cells[9].candidates[2] = 3; cells[9].candidates[3] = 4; cells[9].candidates[4] = 5; cells[9].candidates[5] = 6; cells[9].candidates[6] = 7; cells[9].candidates[7] = 8; cells[9].candidates[8] = 9; cells[10].value = 1; cells[10].row = 0; cells[10].collumn = 3 * boxCol + 1; cells[10].box = boxRow * 3 + boxCol; cells[10].candidates[0] = 1; cells[10].candidates[1] = 2; cells[10].candidates[2] = 3; cells[10].candidates[3] = 4; cells[10].candidates[4] = 5; cells[10].candidates[5] = 6; cells[10].candidates[6] = 7; cells[10].candidates[7] = 8; cells[10].candidates[8] = 9; cells[11].value = 3; cells[11].row = 0; cells[11].collumn = 3 * boxCol + 2; cells[11].box = boxRow * 3 + boxCol; cells[11].candidates[0] = 1; cells[11].candidates[1] = 2; cells[11].candidates[2] = 3; cells[11].candidates[3] = 4; cells[11].candidates[4] = 5; cells[11].candidates[5] = 6; cells[11].candidates[6] = 7; cells[11].candidates[7] = 8; cells[11].candidates[8] = 9; cells[12].value = 9; cells[12].row = 1; cells[12].collumn = 3 * boxCol; cells[12].box = boxRow * 3 + boxCol; cells[12].candidates[0] = 1; cells[12].candidates[1] = 2; cells[12].candidates[2] = 3; cells[12].candidates[3] = 4; cells[12].candidates[4] = 5; cells[12].candidates[5] = 6; cells[12].candidates[6] = 7; cells[12].candidates[7] = 8; cells[12].candidates[8] = 9; cells[13].value = 2; cells[13].row = 1; cells[13].collumn = 3 * boxCol + 1; cells[13].box = boxRow * 3 + boxCol; cells[13].candidates[0] = 1; cells[13].candidates[1] = 2; cells[13].candidates[2] = 3; cells[13].candidates[3] = 4; cells[13].candidates[4] = 5; cells[13].candidates[5] = 6; cells[13].candidates[6] = 7; cells[13].candidates[7] = 8; cells[13].candidates[8] = 9; cells[14].value = 6; cells[14].row = 1; cells[14].collumn = 3 * boxCol + 2; cells[14].box = boxRow * 3 + boxCol; cells[14].candidates[0] = 1; cells[14].candidates[1] = 2; cells[14].candidates[2] = 3; cells[14].candidates[3] = 4; cells[14].candidates[4] = 5; cells[14].candidates[5] = 6; cells[14].candidates[6] = 7; cells[14].candidates[7] = 8; cells[14].candidates[8] = 9; cells[15].value = 8; cells[15].row = 2; cells[15].collumn = 3 * boxCol; cells[15].box = boxRow * 3 + boxCol; cells[15].candidates[0] = 1; cells[15].candidates[1] = 2; cells[15].candidates[2] = 3; cells[15].candidates[3] = 4; cells[15].candidates[4] = 5; cells[15].candidates[5] = 6; cells[15].candidates[6] = 7; cells[15].candidates[7] = 8; cells[15].candidates[8] = 9; cells[16].value = 4; cells[16].row = 2; cells[16].collumn = 3 * boxCol + 1; cells[16].box = boxRow * 3 + boxCol; cells[16].candidates[0] = 1; cells[16].candidates[1] = 2; cells[16].candidates[2] = 3; cells[16].candidates[3] = 4; cells[16].candidates[4] = 5; cells[16].candidates[5] = 6; cells[16].candidates[6] = 7; cells[16].candidates[7] = 8; cells[16].candidates[8] = 9; cells[17].value = 5; cells[17].row = 2; cells[17].collumn = 3 * boxCol + 2; cells[17].box = boxRow * 3 + boxCol; cells[17].candidates[0] = 1; cells[17].candidates[1] = 2; cells[17].candidates[2] = 3; cells[17].candidates[3] = 4; cells[17].candidates[4] = 5; cells[17].candidates[5] = 6; cells[17].candidates[6] = 7; cells[17].candidates[7] = 8; cells[17].candidates[8] = 9; boxCol = 2; cells[18].value = 9; cells[18].row = 3 * boxRow; cells[18].collumn = 3 * boxCol; cells[18].box = boxRow * 3 + boxCol; cells[18].candidates[0] = 1; cells[18].candidates[1] = 2; cells[18].candidates[2] = 3; cells[18].candidates[3] = 4; cells[18].candidates[4] = 5; cells[18].candidates[5] = 6; cells[18].candidates[6] = 7; cells[18].candidates[7] = 8; cells[18].candidates[8] = 9; cells[19].value = 5; cells[19].row = 3 * boxRow; cells[19].collumn = 3 * boxCol + 1; cells[19].box = boxRow * 3 + boxCol; cells[19].candidates[0] = 1; cells[19].candidates[1] = 2; cells[19].candidates[2] = 3; cells[19].candidates[3] = 4; cells[19].candidates[4] = 5; cells[19].candidates[5] = 6; cells[19].candidates[6] = 7; cells[19].candidates[7] = 8; cells[19].candidates[8] = 9; cells[20].value = 2; cells[20].row = 3 * boxRow; cells[20].collumn = 3 * boxCol + 2; cells[20].box = boxRow * 3 + boxCol; cells[20].candidates[0] = 1; cells[20].candidates[1] = 2; cells[20].candidates[2] = 3; cells[20].candidates[3] = 4; cells[20].candidates[4] = 5; cells[20].candidates[5] = 6; cells[20].candidates[6] = 7; cells[20].candidates[7] = 8; cells[20].candidates[8] = 9; cells[21].value = 4; cells[21].row = 3 * boxRow + 1; cells[21].collumn = 3 * boxCol; cells[21].box = boxRow * 3 + boxCol; cells[21].candidates[0] = 1; cells[21].candidates[1] = 2; cells[21].candidates[2] = 3; cells[21].candidates[3] = 4; cells[21].candidates[4] = 5; cells[21].candidates[5] = 6; cells[21].candidates[6] = 7; cells[21].candidates[7] = 8; cells[21].candidates[8] = 9; cells[22].value = 8; cells[22].row = 3 * boxRow + 1; cells[22].collumn = 3 * boxCol + 1; cells[22].box = boxRow * 3 + boxCol; cells[22].candidates[0] = 1; cells[22].candidates[1] = 2; cells[22].candidates[2] = 3; cells[22].candidates[3] = 4; cells[22].candidates[4] = 5; cells[22].candidates[5] = 6; cells[22].candidates[6] = 7; cells[22].candidates[7] = 8; cells[22].candidates[8] = 9; cells[23].value = 3; cells[23].row = 3 * boxRow + 1; cells[23].collumn = 3 * boxCol + 2; cells[23].box = boxRow * 3 + boxCol; cells[23].candidates[0] = 1; cells[23].candidates[1] = 2; cells[23].candidates[2] = 3; cells[23].candidates[3] = 4; cells[23].candidates[4] = 5; cells[23].candidates[5] = 6; cells[23].candidates[6] = 7; cells[23].candidates[7] = 8; cells[23].candidates[8] = 9; cells[24].value = 7; cells[24].row = 3 * boxRow + 2; cells[24].collumn = 3 * boxCol; cells[24].box = boxRow * 3 + boxCol; cells[24].candidates[0] = 1; cells[24].candidates[1] = 2; cells[24].candidates[2] = 3; cells[24].candidates[3] = 4; cells[24].candidates[4] = 5; cells[24].candidates[5] = 6; cells[24].candidates[6] = 7; cells[24].candidates[7] = 8; cells[24].candidates[8] = 9; cells[25].value = 6; cells[25].row = 3 * boxRow + 2; cells[25].collumn = 3 * boxCol + 1; cells[25].box = boxRow * 3 + boxCol; cells[25].candidates[0] = 1; cells[25].candidates[1] = 2; cells[25].candidates[2] = 3; cells[25].candidates[3] = 4; cells[25].candidates[4] = 5; cells[25].candidates[5] = 6; cells[25].candidates[6] = 7; cells[25].candidates[7] = 8; cells[25].candidates[8] = 9; cells[26].value = 1; cells[26].row = 3 * boxRow + 2; cells[26].collumn = 3 * boxCol + 2; cells[26].box = boxRow * 3 + boxCol; cells[26].candidates[0] = 1; cells[26].candidates[1] = 2; cells[26].candidates[2] = 3; cells[26].candidates[3] = 4; cells[26].candidates[4] = 5; cells[26].candidates[5] = 6; cells[26].candidates[6] = 7; cells[26].candidates[7] = 8; cells[26].candidates[8] = 9; boxRow = 1; boxCol = 0; cells[27].value = 5; cells[27].row = 3 * boxRow; cells[27].collumn = 3 * boxCol; cells[27].box = boxRow * 3 + boxCol; cells[27].candidates[0] = 1; cells[27].candidates[1] = 2; cells[27].candidates[2] = 3; cells[27].candidates[3] = 4; cells[27].candidates[4] = 5; cells[27].candidates[5] = 6; cells[27].candidates[6] = 7; cells[27].candidates[7] = 8; cells[27].candidates[8] = 9; cells[28].value = 9; cells[28].row = 3 * boxRow; cells[28].collumn = 3 * boxCol + 1; cells[28].box = boxRow * 3 + boxCol; cells[28].candidates[0] = 1; cells[28].candidates[1] = 2; cells[28].candidates[2] = 3; cells[28].candidates[3] = 4; cells[28].candidates[4] = 5; cells[28].candidates[5] = 6; cells[28].candidates[6] = 7; cells[28].candidates[7] = 8; cells[28].candidates[8] = 9; cells[29].value = 2; cells[29].row = 3 * boxRow; cells[29].collumn = 3 * boxCol + 2; cells[29].box = boxRow * 3 + boxCol; cells[29].candidates[0] = 1; cells[29].candidates[1] = 2; cells[29].candidates[2] = 3; cells[29].candidates[3] = 4; cells[29].candidates[4] = 5; cells[29].candidates[5] = 6; cells[29].candidates[6] = 7; cells[29].candidates[7] = 8; cells[29].candidates[8] = 9; cells[30].value = 8; cells[30].row = 3 * boxRow + 1; cells[30].collumn = 3 * boxCol; cells[30].box = boxRow * 3 + boxCol; cells[30].candidates[0] = 1; cells[30].candidates[1] = 2; cells[30].candidates[2] = 3; cells[30].candidates[3] = 4; cells[30].candidates[4] = 5; cells[30].candidates[5] = 6; cells[30].candidates[6] = 7; cells[30].candidates[7] = 8; cells[30].candidates[8] = 9; cells[31].value = 6; cells[31].row = 3 * boxRow + 1; cells[31].collumn = 3 * boxCol + 1; cells[31].box = boxRow * 3 + boxCol; cells[31].candidates[0] = 1; cells[31].candidates[1] = 2; cells[31].candidates[2] = 3; cells[31].candidates[3] = 4; cells[31].candidates[4] = 5; cells[31].candidates[5] = 6; cells[31].candidates[6] = 7; cells[31].candidates[7] = 8; cells[31].candidates[8] = 9; cells[32].value = 4; cells[32].row = 3 * boxRow + 1; cells[32].collumn = 3 * boxCol + 2; cells[32].box = boxRow * 3 + boxCol; cells[32].candidates[0] = 1; cells[32].candidates[1] = 2; cells[32].candidates[2] = 3; cells[32].candidates[3] = 4; cells[32].candidates[4] = 5; cells[32].candidates[5] = 6; cells[32].candidates[6] = 7; cells[32].candidates[7] = 8; cells[32].candidates[8] = 9; cells[33].value = 7; cells[33].row = 3 * boxRow + 2; cells[33].collumn = 3 * boxCol; cells[33].box = boxRow * 3 + boxCol; cells[33].candidates[0] = 1; cells[33].candidates[1] = 2; cells[33].candidates[2] = 3; cells[33].candidates[3] = 4; cells[33].candidates[4] = 5; cells[33].candidates[5] = 6; cells[33].candidates[6] = 7; cells[33].candidates[7] = 8; cells[33].candidates[8] = 9; cells[34].value = 1; cells[34].row = 3 * boxRow + 2; cells[34].collumn = 3 * boxCol + 1; cells[34].box = boxRow * 3 + boxCol; cells[34].candidates[0] = 1; cells[34].candidates[1] = 2; cells[34].candidates[2] = 3; cells[34].candidates[3] = 4; cells[34].candidates[4] = 5; cells[34].candidates[5] = 6; cells[34].candidates[6] = 7; cells[34].candidates[7] = 8; cells[34].candidates[8] = 9; cells[35].value = 3; cells[35].row = 3 * boxRow + 2; cells[35].collumn = 3 * boxCol + 2; cells[35].box = boxRow * 3 + boxCol; cells[35].candidates[0] = 1; cells[35].candidates[1] = 2; cells[35].candidates[2] = 3; cells[35].candidates[3] = 4; cells[35].candidates[4] = 5; cells[35].candidates[5] = 6; cells[35].candidates[6] = 7; cells[35].candidates[7] = 8; cells[35].candidates[8] = 9; boxCol = 1; cells[36].value = 1; cells[36].row = 3 * boxRow; cells[36].collumn = 3 * boxCol; cells[36].box = boxRow * 3 + boxCol; cells[36].candidates[0] = 1; cells[36].candidates[1] = 2; cells[36].candidates[2] = 3; cells[36].candidates[3] = 4; cells[36].candidates[4] = 5; cells[36].candidates[5] = 6; cells[36].candidates[6] = 7; cells[36].candidates[7] = 8; cells[36].candidates[8] = 9; cells[37].value = 6; cells[37].row = 3 * boxRow; cells[37].collumn = 3 * boxCol + 1; cells[37].box = boxRow * 3 + boxCol; cells[37].candidates[0] = 1; cells[37].candidates[1] = 2; cells[37].candidates[2] = 3; cells[37].candidates[3] = 4; cells[37].candidates[4] = 5; cells[37].candidates[5] = 6; cells[37].candidates[6] = 7; cells[37].candidates[7] = 8; cells[37].candidates[8] = 9; cells[38].value = 4; cells[38].row = 3 * boxRow; cells[38].collumn = 3 * boxCol + 2; cells[38].box = boxRow * 3 + boxCol; cells[38].candidates[0] = 1; cells[38].candidates[1] = 2; cells[38].candidates[2] = 3; cells[38].candidates[3] = 4; cells[38].candidates[4] = 5; cells[38].candidates[5] = 6; cells[38].candidates[6] = 7; cells[38].candidates[7] = 8; cells[38].candidates[8] = 9; cells[39].value = 5; cells[39].row = 3 * boxRow + 1; cells[39].collumn = 3 * boxCol; cells[39].box = boxRow * 3 + boxCol; cells[39].candidates[0] = 1; cells[39].candidates[1] = 2; cells[39].candidates[2] = 3; cells[39].candidates[3] = 4; cells[39].candidates[4] = 5; cells[39].candidates[5] = 6; cells[39].candidates[6] = 7; cells[39].candidates[7] = 8; cells[39].candidates[8] = 9; cells[40].value = 3; cells[40].row = 3 * boxRow + 1; cells[40].collumn = 3 * boxCol + 1; cells[40].box = boxRow * 3 + boxCol; cells[40].candidates[0] = 1; cells[40].candidates[1] = 2; cells[40].candidates[2] = 3; cells[40].candidates[3] = 4; cells[40].candidates[4] = 5; cells[40].candidates[5] = 6; cells[40].candidates[6] = 7; cells[40].candidates[7] = 8; cells[40].candidates[8] = 9; cells[41].value = 7; cells[41].row = 3 * boxRow + 1; cells[41].collumn = 3 * boxCol + 2; cells[41].box = boxRow * 3 + boxCol; cells[41].candidates[0] = 1; cells[41].candidates[1] = 2; cells[41].candidates[2] = 3; cells[41].candidates[3] = 4; cells[41].candidates[4] = 5; cells[41].candidates[5] = 6; cells[41].candidates[6] = 7; cells[41].candidates[7] = 8; cells[41].candidates[8] = 9; cells[42].value = 2; cells[42].row = 3 * boxRow + 2; cells[42].collumn = 3 * boxCol; cells[42].box = boxRow * 3 + boxCol; cells[42].candidates[0] = 1; cells[42].candidates[1] = 2; cells[42].candidates[2] = 3; cells[42].candidates[3] = 4; cells[42].candidates[4] = 5; cells[42].candidates[5] = 6; cells[42].candidates[6] = 7; cells[42].candidates[7] = 8; cells[42].candidates[8] = 9; cells[43].value = 8; cells[43].row = 3 * boxRow + 2; cells[43].collumn = 3 * boxCol + 1; cells[43].box = boxRow * 3 + boxCol; cells[43].candidates[0] = 1; cells[43].candidates[1] = 2; cells[43].candidates[2] = 3; cells[43].candidates[3] = 4; cells[43].candidates[4] = 5; cells[43].candidates[5] = 6; cells[43].candidates[6] = 7; cells[43].candidates[7] = 8; cells[43].candidates[8] = 9; cells[44].value = 9; cells[44].row = 3 * boxRow + 2; cells[44].collumn = 3 * boxCol + 2; cells[44].box = boxRow * 3 + boxCol; cells[44].candidates[0] = 1; cells[44].candidates[1] = 2; cells[44].candidates[2] = 3; cells[44].candidates[3] = 4; cells[44].candidates[4] = 5; cells[44].candidates[5] = 6; cells[44].candidates[6] = 7; cells[44].candidates[7] = 8; cells[44].candidates[8] = 9; boxCol = 2; cells[45].value = 8; cells[45].row = 3 * boxRow; cells[45].collumn = 3 * boxCol; cells[45].box = boxRow * 3 + boxCol; cells[45].candidates[0] = 1; cells[45].candidates[1] = 2; cells[45].candidates[2] = 3; cells[45].candidates[3] = 4; cells[45].candidates[4] = 5; cells[45].candidates[5] = 6; cells[45].candidates[6] = 7; cells[45].candidates[7] = 8; cells[45].candidates[8] = 9; cells[46].value = 3; cells[46].row = 3 * boxRow; cells[46].collumn = 3 * boxCol + 1; cells[46].box = boxRow * 3 + boxCol; cells[46].candidates[0] = 1; cells[46].candidates[1] = 2; cells[46].candidates[2] = 3; cells[46].candidates[3] = 4; cells[46].candidates[4] = 5; cells[46].candidates[5] = 6; cells[46].candidates[6] = 7; cells[46].candidates[7] = 8; cells[46].candidates[8] = 9; cells[47].value = 7; cells[47].row = 3 * boxRow; cells[47].collumn = 3 * boxCol + 2; cells[47].box = boxRow * 3 + boxCol; cells[47].candidates[0] = 1; cells[47].candidates[1] = 2; cells[47].candidates[2] = 3; cells[47].candidates[3] = 4; cells[47].candidates[4] = 5; cells[47].candidates[5] = 6; cells[47].candidates[6] = 7; cells[47].candidates[7] = 8; cells[47].candidates[8] = 9; cells[48].value = 2; cells[48].row = 3 * boxRow + 1; cells[48].collumn = 3 * boxCol; cells[48].box = boxRow * 3 + boxCol; cells[48].candidates[0] = 1; cells[48].candidates[1] = 2; cells[48].candidates[2] = 3; cells[48].candidates[3] = 4; cells[48].candidates[4] = 5; cells[48].candidates[5] = 6; cells[48].candidates[6] = 7; cells[48].candidates[7] = 8; cells[48].candidates[8] = 9; cells[49].value = 1; cells[49].row = 3 * boxRow + 1; cells[49].collumn = 3 * boxCol + 1; cells[49].box = boxRow * 3 + boxCol; cells[49].candidates[0] = 1; cells[49].candidates[1] = 2; cells[49].candidates[2] = 3; cells[49].candidates[3] = 4; cells[49].candidates[4] = 5; cells[49].candidates[5] = 6; cells[49].candidates[6] = 7; cells[49].candidates[7] = 8; cells[49].candidates[8] = 9; cells[50].value = 9; cells[50].row = 3 * boxRow + 1; cells[50].collumn = 3 * boxCol + 2; cells[50].box = boxRow * 3 + boxCol; cells[50].candidates[0] = 1; cells[50].candidates[1] = 2; cells[50].candidates[2] = 3; cells[50].candidates[3] = 4; cells[50].candidates[4] = 5; cells[50].candidates[5] = 6; cells[50].candidates[6] = 7; cells[50].candidates[7] = 8; cells[50].candidates[8] = 9; cells[51].value = 5; cells[51].row = 3 * boxRow + 2; cells[51].collumn = 3 * boxCol; cells[51].box = boxRow * 3 + boxCol; cells[51].candidates[0] = 1; cells[51].candidates[1] = 2; cells[51].candidates[2] = 3; cells[51].candidates[3] = 4; cells[51].candidates[4] = 5; cells[51].candidates[5] = 6; cells[51].candidates[6] = 7; cells[51].candidates[7] = 8; cells[51].candidates[8] = 9; cells[52].value = 4; cells[52].row = 3 * boxRow + 2; cells[52].collumn = 3 * boxCol + 1; cells[52].box = boxRow * 3 + boxCol; cells[52].candidates[0] = 1; cells[52].candidates[1] = 2; cells[52].candidates[2] = 3; cells[52].candidates[3] = 4; cells[52].candidates[4] = 5; cells[52].candidates[5] = 6; cells[52].candidates[6] = 7; cells[52].candidates[7] = 8; cells[52].candidates[8] = 9; cells[53].value = 6; cells[53].row = 3 * boxRow + 2; cells[53].collumn = 3 * boxCol + 2; cells[53].box = boxRow * 3 + boxCol; cells[53].candidates[0] = 1; cells[53].candidates[1] = 2; cells[53].candidates[2] = 3; cells[53].candidates[3] = 4; cells[53].candidates[4] = 5; cells[53].candidates[5] = 6; cells[53].candidates[6] = 7; cells[53].candidates[7] = 8; cells[53].candidates[8] = 9; boxRow = 2; boxCol = 0; cells[54].value = 4; cells[54].row = 3 * boxRow; cells[54].collumn = 3 * boxCol; cells[54].box = boxRow * 3 + boxCol; cells[54].candidates[0] = 1; cells[54].candidates[1] = 2; cells[54].candidates[2] = 3; cells[54].candidates[3] = 4; cells[54].candidates[4] = 5; cells[54].candidates[5] = 6; cells[54].candidates[6] = 7; cells[54].candidates[7] = 8; cells[54].candidates[8] = 9; cells[55].value = 7; cells[55].row = 3 * boxRow; cells[55].collumn = 3 * boxCol + 1; cells[55].box = boxRow * 3 + boxCol; cells[55].candidates[0] = 1; cells[55].candidates[1] = 2; cells[55].candidates[2] = 3; cells[55].candidates[3] = 4; cells[55].candidates[4] = 5; cells[55].candidates[5] = 6; cells[55].candidates[6] = 7; cells[55].candidates[7] = 8; cells[55].candidates[8] = 9; cells[56].value = 1; cells[56].row = 3 * boxRow; cells[56].collumn = 3 * boxCol + 2; cells[56].box = boxRow * 3 + boxCol; cells[56].candidates[0] = 1; cells[56].candidates[1] = 2; cells[56].candidates[2] = 3; cells[56].candidates[3] = 4; cells[56].candidates[4] = 5; cells[56].candidates[5] = 6; cells[56].candidates[6] = 7; cells[56].candidates[7] = 8; cells[56].candidates[8] = 9; cells[57].value = 9; cells[57].row = 3 * boxRow + 1; cells[57].collumn = 3 * boxCol; cells[57].box = boxRow * 3 + boxCol; cells[57].candidates[0] = 1; cells[57].candidates[1] = 2; cells[57].candidates[2] = 3; cells[57].candidates[3] = 4; cells[57].candidates[4] = 5; cells[57].candidates[5] = 6; cells[57].candidates[6] = 7; cells[57].candidates[7] = 8; cells[57].candidates[8] = 9; cells[58].value = 3; cells[58].row = 3 * boxRow + 1; cells[58].collumn = 3 * boxCol + 1; cells[58].box = boxRow * 3 + boxCol; cells[58].candidates[0] = 1; cells[58].candidates[1] = 2; cells[58].candidates[2] = 3; cells[58].candidates[3] = 4; cells[58].candidates[4] = 5; cells[58].candidates[5] = 6; cells[58].candidates[6] = 7; cells[58].candidates[7] = 8; cells[58].candidates[8] = 9; cells[59].value = 6; cells[59].row = 3 * boxRow + 1; cells[59].collumn = 3 * boxCol + 2; cells[59].box = boxRow * 3 + boxCol; cells[59].candidates[0] = 1; cells[59].candidates[1] = 2; cells[59].candidates[2] = 3; cells[59].candidates[3] = 4; cells[59].candidates[4] = 5; cells[59].candidates[5] = 6; cells[59].candidates[6] = 7; cells[59].candidates[7] = 8; cells[59].candidates[8] = 9; cells[60].value = 2; cells[60].row = 3 * boxRow + 2; cells[60].collumn = 3 * boxCol; cells[60].box = boxRow * 3 + boxCol; cells[60].candidates[0] = 1; cells[60].candidates[1] = 2; cells[60].candidates[2] = 3; cells[60].candidates[3] = 4; cells[60].candidates[4] = 5; cells[60].candidates[5] = 6; cells[60].candidates[6] = 7; cells[60].candidates[7] = 8; cells[60].candidates[8] = 9; cells[61].value = 8; cells[61].row = 3 * boxRow + 2; cells[61].collumn = 3 * boxCol + 1; cells[61].box = boxRow * 3 + boxCol; cells[61].candidates[0] = 1; cells[61].candidates[1] = 2; cells[61].candidates[2] = 3; cells[61].candidates[3] = 4; cells[61].candidates[4] = 5; cells[61].candidates[5] = 6; cells[61].candidates[6] = 7; cells[61].candidates[7] = 8; cells[61].candidates[8] = 9; cells[62].value = 5; cells[62].row = 3 * boxRow + 2; cells[62].collumn = 3 * boxCol + 2; cells[62].box = boxRow * 3 + boxCol; cells[62].candidates[0] = 1; cells[62].candidates[1] = 2; cells[62].candidates[2] = 3; cells[62].candidates[3] = 4; cells[62].candidates[4] = 5; cells[62].candidates[5] = 6; cells[62].candidates[6] = 7; cells[62].candidates[7] = 8; cells[62].candidates[8] = 9; boxCol = 1; cells[63].value = 3; cells[63].row = 3 * boxRow; cells[63].collumn = 3 * boxCol; cells[63].box = boxRow * 3 + boxCol; cells[63].candidates[0] = 1; cells[63].candidates[1] = 2; cells[63].candidates[2] = 3; cells[63].candidates[3] = 4; cells[63].candidates[4] = 5; cells[63].candidates[5] = 6; cells[63].candidates[6] = 7; cells[63].candidates[7] = 8; cells[63].candidates[8] = 9; cells[64].value = 5; cells[64].row = 3 * boxRow; cells[64].collumn = 3 * boxCol + 1; cells[64].box = boxRow * 3 + boxCol; cells[64].candidates[0] = 1; cells[64].candidates[1] = 2; cells[64].candidates[2] = 3; cells[64].candidates[3] = 4; cells[64].candidates[4] = 5; cells[64].candidates[5] = 6; cells[64].candidates[6] = 7; cells[64].candidates[7] = 8; cells[64].candidates[8] = 9; cells[65].value = 2; cells[65].row = 3 * boxRow; cells[65].collumn = 3 * boxCol + 2; cells[65].box = boxRow * 3 + boxCol; cells[65].candidates[0] = 1; cells[65].candidates[1] = 2; cells[65].candidates[2] = 3; cells[65].candidates[3] = 4; cells[65].candidates[4] = 5; cells[65].candidates[5] = 6; cells[65].candidates[6] = 7; cells[65].candidates[7] = 8; cells[65].candidates[8] = 9; cells[66].value = 4; cells[66].row = 3 * boxRow + 1; cells[66].collumn = 3 * boxCol; cells[66].box = boxRow * 3 + boxCol; cells[66].candidates[0] = 1; cells[66].candidates[1] = 2; cells[66].candidates[2] = 3; cells[66].candidates[3] = 4; cells[66].candidates[4] = 5; cells[66].candidates[5] = 6; cells[66].candidates[6] = 7; cells[66].candidates[7] = 8; cells[66].candidates[8] = 9; cells[67].value = 7; cells[67].row = 3 * boxRow + 1; cells[67].collumn = 3 * boxCol + 1; cells[67].box = boxRow * 3 + boxCol; cells[67].candidates[0] = 1; cells[67].candidates[1] = 2; cells[67].candidates[2] = 3; cells[67].candidates[3] = 4; cells[67].candidates[4] = 5; cells[67].candidates[5] = 6; cells[67].candidates[6] = 7; cells[67].candidates[7] = 8; cells[67].candidates[8] = 9; cells[68].value = 8; cells[68].row = 3 * boxRow + 1; cells[68].collumn = 3 * boxCol + 2; cells[68].box = boxRow * 3 + boxCol; cells[68].candidates[0] = 1; cells[68].candidates[1] = 2; cells[68].candidates[2] = 3; cells[68].candidates[3] = 4; cells[68].candidates[4] = 5; cells[68].candidates[5] = 6; cells[68].candidates[6] = 7; cells[68].candidates[7] = 8; cells[68].candidates[8] = 9; cells[69].value = 6; cells[69].row = 3 * boxRow + 2; cells[69].collumn = 3 * boxCol; cells[69].box = boxRow * 3 + boxCol; cells[69].candidates[0] = 1; cells[69].candidates[1] = 2; cells[69].candidates[2] = 3; cells[69].candidates[3] = 4; cells[69].candidates[4] = 5; cells[69].candidates[5] = 6; cells[69].candidates[6] = 7; cells[69].candidates[7] = 8; cells[69].candidates[8] = 9; cells[70].value = 9; cells[70].row = 3 * boxRow + 2; cells[70].collumn = 3 * boxCol + 1; cells[70].box = boxRow * 3 + boxCol; cells[70].candidates[0] = 1; cells[70].candidates[1] = 2; cells[70].candidates[2] = 3; cells[70].candidates[3] = 4; cells[70].candidates[4] = 5; cells[70].candidates[5] = 6; cells[70].candidates[6] = 7; cells[70].candidates[7] = 8; cells[70].candidates[8] = 9; cells[71].value = 1; cells[71].row = 3 * boxRow + 2; cells[71].collumn = 3 * boxCol + 2; cells[71].box = boxRow * 3 + boxCol; cells[71].candidates[0] = 1; cells[71].candidates[1] = 2; cells[71].candidates[2] = 3; cells[71].candidates[3] = 4; cells[71].candidates[4] = 5; cells[71].candidates[5] = 6; cells[71].candidates[6] = 7; cells[71].candidates[7] = 8; cells[71].candidates[8] = 9; boxCol = 2; cells[72].value = 6; cells[72].row = 3 * boxRow; cells[72].collumn = 3 * boxCol; cells[72].box = boxRow * 3 + boxCol; cells[72].candidates[0] = 1; cells[72].candidates[1] = 2; cells[72].candidates[2] = 3; cells[72].candidates[3] = 4; cells[72].candidates[4] = 5; cells[72].candidates[5] = 6; cells[72].candidates[6] = 7; cells[72].candidates[7] = 8; cells[72].candidates[8] = 9; cells[73].value = 9; cells[73].row = 3 * boxRow; cells[73].collumn = 3 * boxCol + 1; cells[73].box = boxRow * 3 + boxCol; cells[73].candidates[0] = 1; cells[73].candidates[1] = 2; cells[73].candidates[2] = 3; cells[73].candidates[3] = 4; cells[73].candidates[4] = 5; cells[73].candidates[5] = 6; cells[73].candidates[6] = 7; cells[73].candidates[7] = 8; cells[73].candidates[8] = 9; cells[74].value = 8; cells[74].row = 3 * boxRow; cells[74].collumn = 3 * boxCol + 2; cells[74].box = boxRow * 3 + boxCol; cells[74].candidates[0] = 1; cells[74].candidates[1] = 2; cells[74].candidates[2] = 3; cells[74].candidates[3] = 4; cells[74].candidates[4] = 5; cells[74].candidates[5] = 6; cells[74].candidates[6] = 7; cells[74].candidates[7] = 8; cells[74].candidates[8] = 9; cells[75].value = 1; cells[75].row = 3 * boxRow + 1; cells[75].collumn = 3 * boxCol; cells[75].box = boxRow * 3 + boxCol; cells[75].candidates[0] = 1; cells[75].candidates[1] = 2; cells[75].candidates[2] = 3; cells[75].candidates[3] = 4; cells[75].candidates[4] = 5; cells[75].candidates[5] = 6; cells[75].candidates[6] = 7; cells[75].candidates[7] = 8; cells[75].candidates[8] = 9; cells[76].value = 2; cells[76].row = 3 * boxRow + 1; cells[76].collumn = 3 * boxCol + 1; cells[76].box = boxRow * 3 + boxCol; cells[76].candidates[0] = 1; cells[76].candidates[1] = 2; cells[76].candidates[2] = 3; cells[76].candidates[3] = 4; cells[76].candidates[4] = 5; cells[76].candidates[5] = 6; cells[76].candidates[6] = 7; cells[76].candidates[7] = 8; cells[76].candidates[8] = 9; cells[77].value = 5; cells[77].row = 3 * boxRow + 1; cells[77].collumn = 3 * boxCol + 2; cells[77].box = boxRow * 3 + boxCol; cells[77].candidates[0] = 1; cells[77].candidates[1] = 2; cells[77].candidates[2] = 3; cells[77].candidates[3] = 4; cells[77].candidates[4] = 5; cells[77].candidates[5] = 6; cells[77].candidates[6] = 7; cells[77].candidates[7] = 8; cells[77].candidates[8] = 9; cells[78].value = 3; cells[78].row = 3 * boxRow + 2; cells[78].collumn = 3 * boxCol; cells[78].box = boxRow * 3 + boxCol; cells[78].candidates[0] = 1; cells[78].candidates[1] = 2; cells[78].candidates[2] = 3; cells[78].candidates[3] = 4; cells[78].candidates[4] = 5; cells[78].candidates[5] = 6; cells[78].candidates[6] = 7; cells[78].candidates[7] = 8; cells[78].candidates[8] = 9; cells[79].value = 7; cells[79].row = 3 * boxRow + 2; cells[79].collumn = 3 * boxCol + 1; cells[79].box = boxRow * 3 + boxCol; cells[79].candidates[0] = 1; cells[79].candidates[1] = 2; cells[79].candidates[2] = 3; cells[79].candidates[3] = 4; cells[79].candidates[4] = 5; cells[79].candidates[5] = 6; cells[79].candidates[6] = 7; cells[79].candidates[7] = 8; cells[79].candidates[8] = 9; cells[80].value = 4; cells[80].row = 3 * boxRow + 2; cells[80].collumn = 3 * boxCol + 2; cells[80].box = boxRow * 3 + boxCol; cells[80].candidates[0] = 1; cells[80].candidates[1] = 2; cells[80].candidates[2] = 3; cells[80].candidates[3] = 4; cells[80].candidates[4] = 5; cells[80].candidates[5] = 6; cells[80].candidates[6] = 7; cells[80].candidates[7] = 8; cells[80].candidates[8] = 9; } void populateBoxesFromCells(struct board* b, struct cell* cells) { int boxRow; int boxCol; boxRow = 0; boxCol = 0; b->boxes[boxRow][boxCol].cells[0][0] = &cells[0]; b->boxes[boxRow][boxCol].cells[0][1] = &cells[1]; b->boxes[boxRow][boxCol].cells[0][2] = &cells[2]; b->boxes[boxRow][boxCol].cells[1][0] = &cells[3]; b->boxes[boxRow][boxCol].cells[1][1] = &cells[4]; b->boxes[boxRow][boxCol].cells[1][2] = &cells[5]; b->boxes[boxRow][boxCol].cells[2][0] = &cells[6]; b->boxes[boxRow][boxCol].cells[2][1] = &cells[7]; b->boxes[boxRow][boxCol].cells[2][2] = &cells[8]; boxCol = 1; b->boxes[boxRow][boxCol].cells[0][0] = &cells[9]; b->boxes[boxRow][boxCol].cells[0][1] = &cells[10]; b->boxes[boxRow][boxCol].cells[0][2] = &cells[11]; b->boxes[boxRow][boxCol].cells[1][0] = &cells[12]; b->boxes[boxRow][boxCol].cells[1][1] = &cells[13]; b->boxes[boxRow][boxCol].cells[1][2] = &cells[14]; b->boxes[boxRow][boxCol].cells[2][0] = &cells[15]; b->boxes[boxRow][boxCol].cells[2][1] = &cells[16]; b->boxes[boxRow][boxCol].cells[2][2] = &cells[17]; boxCol = 2; b->boxes[boxRow][boxCol].cells[0][0] = &cells[18]; b->boxes[boxRow][boxCol].cells[0][1] = &cells[19]; b->boxes[boxRow][boxCol].cells[0][2] = &cells[20]; b->boxes[boxRow][boxCol].cells[1][0] = &cells[21]; b->boxes[boxRow][boxCol].cells[1][1] = &cells[22]; b->boxes[boxRow][boxCol].cells[1][2] = &cells[23]; b->boxes[boxRow][boxCol].cells[2][0] = &cells[24]; b->boxes[boxRow][boxCol].cells[2][1] = &cells[25]; b->boxes[boxRow][boxCol].cells[2][2] = &cells[26]; boxRow = 1; boxCol = 0; b->boxes[boxRow][boxCol].cells[0][0] = &cells[27]; b->boxes[boxRow][boxCol].cells[0][1] = &cells[28]; b->boxes[boxRow][boxCol].cells[0][2] = &cells[29]; b->boxes[boxRow][boxCol].cells[1][0] = &cells[30]; b->boxes[boxRow][boxCol].cells[1][1] = &cells[31]; b->boxes[boxRow][boxCol].cells[1][2] = &cells[32]; b->boxes[boxRow][boxCol].cells[2][0] = &cells[33]; b->boxes[boxRow][boxCol].cells[2][1] = &cells[34]; b->boxes[boxRow][boxCol].cells[2][2] = &cells[35]; boxCol = 1; b->boxes[boxRow][boxCol].cells[0][0] = &cells[36]; b->boxes[boxRow][boxCol].cells[0][1] = &cells[37]; b->boxes[boxRow][boxCol].cells[0][2] = &cells[38]; b->boxes[boxRow][boxCol].cells[1][0] = &cells[39]; b->boxes[boxRow][boxCol].cells[1][1] = &cells[40]; b->boxes[boxRow][boxCol].cells[1][2] = &cells[41]; b->boxes[boxRow][boxCol].cells[2][0] = &cells[42]; b->boxes[boxRow][boxCol].cells[2][1] = &cells[43]; b->boxes[boxRow][boxCol].cells[2][2] = &cells[44]; boxCol = 2; b->boxes[boxRow][boxCol].cells[0][0] = &cells[45]; b->boxes[boxRow][boxCol].cells[0][1] = &cells[46]; b->boxes[boxRow][boxCol].cells[0][2] = &cells[47]; b->boxes[boxRow][boxCol].cells[1][0] = &cells[48]; b->boxes[boxRow][boxCol].cells[1][1] = &cells[49]; b->boxes[boxRow][boxCol].cells[1][2] = &cells[50]; b->boxes[boxRow][boxCol].cells[2][0] = &cells[51]; b->boxes[boxRow][boxCol].cells[2][1] = &cells[52]; b->boxes[boxRow][boxCol].cells[2][2] = &cells[53]; boxRow = 2; boxCol = 0; b->boxes[boxRow][boxCol].cells[0][0] = &cells[54]; b->boxes[boxRow][boxCol].cells[0][1] = &cells[55]; b->boxes[boxRow][boxCol].cells[0][2] = &cells[56]; b->boxes[boxRow][boxCol].cells[1][0] = &cells[57]; b->boxes[boxRow][boxCol].cells[1][1] = &cells[58]; b->boxes[boxRow][boxCol].cells[1][2] = &cells[59]; b->boxes[boxRow][boxCol].cells[2][0] = &cells[60]; b->boxes[boxRow][boxCol].cells[2][1] = &cells[61]; b->boxes[boxRow][boxCol].cells[2][2] = &cells[62]; boxCol = 1; b->boxes[boxRow][boxCol].cells[0][0] = &cells[63]; b->boxes[boxRow][boxCol].cells[0][1] = &cells[64]; b->boxes[boxRow][boxCol].cells[0][2] = &cells[65]; b->boxes[boxRow][boxCol].cells[1][0] = &cells[66]; b->boxes[boxRow][boxCol].cells[1][1] = &cells[67]; b->boxes[boxRow][boxCol].cells[1][2] = &cells[68]; b->boxes[boxRow][boxCol].cells[2][0] = &cells[69]; b->boxes[boxRow][boxCol].cells[2][1] = &cells[70]; b->boxes[boxRow][boxCol].cells[2][2] = &cells[71]; boxCol = 2; b->boxes[boxRow][boxCol].cells[0][0] = &cells[72]; b->boxes[boxRow][boxCol].cells[0][1] = &cells[73]; b->boxes[boxRow][boxCol].cells[0][2] = &cells[74]; b->boxes[boxRow][boxCol].cells[1][0] = &cells[75]; b->boxes[boxRow][boxCol].cells[1][1] = &cells[76]; b->boxes[boxRow][boxCol].cells[1][2] = &cells[77]; b->boxes[boxRow][boxCol].cells[2][0] = &cells[78]; b->boxes[boxRow][boxCol].cells[2][1] = &cells[79]; b->boxes[boxRow][boxCol].cells[2][2] = &cells[80]; } void populateFromFile(struct board* b, int ifd) { int row; int col; unsigned char c; for (row = 0; row < ROW_LIMIT; row++) { for (col = 0; col < COL_LIMIT; col++) { do { if (read(ifd, &c, 1) != 1) { perror("Error filling from file."); close(ifd); exit(-1); } } while (!isdigit(c)); b->rows[row].cells[col]->value = (int) (c - '0'); } } } void populateRowsFromCells(struct board* b, struct cell* cells) { b->rows[0].cells[0] = &cells[0]; b->rows[0].cells[1] = &cells[1]; b->rows[0].cells[2] = &cells[2]; b->rows[0].cells[3] = &cells[9]; b->rows[0].cells[4] = &cells[10]; b->rows[0].cells[5] = &cells[11]; b->rows[0].cells[6] = &cells[18]; b->rows[0].cells[7] = &cells[19]; b->rows[0].cells[8] = &cells[20]; b->rows[1].cells[0] = &cells[3]; b->rows[1].cells[1] = &cells[4]; b->rows[1].cells[2] = &cells[5]; b->rows[1].cells[3] = &cells[12]; b->rows[1].cells[4] = &cells[13]; b->rows[1].cells[5] = &cells[14]; b->rows[1].cells[6] = &cells[21]; b->rows[1].cells[7] = &cells[22]; b->rows[1].cells[8] = &cells[23]; b->rows[2].cells[0] = &cells[6]; b->rows[2].cells[1] = &cells[7]; b->rows[2].cells[2] = &cells[8]; b->rows[2].cells[3] = &cells[15]; b->rows[2].cells[4] = &cells[16]; b->rows[2].cells[5] = &cells[17]; b->rows[2].cells[6] = &cells[24]; b->rows[2].cells[7] = &cells[25]; b->rows[2].cells[8] = &cells[26]; b->rows[3].cells[0] = &cells[27]; b->rows[3].cells[1] = &cells[28]; b->rows[3].cells[2] = &cells[29]; b->rows[3].cells[3] = &cells[36]; b->rows[3].cells[4] = &cells[37]; b->rows[3].cells[5] = &cells[38]; b->rows[3].cells[6] = &cells[45]; b->rows[3].cells[7] = &cells[46]; b->rows[3].cells[8] = &cells[47]; b->rows[4].cells[0] = &cells[30]; b->rows[4].cells[1] = &cells[31]; b->rows[4].cells[2] = &cells[32]; b->rows[4].cells[3] = &cells[39]; b->rows[4].cells[4] = &cells[40]; b->rows[4].cells[5] = &cells[41]; b->rows[4].cells[6] = &cells[48]; b->rows[4].cells[7] = &cells[49]; b->rows[4].cells[8] = &cells[50]; b->rows[5].cells[0] = &cells[33]; b->rows[5].cells[1] = &cells[34]; b->rows[5].cells[2] = &cells[35]; b->rows[5].cells[3] = &cells[42]; b->rows[5].cells[4] = &cells[43]; b->rows[5].cells[5] = &cells[44]; b->rows[5].cells[6] = &cells[51]; b->rows[5].cells[7] = &cells[52]; b->rows[5].cells[8] = &cells[53]; b->rows[6].cells[0] = &cells[54]; b->rows[6].cells[1] = &cells[55]; b->rows[6].cells[2] = &cells[56]; b->rows[6].cells[3] = &cells[63]; b->rows[6].cells[4] = &cells[64]; b->rows[6].cells[5] = &cells[65]; b->rows[6].cells[6] = &cells[72]; b->rows[6].cells[7] = &cells[73]; b->rows[6].cells[8] = &cells[74]; b->rows[7].cells[0] = &cells[57]; b->rows[7].cells[1] = &cells[58]; b->rows[7].cells[2] = &cells[59]; b->rows[7].cells[3] = &cells[66]; b->rows[7].cells[4] = &cells[67]; b->rows[7].cells[5] = &cells[68]; b->rows[7].cells[6] = &cells[75]; b->rows[7].cells[7] = &cells[76]; b->rows[7].cells[8] = &cells[77]; b->rows[8].cells[0] = &cells[60]; b->rows[8].cells[1] = &cells[61]; b->rows[8].cells[2] = &cells[62]; b->rows[8].cells[3] = &cells[69]; b->rows[8].cells[4] = &cells[70]; b->rows[8].cells[5] = &cells[71]; b->rows[8].cells[6] = &cells[78]; b->rows[8].cells[7] = &cells[79]; b->rows[8].cells[8] = &cells[80]; } void populateColsFromCells(struct board* b, struct cell* cells) { b->cols[0].cells[0] = &cells[0]; b->cols[0].cells[1] = &cells[3]; b->cols[0].cells[2] = &cells[6]; b->cols[0].cells[3] = &cells[27]; b->cols[0].cells[4] = &cells[30]; b->cols[0].cells[5] = &cells[33]; b->cols[0].cells[6] = &cells[54]; b->cols[0].cells[7] = &cells[57]; b->cols[0].cells[8] = &cells[60]; b->cols[1].cells[0] = &cells[1]; b->cols[1].cells[1] = &cells[4]; b->cols[1].cells[2] = &cells[7]; b->cols[1].cells[3] = &cells[28]; b->cols[1].cells[4] = &cells[31]; b->cols[1].cells[5] = &cells[34]; b->cols[1].cells[6] = &cells[55]; b->cols[1].cells[7] = &cells[58]; b->cols[1].cells[8] = &cells[61]; b->cols[2].cells[0] = &cells[2]; b->cols[2].cells[1] = &cells[5]; b->cols[2].cells[2] = &cells[8]; b->cols[2].cells[3] = &cells[29]; b->cols[2].cells[4] = &cells[32]; b->cols[2].cells[5] = &cells[35]; b->cols[2].cells[6] = &cells[56]; b->cols[2].cells[7] = &cells[59]; b->cols[2].cells[8] = &cells[62]; b->cols[3].cells[0] = &cells[9]; b->cols[3].cells[1] = &cells[12]; b->cols[3].cells[2] = &cells[15]; b->cols[3].cells[3] = &cells[36]; b->cols[3].cells[4] = &cells[39]; b->cols[3].cells[5] = &cells[42]; b->cols[3].cells[6] = &cells[63]; b->cols[3].cells[7] = &cells[66]; b->cols[3].cells[8] = &cells[69]; b->cols[4].cells[0] = &cells[10]; b->cols[4].cells[1] = &cells[13]; b->cols[4].cells[2] = &cells[16]; b->cols[4].cells[3] = &cells[37]; b->cols[4].cells[4] = &cells[40]; b->cols[4].cells[5] = &cells[43]; b->cols[4].cells[6] = &cells[64]; b->cols[4].cells[7] = &cells[67]; b->cols[4].cells[8] = &cells[70]; b->cols[5].cells[0] = &cells[11]; b->cols[5].cells[1] = &cells[14]; b->cols[5].cells[2] = &cells[17]; b->cols[5].cells[3] = &cells[38]; b->cols[5].cells[4] = &cells[41]; b->cols[5].cells[5] = &cells[44]; b->cols[5].cells[6] = &cells[65]; b->cols[5].cells[7] = &cells[68]; b->cols[5].cells[8] = &cells[71]; b->cols[6].cells[0] = &cells[18]; b->cols[6].cells[1] = &cells[21]; b->cols[6].cells[2] = &cells[24]; b->cols[6].cells[3] = &cells[45]; b->cols[6].cells[4] = &cells[48]; b->cols[6].cells[5] = &cells[51]; b->cols[6].cells[6] = &cells[72]; b->cols[6].cells[7] = &cells[75]; b->cols[6].cells[8] = &cells[78]; b->cols[7].cells[0] = &cells[19]; b->cols[7].cells[1] = &cells[22]; b->cols[7].cells[2] = &cells[25]; b->cols[7].cells[3] = &cells[46]; b->cols[7].cells[4] = &cells[49]; b->cols[7].cells[5] = &cells[52]; b->cols[7].cells[6] = &cells[73]; b->cols[7].cells[7] = &cells[76]; b->cols[7].cells[8] = &cells[79]; b->cols[8].cells[0] = &cells[20]; b->cols[8].cells[1] = &cells[23]; b->cols[8].cells[2] = &cells[26]; b->cols[8].cells[3] = &cells[47]; b->cols[8].cells[4] = &cells[50]; b->cols[8].cells[5] = &cells[53]; b->cols[8].cells[6] = &cells[74]; b->cols[8].cells[7] = &cells[77]; b->cols[8].cells[8] = &cells[80]; } int cullRows(struct board* b) { int adjustments = 0; int row; int cell; int cullIndex; int value; int i; for (row = 0; row < ROW_LIMIT; row++) { for (cell = 0; cell < COL_LIMIT; cell++) { value = b->rows[row].cells[cell]->value; if (value != 0) { for (cullIndex =0; cullIndex < ROW_LIMIT; cullIndex++) { if (b->rows[row].cells[cullIndex]->candidates[value - 1] != 0) { b->rows[row].cells[cullIndex]->candidates[value - 1] = 0; adjustments++; } if (cullIndex == cell) { for (i = 0; i < VAL_LIMIT; i++) { b->rows[row].cells[cullIndex]->candidates[i] = 0; } } } } } } return (adjustments); } int cullCols(struct board* b) { int adjustments = 0; int col; int cell; int cullIndex; int value; int i; for (col = 0; col < COL_LIMIT; col++) { for (cell = 0; cell < ROW_LIMIT; cell++) { value = b->cols[col].cells[cell]->value; if (value != 0) { for (cullIndex = 0; cullIndex < ROW_LIMIT; cullIndex++) { if (b->cols[col].cells[cullIndex]->candidates[value - 1] != 0) { b->cols[col].cells[cullIndex]->candidates[value - 1] = 0; adjustments++; } if (cullIndex == cell) { for (i = 0; i < VAL_LIMIT; i++) { b->cols[col].cells[cullIndex]->candidates[i] = 0; } } } } } } return (adjustments); } int cullBoxes(struct board* b) { int boxRow; int boxCol; int row; int col; int iRow; int iCol; int value; int adjustments = 0; for (boxRow = 0; boxRow < 3; boxRow++) { for (boxCol = 0; boxCol < 3; boxCol++) { for (row = 0; row < 3; row++) { for (col = 0; col < 3; col++) { value = b->boxes[boxRow][boxCol].cells[row][col]->value; if (value != 0) { for (iRow = 0; iRow < 3; iRow++) { for (iCol = 0; iCol < 3; iCol++) { if (b->boxes[boxRow][boxCol].cells[iRow][iCol]->candidates[value - 1] != 0) { b->boxes[boxRow][boxCol].cells[iRow][iCol]->candidates[value - 1] = 0; adjustments++; } } } } } } } } return (adjustments); } int findKnownCellByCandidates(struct board* b) { int found = FALSE; int row; int col; int cell; int index; int value; int candidates = 0; int candidateValue; for (row = 0; row < ROW_LIMIT; row++) { for (cell = 0; cell < COL_LIMIT; cell++) { value = b->rows[row].cells[cell]->value; if (value == 0) { candidates = 0; for (index = 0; index < VAL_LIMIT; index++) { if (b->rows[row].cells[cell]->candidates[index] != 0) { candidates++; candidateValue = index + 1; } } if (candidates == 1) { printf("findKnownCellByCandidates: determined value of (%d,%d) to be %d ", row, cell, candidateValue); found = TRUE; setBoardRowColX(b, row, cell, candidateValue); printf("(changed)\n"); } } } } return found; } int findUniqueInRow(struct board* b) { int found = FALSE; int row; int cell; int value; int candidateValue; int tally; int tallyIndex; int cellIndex; for (row = 0; row < ROW_LIMIT; row++) { for (tallyIndex = 0; tallyIndex < VAL_LIMIT; tallyIndex++) { tally = 0; for (cell = 0; cell < COL_LIMIT; cell++) { value = b->rows[row].cells[cell]->value; if (value == 0) { if (b->rows[row].cells[cell]->candidates[tallyIndex] != 0) { tally++; cellIndex = cell; candidateValue = tallyIndex + 1; } } } if (tally == 1) { printf("findUniqueInRow: determined that cell (%d,%d) is %d ", row, cellIndex, candidateValue); found = TRUE; setBoardRowColX(b, row, cellIndex, candidateValue); printf("(changed)\n"); } } } return found; } int findUniqueInCol(struct board* b) { int found = FALSE; int col; int cell; int value; int candidateValue; int tally; int tallyIndex; int cellIndex; for (col = 0; col < COL_LIMIT; col++) { for (tallyIndex = 0; tallyIndex < VAL_LIMIT; tallyIndex++) { tally = 0; for (cell = 0; cell < ROW_LIMIT; cell++) { value = b->cols[col].cells[cell]->value; if (value == 0) { if (b->cols[col].cells[cell]->candidates[tallyIndex] != 0) { tally++; cellIndex = cell; candidateValue = tallyIndex + 1; } } } if (tally == 1) { printf("findUniqueInCol: determined that cell (%d,%d) is %d ", cellIndex, col, candidateValue); found = TRUE; setBoardRowColX(b, cellIndex, col, candidateValue); printf("(changed)\n"); } } } return found; } int findUniqueInBox(struct board* b) { int found = FALSE; int boxRow; int boxCol; int row; int col; int value; int candidateValue; int tally; int tallyIndex; int rowIndex; int colIndex; for (boxRow = 0; boxRow < 3; boxRow++) { for (boxCol = 0; boxCol < 3; boxCol++) { for (tallyIndex = 0; tallyIndex < VAL_LIMIT; tallyIndex++) { tally = 0; for (row = 0; row < 3; row++) { for (col = 0; col < 3; col++) { value = b->boxes[boxRow][boxCol].cells[row][col]->value; if (value == 0) { if (b->boxes[boxRow][boxCol].cells[row][col]->candidates[tallyIndex] != 0) { tally++; rowIndex = row; colIndex = col; candidateValue = tallyIndex + 1; } } } } if (tally == 1) { printf("findUniqueInBox: determined that cell (%d,%d) is %d ", boxRow*3+rowIndex, boxCol*3+colIndex, candidateValue); found = TRUE; setBoardRowColX(b, boxRow*3+rowIndex, boxCol*3+colIndex, candidateValue); printf("(changed)\n"); } } } } return found; } void printCandidateMatrix(struct board* b) { int row; int col; int cullIndex; int value; int candidateIndex; for (row = 0; row < ROW_LIMIT; row++) { for (col = 0; col < COL_LIMIT; col++) { printf("("); for (candidateIndex = 0; candidateIndex < VAL_LIMIT; candidateIndex++) { if (b->rows[row].cells[col]->candidates[candidateIndex] != 0) { printf("%d", b->rows[row].cells[col]->candidates[candidateIndex]); } } printf(")\t"); } printf("\n"); } printf("\n"); return; } int findUniqueBoxInRow(struct board* b) { int row; int col; int colIndex; int tallyIndex; int value; int boxRow; int boxCol; int moreThanOneBox; int candidateValue; int boxes[3]; int sum; int changes = 0; int changed = 0; for (row = 0; row < ROW_LIMIT; row++) { for (tallyIndex = 0; tallyIndex < VAL_LIMIT; tallyIndex++) { boxes[0] = 0; boxes[1] = 0; boxes[2] = 0; sum = 0; for (col = 0; col < COL_LIMIT; col++) { value = b->rows[row].cells[col]->value; if (value == 0) { if (b->rows[row].cells[col]->candidates[tallyIndex] != 0) { boxCol = col/3; boxes[boxCol] = 1; sum = boxes[0] + boxes[1] + boxes[2]; if (sum > 1) { //This one is not unique to this box col = COL_LIMIT; continue; //ungraceful abort for this row } candidateValue = tallyIndex + 1; } } } if (sum == 1) { printf("findUniqueBoxInRow: determined that candidate %d in row %d only occurs in box (%d,%d) ", candidateValue, row, row/3, boxCol); changed = stripXFromBoxRowsExcept(b, candidateValue, row/3, boxCol, row%3); if (changed) { printf("(changes)\n"); } else { printf("(no changes)\n"); } changes += changed; } } } return changes; } int findUniqueBoxInCol(struct board* b) { int row; int col; int colIndex; int tallyIndex; int value; int boxRow; int boxCol; int moreThanOneBox; int candidateValue; int boxes[3]; int sum; int changed = 0; int changes = 0; for (col = 0; col < COL_LIMIT; col++) { for (tallyIndex = 0; tallyIndex < VAL_LIMIT; tallyIndex++) { boxes[0] = 0; boxes[1] = 0; boxes[2] = 0; sum = 0; for (row = 0; row < ROW_LIMIT; row++) { value = b->cols[col].cells[row]->value; if (value == 0) { if (b->cols[col].cells[row]->candidates[tallyIndex] != 0) { boxRow = row/3; boxes[boxRow] = 1; sum = boxes[0] + boxes[1] + boxes[2]; if (sum > 1) { //This one is not unique to this box row = ROW_LIMIT; continue; //ungraceful abort for this row } candidateValue = tallyIndex + 1; } } } if (sum == 1) { printf("findUniqueBoxInCol: determined that candidate %d in col %d only occurs in box (%d,%d) ", candidateValue, col, boxRow, col/3); changed = stripXFromBoxColsExcept(b, candidateValue, boxRow, col/3, col%3); if (changed) { printf("(changes)\n"); } else { printf("(no changes)\n"); } changes += changed; } } } return changes; } int findUniqueRowInBox(struct board* b) { int row; int col; int rowIndex; int tallyIndex; int value; int boxRow; int boxCol; int moreThanOneBox; int candidateValue; int rows[3]; int sum; int changes = 0; int changed = 0; int boxIndex; for(boxRow = 0; boxRow < 3; boxRow++) { for (boxCol = 0; boxCol < 3; boxCol++) { for (tallyIndex = 0; tallyIndex < VAL_LIMIT; tallyIndex++) { rows[0] = 0; rows[1] = 0; rows[2] = 0; for (col = 0; col < 3; col++) { if (b->boxes[boxRow][boxCol].cells[0][col]->candidates[tallyIndex] != 0) { rows[0] = 1; rowIndex = 0; } if (b->boxes[boxRow][boxCol].cells[1][col]->candidates[tallyIndex] != 0) { rows[1] = 1; rowIndex = 1; } if (b->boxes[boxRow][boxCol].cells[2][col]->candidates[tallyIndex] != 0) { rows[2] = 1; rowIndex = 2; } } sum = rows[0] + rows[1] + rows[2]; if (sum == 1) { changed = 0; printf("%d only exists in row %d of box (%d,%d) ", tallyIndex+1, rowIndex, boxRow, boxCol); //so now I can remove this tallyIndex from the same row, outside //this box. for (boxIndex = 0; boxIndex < 3; boxIndex++) { if (boxIndex == boxCol) { continue; } changed += stripXFromCell(tallyIndex+1, b->boxes[boxRow][boxIndex].cells[rowIndex][0]); changed += stripXFromCell(tallyIndex+1, b->boxes[boxRow][boxIndex].cells[rowIndex][1]); changed += stripXFromCell(tallyIndex+1, b->boxes[boxRow][boxIndex].cells[rowIndex][2]); } if (changed) { printf("(changes)\n"); changes += changed; } else { printf("(no changes)\n"); } //printCandidateMatrix(b); } } } } return changes; } int findUniqueColInBox(struct board* b) { int row; int col; int colIndex; int tallyIndex; int value; int boxRow; int boxCol; int moreThanOneBox; int candidateValue; int cols[3]; int sum; int changes = 0; int changed = 0; int boxIndex; for(boxRow = 0; boxRow < 3; boxRow++) { for (boxCol = 0; boxCol < 3; boxCol++) { for (tallyIndex = 0; tallyIndex < VAL_LIMIT; tallyIndex++) { cols[0] = 0; cols[1] = 0; cols[2] = 0; for (row = 0; row < 3; row++) { if (b->boxes[boxRow][boxCol].cells[row][0]->candidates[tallyIndex] != 0) { cols[0] = 1; colIndex = 0; } if (b->boxes[boxRow][boxCol].cells[row][1]->candidates[tallyIndex] != 0) { cols[1] = 1; colIndex = 1; } if (b->boxes[boxRow][boxCol].cells[row][2]->candidates[tallyIndex] != 0) { cols[2] = 1; colIndex = 2; } } sum = cols[0] + cols[1] + cols[2]; if (sum == 1) { changed = 0; printf("%d only exists in col %d of box (%d,%d) ", tallyIndex+1, colIndex, boxRow, boxCol); //so now I can remove this tallyIndex from the same col, outside //this box. for (boxIndex = 0; boxIndex < 3; boxIndex++) { if (boxIndex == boxRow) { continue; } changed += stripXFromCell(tallyIndex+1, b->boxes[boxIndex][boxCol].cells[0][colIndex]); changed += stripXFromCell(tallyIndex+1, b->boxes[boxIndex][boxCol].cells[1][colIndex]); changed += stripXFromCell(tallyIndex+1, b->boxes[boxIndex][boxCol].cells[2][colIndex]); } if (changed) { printf("(changes)\n"); changes += changed; } else { printf("(no changes)\n"); } //printCandidateMatrix(b); } } } } return changes; } int stripXFromBoxRowsExcept(struct board* b, int x, int boxRow, int boxCol, int row) { int i; int j; int changes = FALSE; for (i = 0; i < 3; i++) { if (i == row) { continue; } for (j=0; j < 3; j++) { if (b->boxes[boxRow][boxCol].cells[i][j]->candidates[x-1] != 0) { b->boxes[boxRow][boxCol].cells[i][j]->candidates[x-1] = 0; changes++; } } } return changes; } int stripXFromBoxColsExcept(struct board* b, int x, int boxRow, int boxCol, int col) { int i; int j; int changes = FALSE; for (i = 0; i < 3; i++) { for (j=0; j < 3; j++) { if (j == col) { continue; } if (b->boxes[boxRow][boxCol].cells[i][j]->candidates[x-1] != 0) { b->boxes[boxRow][boxCol].cells[i][j]->candidates[x-1] = 0; changes++; } } } return changes; } int findMatchedPairInRow(struct board* b) { int changes = 0; int row; int col; int tallyIndex; int tally; int candidates[ROW_LIMIT]; int colSighting[VAL_LIMIT][ROW_LIMIT]; int locations[ROW_LIMIT]; int pairIndex = 0; int numPairs = 0; int lo; int hi; int i; for (row = 0; row < ROW_LIMIT; row++) { candidates[0] = 0; candidates[1] = 0; candidates[2] = 0; candidates[3] = 0; candidates[4] = 0; candidates[5] = 0; candidates[6] = 0; candidates[7] = 0; candidates[8] = 0; pairIndex = 0; for (tallyIndex = 0; tallyIndex < VAL_LIMIT; tallyIndex++) { tally = 0; for (col = 0; col < COL_LIMIT; col++) { if (b->rows[row].cells[col]->candidates[tallyIndex] != 0) { locations[tally] = col; tally++; } } if (tally == 2) { colSighting[pairIndex][0] = locations[0]; colSighting[pairIndex][1] = locations[1]; candidates[pairIndex] = tallyIndex; pairIndex++; } } if (pairIndex > 1) { for (lo = 0; lo < pairIndex-1; lo++) { for (hi = lo + 1; hi < pairIndex; hi++) { if ((colSighting[lo][0] == colSighting[hi][0]) && (colSighting[lo][1] == colSighting[hi][1])) { printf("Found a matching pair [%d, %d] in row %d at (%d,%d) and (%d, %d)\n", candidates[lo]+1, candidates[hi]+1, row, row, colSighting[lo][0], row, colSighting[lo][1]); numPairs++; for (col = 0; col < COL_LIMIT; col++) { if ((col == colSighting[lo][0]) || (col == colSighting[lo][1])) { for (i = 0; i < VAL_LIMIT; i++) { if (((i + 1) == (candidates[lo] + 1)) || ((i + 1) == (candidates[hi] + 1))) { continue; } changes += stripXFromCell(i + 1, b->rows[row].cells[col]); } continue; } changes += stripXFromCell(candidates[lo]+1, b->rows[row].cells[col]); changes += stripXFromCell(candidates[hi]+1, b->rows[row].cells[col]); } printCandidateMatrix(b); } } } } } printf("returning %d\n", changes); return changes; } int findMatchedPairInCol(struct board* b) { int changes = 0; int row; int col; int tallyIndex; int tally; int candidates[ROW_LIMIT]; int rowSighting[VAL_LIMIT][ROW_LIMIT]; int locations[ROW_LIMIT]; int pairIndex = 0; int numPairs = 0; int lo; int hi; int i; for (col = 0; col < COL_LIMIT; col++) { candidates[0] = 0; candidates[1] = 0; candidates[2] = 0; candidates[3] = 0; candidates[4] = 0; candidates[5] = 0; candidates[6] = 0; candidates[7] = 0; candidates[8] = 0; pairIndex = 0; for (tallyIndex = 0; tallyIndex < VAL_LIMIT; tallyIndex++) { tally = 0; for (row = 0; row < ROW_LIMIT; row++) { if (b->cols[col].cells[row]->candidates[tallyIndex] != 0) { locations[tally] = row; tally++; } } if (tally == 2) { rowSighting[pairIndex][0] = locations[0]; rowSighting[pairIndex][1] = locations[1]; candidates[pairIndex] = tallyIndex; pairIndex++; } } if (pairIndex > 1) { for (lo = 0; lo < pairIndex-1; lo++) { for (hi = lo + 1; hi < pairIndex; hi++) { if ((rowSighting[lo][0] == rowSighting[hi][0]) && (rowSighting[lo][1] == rowSighting[hi][1])) { printf("Found a matching pair [%d, %d] in collumn %d at (%d,%d) and (%d, %d)\n", candidates[lo]+1, candidates[hi]+1, col, rowSighting[lo][0],col, rowSighting[lo][1], col); numPairs++; for (row = 0; row < ROW_LIMIT; row++) { if ((row == rowSighting[lo][0]) || (row == rowSighting[lo][1])) { for (i = 0; i < VAL_LIMIT; i++) { if (((i + 1) == (candidates[lo] + 1)) || ((i + 1) == (candidates[hi] + 1))) { continue; } changes += stripXFromCell(i + 1, b->cols[col].cells[row]); } continue; } changes += stripXFromCell(candidates[lo]+1, b->cols[col].cells[row]); changes += stripXFromCell(candidates[hi]+1, b->cols[col].cells[row]); } printCandidateMatrix(b); } } } } } return changes; } int countCandidates(struct cell* cell) { int i; int candidates = 0; for (i = 0; i < VAL_LIMIT; i++) { if (cell->candidates[i] != 0) { candidates++; } } return candidates; }