Skip to content

Conversation

zthoffman21
Copy link

Issue:
In the current version, the getAdjCells function reference the board using 'board'. However, by not specifying the scope using 'this.'—like it currently does for 'this.row' and 'this.col'—it produces incorrect and seemingly random results. By including the scope identifier, the function now produces the desired result.

Test:
Below is a simple test that creates a 3x3 board and places 2 bombs in that board. It then runs the getAdjCells function on a cell in that board which should output 2. However, without including the 'this.' keyword, it produces inconsistent results even though it is ran on the same board and cell. Once the keyword is included, the function only output the correct result.

function testGetAdjBombs() {
  // Create a controlled 3x3 board with new Cell instances.
  let testBoard = [];
  for (let i = 0; i < 3; i++) {
    let row = [];
    for (let j = 0; j < 3; j++) {
      // Temporarily set the board to null; we'll assign it next.
      row.push(new Cell(i, j, null));
    }
    testBoard.push(row);
  }
  
  // Assign the test board to each cell's board property.
  testBoard.forEach(row => {
    row.forEach(cell => cell.board = testBoard);
  });
  
  // Place bombs at known positions (e.g., (0,1) and (1,1)).
  testBoard[0][1].bomb = true;
  testBoard[1][1].bomb = true;
  
  // Recalculate adjacent bomb counts for all cells.
  testBoard.forEach(row => {
    row.forEach(cell => cell.calcAdjBombs());
  });
  
  // For cell at (1,0), the expected adjacent bomb count is 2.
  console.log('Cell (1,0) adjacent bombs:', testBoard[1][0].adjBombs);
}

testGetAdjBombs();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant