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);
Assuming that this is for the 16x16 Sudoku variant since the input in the OP is only 4x4 -- going along with this assumption then you need to find all of the blanks (represented as a 0) in a box. In the following example, function blankMap(box) takes a 2D array of any size and returns an array of pairs. An array of pairs is a 2D array of N rows and 2 colunms. In each pair (or sub-array) is the location of a 0 -- the first colunm (Array[N][0]) represents the index number of a row, the second colunm (Array[N][1]) represents the index number of a column.
| INPUT | OUTPUT |
|---|---|
| const box9 = [ | const box9X = [ |
| [0, 12, 0, 9], | [0, 0], [0, 2], |
| [16, 3, 0, 8], | [1, 2], |
| [0, 0, 0, 0], | [2, 0], [2, 1], [2, 2], [2, 3], |
| [10, 5, 2, 0] | [3, 3] |
| ]; | ]; |
/* INPUT
A 4 row by 4 colunm table represents 1 of a total of 16 sub-tables.
*/
const box0 = [
[0, 5, 13, 0],
[10, 0, 0, 0],
[7, 11, 3, 0],
[0, 0, 0, 0]
];
const box13 = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
];
const blankMap = box =>
box.flatMap((arr, row) => // 1st grab each sub-array (~arr~)
arr.flatMap((num, col) => // On each ~arr~,...
num === 0 ? [[row, col]] : [])); /* ...if it's a ~0~ ~?~ then return the position in double
brackets: ~[[row, col]]~ otherwise ~:~ return an empty array ~[]~.*/
// A utility to verify if a box is completed
const isDone = array => array.length < 1;
console.log('box0=-=-=-=-=-=-=-=-=-=-=-=-=');
let b0 = blankMap(box0);
console.log(JSON.stringify(b0));
console.log('box0 is complete: '+isDone(b0));
console.log('box13-=-=-=-=-=-=-=-=-=-=-=-=');
let b13 = blankMap(box13);
console.log(JSON.stringify(b13));
console.log('box13 is complete: '+isDone(b13));
The .flatMap() method was used twice: once to iterate through the outer array and grab the sub-arrays, and once to iterate through each sub-array to find the 0s. .flatMap() is the .map() and .flat() methods combined so if you want the return as a 2D array, wrap it in brackets (double if another .flatMap() is working on the same array). Inversely, if you want to totally ignore an iteration then return an empty array [].
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);