1

I`m trying to initialize multi-array sudoku[9][9] which is consist of cell when I run sudoku.initialize(), all row, col, box in *sudoku[x][y]*s are 8,8,9 each.

  var cell = {
    sol: 0,
    row: 0,
    col: 0,
    box: 0,
    candi: [1, 2, 3, 4, 5, 6, 7, 8, 9]
};


var sudoku = {}
sudoku = new Array(9);
for (var i = 0; i < 9; i++) {
    sudoku[i] = new Array(9);
}
for (var i = 0; i < 9; i++) {
    for (var j = 0; j < 9; j++) {
        sudoku[i][j] = cell;
    }
}
sudoku.initialize = function () {
    for (var i = 0; i < 9; i++) {
        for (var j = 0; j < 9; j++) {
            sudoku[i][j].row = i;
            sudoku[i][j].col = j;
            if (i < 3) {
                if (j < 3)
                    sudoku[i][j].box = 1;
                else if (2 < j && j < 6)
                    sudoku[i][j].box = 2;
                else if (5 < j)
                    sudoku[i][j].box = 3;
            }
            else if (2 < i && i < 6) {
                if (j < 3)
                    sudoku[i][j].box = 4;
                else if (2 < j && j < 6)
                    sudoku[i][j].box = 5;
                else if (5 < j)
                    sudoku[i][j].box = 6;
            }
            else if (5 < i)
                if (j < 3)
                    sudoku[i][j].box = 7;
                else if (2 < j && j < 6)
                    sudoku[i][j].box = 8;
                else if (5 < j)
                    sudoku[i][j].box = 9;
        }
    }
}
sudoku.print=function(){
    for (var i = 0; i < 9; i++) {
        for (var j = 0; j < 9; j++) {
            document.write(sudoku[i][j].col + "&nbsp");
        }
        document.write("\n");
    }
}
sudoku.initialize();`

I guess there is misunderstand for multi-dementional array in javascript.

 sudoku[2][2].box=1
    1
    sudoku[4][8].box=2
    2
    sudoku[2][2].box
    2
    sudoku[3][3].box
    2

cuz I`m beginner for javascript, this codes could be ridiculous. but anyway you can catch what I meant.

1
  • 2
    All 81 items in your array point to the same object. Commented Jul 11, 2017 at 10:27

2 Answers 2

1

All 81 items in your multi-dimensional array point to the same object, because of this:

for (var i = 0; i < 9; i++) {
    for (var j = 0; j < 9; j++) {
        sudoku[i][j] = cell;
    }
}

You make sudoku[i][j] store a reference to the same object cell.
When you later try to modify / access values in sudoku[2][2], sudoku[3][3], sudoku[4][8], you actually work with the same object.

You need to create a new object for every cell.
Changing your code this way should help:

for (var i = 0; i < 9; i++) {
    for (var j = 0; j < 9; j++) {
        sudoku[i][j] = {
            sol: 0,
            row: 0,
            col: 0,
            box: 0,
            candi: [1, 2, 3, 4, 5, 6, 7, 8, 9]
        };
    }
}

You also don't need so many ifs in your initialize method.
Some math and integer division / modulus would do all the magic in two lines of code ;)

function Cell(row, col)
{
  this.col = col;
  this.row = row;
  this.sol = 0;
  this.box = Math.ceil(row / 3) * 3 + Math.ceil(col / 3) - 3;
  this.candidates = [1, 2, 3, 4, 5, 6, 7, 8, 9];
}

function Sudoku()
{
  this.rows = new Array(9);
  for (var i = 0; i < 9; i++) 
  {
    this.rows[i] = new Array(9);
    for (var j = 0; j < 9; j++) {
      this.rows[i][j] = new Cell(i, j);
    }
  }
  
  this.print = function() {
    for (var i = 0; i < 9; i++) {
        for (var j = 0; j < 9; j++) {
            document.write(this.rows[i][j].row + "/" + this.rows[i][j].col + "&nbsp");
        }
        document.write("<br/>");
    }
  };
}

var sudoku = new Sudoku();
sudoku.print();

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that you are assigning the same object to every one of your cells. Because objects in Javascript are stored by reference (not by value), doing

sudoku[1][2] = cell;

Essentially makes sudoku[1][2] and cell mean the same thing. You need to create a new cell object for every one of the positions in sudoku. There is a lot of material available on how to define "classes" in Javascript and instantiate objects, see for example:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

Although there are other approaches possible, for example you could clone the object, use prototypes, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.