Compare commits

..

2 Commits

Author SHA1 Message Date
86ea672173 check solution 2024-08-23 22:08:25 +02:00
97358e6797 correct undef values in 2nd example 2024-08-20 20:14:00 +02:00

View File

@ -14,15 +14,15 @@
// https://en.wikipedia.org/wiki/Sudoku#/media/File:Sudoku_Puzzle_by_L2G-20050714_standardized_layout.svg // https://en.wikipedia.org/wiki/Sudoku#/media/File:Sudoku_Puzzle_by_L2G-20050714_standardized_layout.svg
/* /*
int sudoku[9][9] = { int sudoku[9][9] = {
{5,3,0,6,0,0,0,9,8}, {5,3,VALUE_UNDEFINED,6,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,9,8},
{0,7,0,1,9,5,0,0,0}, {VALUE_UNDEFINED,7,VALUE_UNDEFINED,1,9,5,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED},
{0,0,0,0,0,0,0,6,0}, {VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,6,VALUE_UNDEFINED},
{8,0,0,4,0,0,7,0,0}, {8,VALUE_UNDEFINED,VALUE_UNDEFINED,4,VALUE_UNDEFINED,VALUE_UNDEFINED,7,VALUE_UNDEFINED,VALUE_UNDEFINED},
{0,6,0,8,0,3,0,2,0}, {VALUE_UNDEFINED,6,VALUE_UNDEFINED,8,VALUE_UNDEFINED,3,VALUE_UNDEFINED,2,VALUE_UNDEFINED},
{0,0,3,0,0,1,0,0,6}, {VALUE_UNDEFINED,VALUE_UNDEFINED,3,VALUE_UNDEFINED,VALUE_UNDEFINED,1,VALUE_UNDEFINED,VALUE_UNDEFINED,6},
{0,6,0,0,0,0,0,0,0}, {VALUE_UNDEFINED,6,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED},
{0,0,0,4,1,9,0,8,0}, {VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,4,1,9,VALUE_UNDEFINED,8,VALUE_UNDEFINED},
{2,8,0,0,0,5,0,7,9} {2,8,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,5,VALUE_UNDEFINED,7,9}
}; };
*/ */
@ -231,6 +231,38 @@ void setValue(int q, int f, int val) {
//printDumpSudoku(); //printDumpSudoku();
} }
bool checkSolution() {
bool foundError = false;
for (int val = 1; val <= 9; val++) {
// check for val every quadrat
for (int q = 0; q < 9; q++) {
if (!isInQuadrat(q, val)) {
foundError = true;
fprintf(stderr, "Value %d not found in Q %d\n", val, q);
}
}
// check for val in every row
for (int r = 0; r < 9; r++) {
if (!isInRow(r, val)) {
foundError = true;
fprintf(stderr, "Value %d not found in row %d\n", val, r);
}
}
// check for val in every col
for (int c = 0; c < 9; c++) {
if (!isInCol(c, val)) {
foundError = true;
fprintf(stderr, "Value %d not found in col %d\n", val, c);
}
}
}
return !foundError;
}
int main() { int main() {
printDumpSudoku(); printDumpSudoku();
bool somethingChanged; bool somethingChanged;
@ -302,4 +334,6 @@ int main() {
} while (somethingChanged); } while (somethingChanged);
printDumpSudoku(); printDumpSudoku();
checkSolution();
} }