So I was following along with a Computerphile video about writing a sudoku solver and managed to get the code working in python, here's what I got:
def possible(grid, y, x, n):
for i in range(0,9):
if grid[y][i] == n:
return False
if grid[i][x] == n:
return False
x0 = (x//3)*3
y0 = (y//3)*3
for i in range(0,3):
for j in range(0,3):
if grid[y0+i][x0+j] == n:
return False
return True
def solver(grid):
for y in range(9):
for x in range(9):
if grid[y][x] == 0:
for n in range(1,10):
if possible(grid, y, x, n):
grid[y][x] = n
solver(grid)
grid[y][x] = 0
return
print(np.matrix(grid))
This code works properly. But I wanted to try to get this working in a web application, so I translated it to Javascript like this:
function possible(board, y, x, n) {
for(i = 0; i < 9; i++) {
if(board[y][i] === n) {
return false;
}
if(board[i][x] === n) {
return false;
}
}
y0 = Math.floor(y/3)*3;
x0 = Math.floor(x/3)*3;
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
if(board[i+y0][j+x0] === n) {
return false;
}
}
}
return true;
}
function solver(board) {
for(y = 0; y < 9; y++) {
for(x = 0; x < 9; x++) {
if(board[y][x] === 0) {
for(n = 1; n < 10; n++) {
if(possible(board, y, x, n)) {
board[y][x] = n;
solver(board);
board[y][x] = 0;
}
}
return;
}
}
}
console.log(board);
}
As far as I can see, these functions are exactly the same, and since it works in Python I can't figure out why it doesn't work in JavaScript. In JS, it seems that it never even reaches the console.log(board) line, so I'm wandering if I'm hitting max recursion depth or something simple like that.
For reference, here is a grid that works in the Python solver:
grid = [
[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 9, 5, 0, 0, 0],
[0, 9, 8, 0, 0, 0, 0, 6, 0],
[8, 0, 0, 0, 6, 0, 0, 0, 3],
[4, 0, 0, 8, 0, 3, 0, 0, 1],
[7, 0, 0, 0, 2, 0, 0, 0, 6],
[0, 6, 0, 0, 0, 0, 2, 8, 0],
[0, 0, 0, 4, 1, 9, 0, 0, 5],
[0, 0, 0, 0, 8, 0, 0, 7, 9]
]