From f542319b43df287d28ef250c70b2e61f7ef46e8c Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 13 Dec 2022 11:00:26 +0100 Subject: [PATCH] Move initialization code to `Game` class --- src/js/Game.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ src/js/board.js | 52 -------------------------------------------- src/js/main.js | 3 +-- 3 files changed, 59 insertions(+), 54 deletions(-) delete mode 100644 src/js/board.js diff --git a/src/js/Game.js b/src/js/Game.js index 1fd08df..6995882 100644 --- a/src/js/Game.js +++ b/src/js/Game.js @@ -1,3 +1,5 @@ +import { Constants } from "./Constants.js"; +import { elt } from "./elt.js"; import { State } from "./State.js"; /** @@ -27,4 +29,60 @@ export class Game { return this.#state; } + + /** + * Gets the board of the game. + */ + get board() + { + return this.state.board; + } + + /** + * Initializes the game. + * + * @param {string} id + * The id of the element to add the board to. + */ + initialize(id) + { + let board = document.getElementById(id); + board.innerHTML = ""; + + for (let i = 0; i < 6; i++) + { + for (let j = 0; j < 7; j++) + { + /** @type {Node[]} */ + let children = []; + let playerId = this.board[i][j]; + + if (playerId !== "") + { + children.push( + elt( + "div", + { + class: `piece ${Constants.PLAYER_NAMES[playerId]}` + })); + } + + let field = elt( + "div", + { + class: "field" + }, + ...children); + + field.onclick = () => + { + this.board[i][j] = this.state.currentPlayer; + this.state.turnCount++; + this.initialize(id); + }; + + board.appendChild(field); + } + } + } } diff --git a/src/js/board.js b/src/js/board.js deleted file mode 100644 index f39d820..0000000 --- a/src/js/board.js +++ /dev/null @@ -1,52 +0,0 @@ -import { Constants } from "./Constants.js"; -import { elt } from "./elt.js"; - -/** - * Initializes the game board. - * - * @param {string} id - * The id of the element to add the board to. - * - * @param {import("./Game.js").Game} game - * The game that is being played. - */ -export function initializeBoard(id, game) -{ - let board = document.getElementById(id); - board.innerHTML = ""; - - for (let i = 0; i < 6; i++) - { - for (let j = 0; j < 7; j++) - { - /** @type {Node[]} */ - let children = []; - let playerId = game.state.board[i][j]; - - if (playerId !== "") - { - children.push(elt( - "div", - { - class: `piece ${Constants.PLAYER_NAMES[playerId]}` - })); - } - - let field = elt( - "div", - { - class: "field" - }, - ...children); - - field.onclick = () => - { - game.state.board[i][j] = game.state.currentPlayer; - game.state.turnCount++; - initializeBoard("board", game); - }; - - board.appendChild(field); - } - } -} diff --git a/src/js/main.js b/src/js/main.js index c953a1c..c00150e 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -1,4 +1,3 @@ -import { initializeBoard } from "./board.js"; import { Game } from "./Game.js"; /** @@ -14,7 +13,7 @@ let game; function initialize() { game = new Game(); - initializeBoard("board", game); + game.initialize("board"); } initialize();