Store the current player in the State
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Manuel Thalmann 2022-12-11 22:55:03 +01:00
parent aa1af7a9b8
commit f3a9568f70
2 changed files with 33 additions and 13 deletions

View file

@ -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(""))))
};

View file

@ -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);
}
}
}