Add a method for adding chips

This commit is contained in:
Manuel Thalmann 2022-12-13 11:28:59 +01:00
parent 391edbed72
commit fb51f48a02
No known key found for this signature in database
GPG key ID: 5FD9AD3CCDDBD27B

View file

@ -111,13 +111,41 @@ export class Game
field.onclick = () => field.onclick = () =>
{ {
this.board[y][x] = this.state.currentPlayer; if (this.addChip(x, y))
this.state.turnCount++; {
this.draw(); this.state.turnCount++;
this.draw();
}
}; };
board.appendChild(field); board.appendChild(field);
} }
} }
} }
/**
* Adds a chip to the board indicated by the {@link x `x`} and the {@link y `y`} coordinate.
*
* @param {number} x
* The x coordinate to add the chip to.
*
* @param {number} y
* The y coordinate to add the chip to.
*
* @returns {boolean}
* A value indicating whether the chip could be added.
*/
addChip(x, y)
{
for (let i = Game.#height - 1; i >= 0; i--)
{
if (this.board[i][x] === "")
{
this.board[i][x] = this.state.currentPlayer;
return true;
}
}
return false;
}
} }