Compare commits

...

4 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
8f387ff781 more readable output 2024-08-20 20:11:58 +02:00
370d7516f5 removed unused code 2024-08-20 20:04:31 +02:00

View File

@ -14,15 +14,15 @@
// https://en.wikipedia.org/wiki/Sudoku#/media/File:Sudoku_Puzzle_by_L2G-20050714_standardized_layout.svg
/*
int sudoku[9][9] = {
{5,3,0,6,0,0,0,9,8},
{0,7,0,1,9,5,0,0,0},
{0,0,0,0,0,0,0,6,0},
{8,0,0,4,0,0,7,0,0},
{0,6,0,8,0,3,0,2,0},
{0,0,3,0,0,1,0,0,6},
{0,6,0,0,0,0,0,0,0},
{0,0,0,4,1,9,0,8,0},
{2,8,0,0,0,5,0,7,9}
{5,3,VALUE_UNDEFINED,6,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,9,8},
{VALUE_UNDEFINED,7,VALUE_UNDEFINED,1,9,5,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED},
{VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,6,VALUE_UNDEFINED},
{8,VALUE_UNDEFINED,VALUE_UNDEFINED,4,VALUE_UNDEFINED,VALUE_UNDEFINED,7,VALUE_UNDEFINED,VALUE_UNDEFINED},
{VALUE_UNDEFINED,6,VALUE_UNDEFINED,8,VALUE_UNDEFINED,3,VALUE_UNDEFINED,2,VALUE_UNDEFINED},
{VALUE_UNDEFINED,VALUE_UNDEFINED,3,VALUE_UNDEFINED,VALUE_UNDEFINED,1,VALUE_UNDEFINED,VALUE_UNDEFINED,6},
{VALUE_UNDEFINED,6,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED},
{VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,4,1,9,VALUE_UNDEFINED,8,VALUE_UNDEFINED},
{2,8,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,5,VALUE_UNDEFINED,7,9}
};
*/
@ -88,27 +88,6 @@ void printDumpSudoku() {
printf("+-------+-------+-------+\n");
}
void printDumpQuadrat(int q[]) {
for (int row = 0; row < 3; row++) {
if (row % 3 == 0) {
printf("+-------+\n");
}
for (int col = 0; col < 3; col++) {
if (col % 3 == 0) {
if (col != 0) {
printf(" ");
}
printf("|");
}
int f = calculateField(row, col);
printf(" %d", q[f]);
}
printf(" |\n");
}
printf("+-------+\n");
}
// checks if this value is present anywhere in the row
bool isInRow(int row, int val) {
for (int col = 0; col < 9; col++) {
@ -249,7 +228,39 @@ bool isInQuadrat(int q, int val) {
void setValue(int q, int f, int val) {
sudoku[q][f] = val;
printf("=> setting %d in Q %d; F %d\n", val, q, f);
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() {
@ -305,7 +316,6 @@ int main() {
}
// check if only one field is left undefined -> hast to be the one special value
int undefinedFound = VALUE_UNDEFINED;
for (int f = 0; f < 9; f++) {
// keep in mind that sudoku array might have changed since evaluation
// -> ignore already set values
@ -323,5 +333,7 @@ int main() {
}
} while (somethingChanged);
// next step: muss in Feld X sein, weil alle anderen Felder irgendwie anders belegt sein müssen (aber noch nicht sind)
printDumpSudoku();
checkSolution();
}