Home > Software engineering >  My 2d array is not updating even after assignment
My 2d array is not updating even after assignment

Time:10-25

After debugging for more than an hour, I found the problem in my code. I do not understand why it updates board[y][x] but not board itself. Here is the code:

puzzle = [
    [0, 0, 7, 0, 0, 6, 2, 0, 0],
    [1, 0, 0, 8, 9, 0, 7, 6, 0],
    [8, 0, 2, 0, 7, 3, 0, 5, 0],
    [0, 0, 0, 1, 0, 9, 5, 0, 6],
    [0, 1, 0, 0, 0, 0, 0, 8, 0],
    [7, 0, 3, 6, 0, 8, 0, 0, 0],
    [0, 7, 0, 2, 4, 0, 6, 0, 3],
    [0, 4, 1, 0, 6, 7, 0, 0, 5],
    [0, 0, 9, 3, 0, 0, 1, 0, 0]
];

function find_empty(board) {
    for (let y = 0; y < 9;   y) {
        for (let x = 0; x < 9;   x) {
            if (board[y][x] === 0) {
                return [y, x];
            }
        }
    }
    return null;
}

function is_valid(board, num, pos) {
    let y = pos[0];
    let x = pos[1];

    // Horizontal checking...
    for (let i = 0; i < 9;   i) {
        if (board[y][i] === num && x !== i) {
            return false;
        }
    }

    // Vertical checking
    for (let i = 0; i < 9;   i) {
        if (board[i][x] === num && y !== i) {
            return false;
        }
    }

    // 3*3 box checking
    box_start_y = (Math.floor(y / 3)) * 3;
    box_start_x = (Math.floor(x / 3)) * 3;

    for (let i = box_start_y; i < box_start_y   3;   i) {
        for (let j = box_start_x; j < box_start_x   3;   j) {
            if (board[i][j] === num && y !== i && x !== j) {
                return false;
            }
        }
    }

    return true;
}

function solve_board(board) {
    console.log("start", board);
    empty_pos = find_empty(board);
    console.log("empty_pos", empty_pos)
    if (empty_pos === null) {
        return board;
    }
        
    let y = empty_pos[0];
    let x = empty_pos[1];

    for (let num = 1; num < 10;   num) {
        console.log(board)
        if (is_valid(board, num, empty_pos)) {
            console.log(true, num)
            console.log("Before assignment", board[y][x]);
            board[y][x] = num;
            console.log("y", y, ", x", x);
            console.log("After true", board[y][x])
            console.log("After true", board);
            let solved_board = solve_board(board)
            console.log("solved", solved_board);
            if (solved_board !== false) {
                return solved_board;
            }
            console.log(num, "did not work!!")
            board[y][x] = 0;
        }
        console.log(false, num)
    }
    return false;
}

console.log("result", solve_board(puzzle))

The problem is on line 72 in solve_board function. It updates board[y][x] as seen in the console.logs, but does not affect board. Is it some reference issue or pass by value problem?

If anyone can help me solve this issue, I would be really grateful.

CodePudding user response:

you are assigning the new value correctly. Arrays are passed to the function by reference, so there should be no problem. You can verify it with this code snippet:

const arr = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

function foo(x) {
  x[0][0] = 2
}

foo(arr)
console.log(arr[0][0]) // 2

There probably is some issue with the game logic itself, the conditions in the is_valid don't look right. Consider taking a look on this article explaining how to solve sudoku programmatically with logic and without brute force.

Alternatively, I suggest you look at the backtracking algorithm, which tries all possible options, which should be straightforward to implement.

  • Related