I am trying to make a sudoku solver. the code works, However when there are no more 0(zeros) in the table that means my sudoku table is solved. if the sudoku table has been solve I want everything to stop running. The problem is I have not found a good way to stop program from running when it is solved. I am open to suggestions.
the case that is supposed to stop the program from running is function find_empty_space(table), it is the first if condition, however in the function sudoku_solver(table) it calls var values = find_empty_space(table); and since I don’t return anything if this case occurs an error happens.
I tried adding a if condition below var values = find_empty_space(table); where values == null then nothing happens so the function can stop but that for some reason ruins the sudoku solver from working. if anyone has another idea how to stop my program and all other functions after condition is meet please lmk
var sudoku_1 = [
[0, 0, 0, 0],
[1, 0, 2, 0],
[0, 1, 4, 0],
[2, 0, 0, 1],
];
var table = sudoku_1;
function find_empty_space(table) {
if (
table[0].indexOf(0) == -1 &&
table[1].indexOf(0) == -1 &&
table[2].indexOf(0) == -1 &&
table[3].indexOf(0) == -1
) {
console.log("Sudoku Solver has solved your table");
var solved_table = table;
} else {
for (var r = 0; r < 5; r++) {
for (var c = 0; c < 5; c++) {
console.log(`row: ${r}`); //!for testing
console.log(`column: ${c}`); //!for testing
// if object in array is equal to 0 then it means the space is empty
if (table[r][c] == 0) {
return [r, c];
}
}
}
}
}
function check_if_number_can_go_in_position(table, n, r, c) {
console.log("function check_if_number_can_go_in_position()");
console.log(`row ${table[r]}`);
// var below makes a array of tables column that is need to search for n
var column_c = table.map((d) => d[c]);
console.log(`col ${column_c}`);
if (table[r].indexOf(n) != -1) {
console.log("backtrack r");
return false;
}
if (column_c.indexOf(n) != -1) {
console.log("backtrack c");
return false;
}
return true;
}
// this is the main function
function sudoku_solver(table) {
var values = find_empty_space(table);
console.log(values);
var r = values[0];
var c = values[1];
console.log("in one");
for (var n = 1; n < 5; n++) {
console.log(`n = ${n}`);
if (check_if_number_can_go_in_position(table, n, r, c) == true) {
table[r][c] = n;
console.table(table);
sudoku_solver(table);
}
}
table[r][c] = 0;
}
sudoku_solver(table);
>Solution :
Marked the added code
function find_empty_space(table) {
// function goes from right to left of table finding every empty space, empty space == 0,
//for loops go through 1-4
// var meanings r = row, c = column
if (
table[0].indexOf(0) == -1 &&
table[1].indexOf(0) == -1 &&
table[2].indexOf(0) == -1 &&
table[3].indexOf(0) == -1
) {
console.log("Sudoku Solver has solved your table");
var solved_table = table;
return true; // ***** added this
} else {
for (var r = 0; r < 5; r++) {
for (var c = 0; c < 5; c++) {
console.log(`row: ${r}`); //!for testing
console.log(`column: ${c}`); //!for testing
// if object in array is equal to 0 then it means the space is empty
if (table[r][c] == 0) {
return [r, c];
}
}
}
}
}
// this is the main function
function sudoku_solver(table) {
// contains all other sub functions this is the main function
//r = row, c = column
// function goes from right to left of table finding every empty space, empty
var values = find_empty_space(table);
if (values === true) return true; // ***** added this
console.log(values);
var r = values[0];
var c = values[1];
console.log("in one");
for (var n = 1; n < 5; n++) {
console.log(`n = ${n}`);
if (check_if_number_can_go_in_position(table, n, r, c) == true) {
table[r][c] = n;
console.table(table);
if (sudoku_solver(table) === true) // ***** changed this
return true; // ***** added this
}
}
table[r][c] = 0;
}
sudoku_solver(table);