let grid = [
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
]
function solveSudoku(){
console.log("started")
solveSudokuHelper(0,0)
console.log(grid)
}
function solveSudokuHelper(x,y){
// this happens when the grid is solved
if (y>8){
console.log("init finishing process")
displaySodoku(grid)
}
// this happens when the grid is at the final row, and needs to go down a row
if (x>8){
solveSudokuHelper(0,y+1,grid)
return
}
if (grid[y][x] == 0){
for (i = 1; i < 10; i++){
if (isValid(x, y, i, grid)){
console.log(grid, 1, i, y, x)
grid[y][x] = i
console.log(grid,2)
solveSudokuHelper(x+1, y, grid)
grid[y][x] = 0
}
}
}
else{
solveSudokuHelper(x+1, y, grid)
}
return
}
function isValid(x, y, num, grid){
// row checking
for (col = 0; col < 9; col++){
if (num == grid[y][col]){
return(false)
}
}
// column checking
for (row = 0; row < 9; row++){
if (num == grid[row][x]){
return(false)
}
}
// square checking
startRow = y - (y % 3)
startCol = x - (x % 3)
row = startRow
while (row <= startRow + 2){
col = startCol
while (col <= startCol + 2){
if (grid[row][col] == num){
return(false)
}
col += 1
}
row += 1
}
return(true)
}
solveSudoku();
I am trying to make something to solve a soduku, but the array assignment of values is not working. I have surrounded it with console.log statements, and I can’t seem to get anywhere. This occurs at line 48.
Something to note is that I already devised this algorithm in python, so unless the console.log statements are lying to me I have narrowed it down to this.
Python code: https://github.com/owenmoogk/sudoku-bot/blob/main/main.py
My output is just the original array repeated a bunch of times. Thanks in advance.
Thanks, I just forgot ‘let’ in my forloop and the variable scope was being messed up. Thanks!
Try this code
payday loan companies national payday loan payday loan 1000 speedy cash