Add a method for adding chips
This commit is contained in:
parent
391edbed72
commit
fb51f48a02
1 changed files with 31 additions and 3 deletions
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue