Store the current player in the State
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
aa1af7a9b8
commit
f3a9568f70
2 changed files with 33 additions and 13 deletions
|
@ -1,8 +1,20 @@
|
|||
/**
|
||||
* The state of the board.
|
||||
*
|
||||
* @type {Board}
|
||||
*/
|
||||
export const State = /** @type {Board} */ (
|
||||
Array(6).fill("").map(
|
||||
() => (Array(7).fill(""))));
|
||||
export const State = {
|
||||
turnCount: 0,
|
||||
|
||||
/**
|
||||
* Gets the id of the current player.
|
||||
*
|
||||
* @type {CellOwner}
|
||||
*/
|
||||
get currentPlayer()
|
||||
{
|
||||
return this.turnCount % 2 === 0 ? "r" : "b";
|
||||
},
|
||||
|
||||
board: /** @type {Board} */ (
|
||||
Array(6).fill("").map(
|
||||
() => (Array(7).fill(""))))
|
||||
};
|
||||
|
|
|
@ -19,7 +19,7 @@ export function initializeBoard(id)
|
|||
{
|
||||
/** @type {Node[]} */
|
||||
let children = [];
|
||||
let playerId = State[i][j];
|
||||
let playerId = State.board[i][j];
|
||||
|
||||
if (playerId !== "")
|
||||
{
|
||||
|
@ -30,13 +30,21 @@ export function initializeBoard(id)
|
|||
}));
|
||||
}
|
||||
|
||||
board.appendChild(
|
||||
elt(
|
||||
"div",
|
||||
{
|
||||
class: "field"
|
||||
},
|
||||
...children));
|
||||
let field = elt(
|
||||
"div",
|
||||
{
|
||||
class: "field"
|
||||
},
|
||||
...children);
|
||||
|
||||
field.onclick = () =>
|
||||
{
|
||||
State.board[i][j] = State.currentPlayer;
|
||||
State.turnCount++;
|
||||
initializeBoard("board");
|
||||
};
|
||||
|
||||
board.appendChild(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue