diff --git a/src/js/State.js b/src/js/State.js index bbd620b..8c553f9 100644 --- a/src/js/State.js +++ b/src/js/State.js @@ -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("")))) +}; diff --git a/src/js/board.js b/src/js/board.js index 83e5db0..8557e94 100644 --- a/src/js/board.js +++ b/src/js/board.js @@ -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); } } }