0

I am building a Sudoku game in React, the last function I wrote would make the board. It makes an empty Board (2D Array filled with zeros), solves the board, then takes some random values out to make it a playable Sudoku. The problem is that making the 2D Zeros Array is giving me a 2D Array filled with random numbers. It is crazy I know, but it is making me crazy that I can't figure out what the problem is. Code snippets that I've used:

let emptyBoard = [
  [ 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 ]
];
let emptyBoard = new Array(9).fill(new Array(9).fill(0));
let emptyBoard = [];
for (let i = 0; i < 9; i++) {
  emptyBoard.push([]);
  for (let j = 0; j < 9; j++) {
    emptyBoard[i].push(0);
  }
}

The complete function is as follows:

makeBoard() {
  let emptyBoard = [
    [ 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 ]
  ];
  console.log(emptyBoard);
  let { result, cells } = this.solveAndShuffle(emptyBoard);
  if (result) {
    let shuffledKeys = this.shuffle([ ...Array(81).keys() ]);
    for (let key of shuffledKeys) {
      const subgrid = Math.floor(key / 9);
      const cell = key % 9;
      const cellValue = cells[subgrid][cell];
      cells[subgrid][cell] = 0;
      const solutions = this.cellSolutions(subgrid, cell, cells);
      if (solutions.length !== 1 && solutions.length !== 2) {
        cells[subgrid][cell] = cellValue;
      }
    }
    const immutable = this.immutableCells(cells);
    this.setState({
      cells: cells,
      immutable: immutable,
      wrongCells: new Array(9).fill(new Array(9).fill(false)),
      causingError: new Array(9).fill(new Array(9).fill(false)),
      solved: false
    });
  } else {
    alert('Something wrong happend while trying to make a board!');
  }
}

I even deployed it with the console.log on Github Pages if anyone wants to check it online it is on Sudoku JS, you can call the function by pressing "New".

And the code is hosted on Github

I am thankful for anyone who could provide help ♥

malkrad
  • 11
  • 1
  • 3
  • What is `solveAndShuffle`? On which line does the array seem to get populated with random values? – CertainPerformance Feb 03 '20 at 08:11
  • @CertainPerformance it solves the empty board by shuffling the possible solutions of each cell. This way I make sure that I get a random solved grid. – malkrad Feb 03 '20 at 08:13
  • It's still not clear what this means: *The problem is that making the 2D Zeros Array is giving me a 2D Array filled with random numbers* – CertainPerformance Feb 03 '20 at 08:15
  • @CertainPerformance If you could please open the deployed Sudoku, and open the browser console, then click the "New" button, in the function I declare "emptyBoard" a 2D array as defined, then I directly print it to the console using console.log(emptyBoard), It should be an Array of 9 arrays, each one of them has 9 zeros, but what the console.log prints is an Array of 9 Arrays filled with random integers. – malkrad Feb 03 '20 at 08:18
  • @CertainPerformance thanks for linking the other question, I would have never guessed that the problem would be the way console.log acts T_T – malkrad Feb 03 '20 at 08:26

1 Answers1

0

If the problem is that you see randomly filled array in console, then it is just Chrome Dev Tools way to display values. If you open the array, you can see the blue "i" icon that says: "Value below was evaluated just now" - it means that the logged array was logged by reference and it's value was changed since it was logged. Try to check the value with JSON.stringify(). I suppose, it is everything okay with you code :)

Limbo
  • 2,123
  • 21
  • 41