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.
|
* The state of the board.
|
||||||
*
|
|
||||||
* @type {Board}
|
|
||||||
*/
|
*/
|
||||||
export const State = /** @type {Board} */ (
|
export const State = {
|
||||||
Array(6).fill("").map(
|
turnCount: 0,
|
||||||
() => (Array(7).fill(""))));
|
|
||||||
|
/**
|
||||||
|
* 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[]} */
|
/** @type {Node[]} */
|
||||||
let children = [];
|
let children = [];
|
||||||
let playerId = State[i][j];
|
let playerId = State.board[i][j];
|
||||||
|
|
||||||
if (playerId !== "")
|
if (playerId !== "")
|
||||||
{
|
{
|
||||||
|
@ -30,13 +30,21 @@ export function initializeBoard(id)
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
board.appendChild(
|
let field = elt(
|
||||||
elt(
|
"div",
|
||||||
"div",
|
{
|
||||||
{
|
class: "field"
|
||||||
class: "field"
|
},
|
||||||
},
|
...children);
|
||||||
...children));
|
|
||||||
|
field.onclick = () =>
|
||||||
|
{
|
||||||
|
State.board[i][j] = State.currentPlayer;
|
||||||
|
State.turnCount++;
|
||||||
|
initializeBoard("board");
|
||||||
|
};
|
||||||
|
|
||||||
|
board.appendChild(field);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue