2D array assignment Javascript

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.

3 thoughts on “2D array assignment Javascript”

  1. let grid = [
      [0, 0, 5, 0, 7, 8, 0, 0, 9],
      [0, 8, 0, 0, 0, 0, 0, 0, 0],
      [2, 7, 0, 0, 0, 0, 1, 0, 0],
      [0, 0, 0, 0, 0, 0, 2, 4, 3],
      [1, 9, 0, 7, 0, 0, 0, 0, 5],
      [0, 0, 0, 3, 0, 0, 0, 0, 0],
      [0, 4, 0, 0, 0, 1, 0, 0, 0],
      [0, 0, 0, 8, 0, 5, 0, 0, 7],
      [3, 0, 0, 0, 0, 0, 9, 0, 0]
    ];
    
    function displaySodoku() {
      console.log(grid);
    }
    
    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();
        return;
      }
    
      // this happens when the grid is at the final row, and needs to go down a row
      if (x > 8) {
        solveSudokuHelper(0, y + 1);
        return;
      }
    
      if (grid[y][x] == 0) {
        for (let i = 1; i < 10; i++) {
          if (isValid(x, y, i)) {
            grid[y][x] = i;
            solveSudokuHelper(x + 1, y);
            grid[y][x] = 0;
          }
        }
      } else solveSudokuHelper(x + 1, y);
      return;
    }
    
    function isValid(x, y, num) {
      // 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();

    Try this code

    Reply

Leave a Comment