0

My questions is that I have this class for a checkers game but I dont know how to implement that on a new function to restart the board.

class Board{
  private board:number[][];

constructor(){
  this.board = [
    [1, 0, 1, 0, 1, 0, 1, 0],
    [0, 1, 0, 1, 0, 1, 0, 1],
    [1, 0, 1, 0, 1, 0, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 2, 0, 2, 0, 2, 0, 2],
    [2, 0, 2, 0, 2, 0, 2, 0],
    [0, 2, 0, 2, 0, 2, 0, 2]
  ];
}

initializeBoard() {
  for (let i = 0; i < this.board.length; i++) {
    for (let j = 0; j < this.board[i].length; j++) {
      const cellvalue = this.board[i][j];
      if (cellvalue === 2) {
        setTile(i, j, "red pawn");
      } else if (cellvalue === 1) {
        setTile(i, j, "blue pawn");
      }
    }
  }
  setTile(0, 0, "blue queen");
  setTile(7, 7, "red queen");
}
}

And I have another file with the function to restart the board, how I import that initializeBoard() function on the class Board to the new one (function onRestart)??

Thank you very much!

I will want to see when I apply the function onRestart the Board start again with the same positions.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • How are you using the `Board` class right now? How do you create a `Board` and call its methods? – Alex Wayne Mar 15 '23 at 00:01
  • Store the value currently in `this.board` in a `private readonly` variable, with a name like `defaultBoard`. Then you can reset the board simply by `this.board = this.defaultBoard`. But since this would create a reference, and not a clone, better do something like described here: https://stackoverflow.com/questions/13756482/create-copy-of-multi-dimensional-array-not-reference-javascript – Tamás Polgár Mar 15 '23 at 00:04

0 Answers0