Compare commits

...

4 commits

Author SHA1 Message Date
Manuel Thalmann 9c9504363d
Repeatedly update game board
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-12-08 15:08:02 +01:00
Manuel Thalmann f36b60b2e4
Simplify accessability of player names 2022-12-08 15:07:50 +01:00
Manuel Thalmann 480a77ff86
Move state to a separate file 2022-12-08 14:32:09 +01:00
Manuel Thalmann 8269daa423
Remove test code 2022-12-08 13:31:04 +01:00
6 changed files with 123 additions and 12 deletions

View file

@ -10,4 +10,4 @@
<div class="board" id="board">
</div>
</body>
</html>
</html>

View file

@ -5,9 +5,11 @@ export class Constants
{
/**
* The IDs of the players.
*
* @type {Partial<Record<CellOwner, string>>}
*/
static PLAYER_NAMES = [
"red",
"blue"
];
static PLAYER_NAMES = {
r: "red",
b: "blue"
};
}

23
src/js/State.js Normal file
View file

@ -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 "";
}
})))
);

View file

@ -1,5 +1,6 @@
import { Constants } from "./Constants.js";
import { elt } from "./elt.js";
import { State } from "./State.js";
/**
* Initializes the game board.
@ -10,21 +11,22 @@ import { elt } from "./elt.js";
export function initializeBoard(id)
{
let board = document.getElementById(id);
board.innerHTML = "";
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 ${Constants.PLAYER_NAMES[playerId]}`
}));
}

View file

@ -1,5 +1,68 @@
import { initializeBoard } from "./board.js";
import { elt } from "./elt.js";
import { State } from "./State.js";
document.documentElement.appendChild(elt("a", { href: "https://startpage.com/" }, "This is a test"));
initializeBoard("board");
/**
* Initializes the board.
*/
function initialize()
{
initializeBoard("board");
}
initialize();
/**
* Sleeps for the specified number of milliseconds.
*
* @param {number} ms
* The number of milliseconds to sleep.
*/
async function sleep(ms)
{
return new Promise((resolve) => setTimeout(resolve, ms));
}
(
async () =>
{
let counter = 0;
let tick = 0;
// eslint-disable-next-line no-constant-condition
while (true)
{
await sleep(1000 * (1 / 5));
let cycle = counter % 24;
if ((cycle <= 11) || ((cycle % 2) === 0))
{
tick = (tick % 18) + 1;
let rowId = Math.floor(Math.random() * State.length);
let fieldId = Math.floor(Math.random() * State[rowId].length);
/**
* @type {CellOwner}
*/
let owner;
switch (Math.floor(Math.random() * 3))
{
case 1:
owner = "r";
break;
case 2:
owner = "b";
break;
default:
owner = "";
break;
}
State[rowId][fieldId] = owner;
console.log(tick);
initialize();
}
counter++;
}
})();

21
src/js/types.d.ts vendored Normal file
View file

@ -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
];