From 480a77ff8650e440f72a16b1e06ababc9e8a53b1 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Thu, 8 Dec 2022 14:32:09 +0100 Subject: [PATCH] Move state to a separate file --- src/js/State.js | 23 +++++++++++++++++++++++ src/js/board.js | 10 +++++----- src/js/types.d.ts | 21 +++++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 src/js/State.js create mode 100644 src/js/types.d.ts diff --git a/src/js/State.js b/src/js/State.js new file mode 100644 index 0000000..fb61658 --- /dev/null +++ b/src/js/State.js @@ -0,0 +1,23 @@ +/** + * The state of the board. + * + * @type {Board} + */ +export const State = /** @type {Board} */ ( + Array(6).fill("").map( + () => (Array(7).fill("").map( + () => + { + let id = Math.floor(Math.random() * 3); + + switch (id) + { + case 1: + return "r"; + case 2: + return "b"; + default: + return ""; + } + }))) +); diff --git a/src/js/board.js b/src/js/board.js index aa9f132..c8dec59 100644 --- a/src/js/board.js +++ b/src/js/board.js @@ -1,5 +1,5 @@ -import { Constants } from "./Constants.js"; import { elt } from "./elt.js"; +import { State } from "./State.js"; /** * Initializes the game board. @@ -13,18 +13,18 @@ export function initializeBoard(id) for (let i = 0; i < 6; i++) { - for (let i = 0; i < 7; i++) + for (let j = 0; j < 7; j++) { /** @type {Node[]} */ let children = []; - let playerId = Math.floor(Math.random() * 3); + let playerId = State[i][j]; - if (playerId > 0) + if (playerId !== "") { children.push(elt( "div", { - class: `piece ${Constants.PLAYER_NAMES[playerId - 1]}` + class: `piece ${playerId === "r" ? "red" : "blue"}` })); } diff --git a/src/js/types.d.ts b/src/js/types.d.ts new file mode 100644 index 0000000..09d85de --- /dev/null +++ b/src/js/types.d.ts @@ -0,0 +1,21 @@ +/** + * Represents the state of a field. + */ +type CellOwner = "" | "r" | "b"; + +/** + * Represents a row of the game field. + */ +type Row = [CellOwner, CellOwner, CellOwner, CellOwner, CellOwner, CellOwner, CellOwner]; + +/** + * Represents a game board. + */ +type Board = [ + Row, + Row, + Row, + Row, + Row, + Row +];