From 4717a6e4e97c5ee02d4d7158e0a61cbd2d0859c2 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 7 Dec 2022 22:24:48 +0100 Subject: [PATCH] Add code for automatically creating game board --- src/index.html | 48 +-------------------------------------------- src/js/Constants.js | 13 ++++++++++++ src/js/board.js | 40 +++++++++++++++++++++++++++++++++++++ src/js/main.js | 4 +++- 4 files changed, 57 insertions(+), 48 deletions(-) create mode 100644 src/js/Constants.js create mode 100644 src/js/board.js diff --git a/src/index.html b/src/index.html index 9704a97..a8bd83e 100644 --- a/src/index.html +++ b/src/index.html @@ -7,53 +7,7 @@ -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
\ No newline at end of file diff --git a/src/js/Constants.js b/src/js/Constants.js new file mode 100644 index 0000000..fbf90ec --- /dev/null +++ b/src/js/Constants.js @@ -0,0 +1,13 @@ +/** + * Provides constants for the project. + */ +export class Constants +{ + /** + * The IDs of the players. + */ + static PLAYER_NAMES = [ + "red", + "blue" + ]; +} diff --git a/src/js/board.js b/src/js/board.js new file mode 100644 index 0000000..aa9f132 --- /dev/null +++ b/src/js/board.js @@ -0,0 +1,40 @@ +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. + */ +export function initializeBoard(id) +{ + let board = document.getElementById(id); + + for (let i = 0; i < 6; i++) + { + for (let i = 0; i < 7; i++) + { + /** @type {Node[]} */ + let children = []; + let playerId = Math.floor(Math.random() * 3); + + if (playerId > 0) + { + children.push(elt( + "div", + { + class: `piece ${Constants.PLAYER_NAMES[playerId - 1]}` + })); + } + + board.appendChild( + elt( + "div", + { + class: "field" + }, + ...children)); + } + } +} diff --git a/src/js/main.js b/src/js/main.js index 97e3c49..7bfd889 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -1,3 +1,5 @@ +import { initializeBoard } from "./board.js"; import { elt } from "./elt.js"; -elt("a", { href: "https://startpage.com/" }, "This is a test"); +document.documentElement.appendChild(elt("a", { href: "https://startpage.com/" }, "This is a test")); +initializeBoard("board");