Compare commits
4 commits
2d0b47f603
...
c40492c0e3
Author | SHA1 | Date | |
---|---|---|---|
c40492c0e3 | |||
602d8b4d6f | |||
399fa7257c | |||
2aa9bf5b4b |
4 changed files with 124 additions and 45 deletions
|
@ -1,5 +1,6 @@
|
|||
import { Constants } from "./Constants.js";
|
||||
import { elt } from "./elt.js";
|
||||
import { render } from "./SJDON.js";
|
||||
import { State } from "./State.js";
|
||||
|
||||
/**
|
||||
|
@ -145,55 +146,52 @@ export class Game
|
|||
let board = this.#board;
|
||||
board.innerHTML = "";
|
||||
|
||||
for (let y = 0; y < Game.#height; y++)
|
||||
{
|
||||
for (let x = 0; x < Game.#width; x++)
|
||||
{
|
||||
/** @type {Node[]} */
|
||||
let children = [];
|
||||
let playerId = this.board[y][x];
|
||||
|
||||
if (playerId !== "")
|
||||
{
|
||||
children.push(
|
||||
elt(
|
||||
"div",
|
||||
render(
|
||||
[
|
||||
"div",
|
||||
...this.board.flatMap(
|
||||
(row, y) =>
|
||||
{
|
||||
return row.map(
|
||||
(cell, x) =>
|
||||
{
|
||||
class: `piece ${Constants.PLAYER_NAMES[playerId]}`
|
||||
}));
|
||||
}
|
||||
return /** @type {NodeDescriptor} */ ([
|
||||
"div",
|
||||
{
|
||||
className: "field",
|
||||
onclick: () =>
|
||||
{
|
||||
if (this.addChip(x, y))
|
||||
{
|
||||
this.state.turnCount++;
|
||||
this.draw();
|
||||
}
|
||||
}
|
||||
},
|
||||
...(
|
||||
cell !== "" ?
|
||||
[
|
||||
/** @type {NodeDescriptor} */ (["div", { className: `piece ${Constants.PLAYER_NAMES[cell]}` }])
|
||||
] :
|
||||
[])
|
||||
]);
|
||||
});
|
||||
}),
|
||||
["div", { style: "clear: both;" }]
|
||||
],
|
||||
board);
|
||||
|
||||
let field = elt(
|
||||
"div",
|
||||
{
|
||||
class: "field"
|
||||
},
|
||||
...children);
|
||||
this.#log.innerHTML = "";
|
||||
|
||||
field.onclick = () =>
|
||||
{
|
||||
if (this.addChip(x, y))
|
||||
{
|
||||
this.state.turnCount++;
|
||||
this.draw();
|
||||
}
|
||||
};
|
||||
|
||||
board.appendChild(field);
|
||||
}
|
||||
}
|
||||
|
||||
board.appendChild(
|
||||
elt(
|
||||
render(
|
||||
[
|
||||
"div",
|
||||
{
|
||||
style: "clear: both;"
|
||||
}));
|
||||
|
||||
this.#log.className = "";
|
||||
this.#log.classList.add(this.state.currentPlayer);
|
||||
this.#log.innerHTML = "";
|
||||
this.#log.textContent = `It's player "${Constants.PLAYER_NAMES[this.state.currentPlayer]}"s turn`;
|
||||
className: this.state.currentPlayer
|
||||
},
|
||||
`It's player "${Constants.PLAYER_NAMES[this.state.currentPlayer]}"s turn`
|
||||
],
|
||||
this.#log);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
51
packages/game/src/js/SJDON.js
Normal file
51
packages/game/src/js/SJDON.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
/**
|
||||
* Renders the specified {@link data `data`} and appends it to the specified {@link element `element`}.
|
||||
*
|
||||
* @param {NodeDescriptor} data
|
||||
* The node to render written in SJDON notation.
|
||||
*
|
||||
* @param {HTMLElement} element
|
||||
* The element to add the rendered node to.
|
||||
*/
|
||||
export function render(data, element)
|
||||
{
|
||||
if (Array.isArray(data))
|
||||
{
|
||||
let descriptor = data[0];
|
||||
let args = data.slice(1);
|
||||
|
||||
if (typeof descriptor === "function")
|
||||
{
|
||||
render(descriptor(...args), element);
|
||||
}
|
||||
else if (typeof descriptor === "string")
|
||||
{
|
||||
let result = element.ownerDocument.createElement(descriptor);
|
||||
element.appendChild(result);
|
||||
|
||||
for (let arg of args)
|
||||
{
|
||||
if (typeof arg === "object" && !Array.isArray(arg))
|
||||
{
|
||||
Object.assign(result, arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
render(arg, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new SyntaxError();
|
||||
}
|
||||
}
|
||||
else if (typeof data === "string")
|
||||
{
|
||||
element.appendChild(element.ownerDocument.createTextNode(data));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new SyntaxError();
|
||||
}
|
||||
}
|
|
@ -65,8 +65,9 @@ function initialize()
|
|||
game = new Game("game");
|
||||
game.initialize();
|
||||
|
||||
(/** @type {HTMLElement} */ (document.querySelector(".new-game"))).onclick = () =>
|
||||
(/** @type {HTMLElement} */ (document.querySelector(".new-game"))).onclick = (event) =>
|
||||
{
|
||||
event.preventDefault();
|
||||
game.reset();
|
||||
};
|
||||
|
||||
|
|
29
packages/game/src/js/types.d.ts
vendored
29
packages/game/src/js/types.d.ts
vendored
|
@ -28,3 +28,32 @@ interface IState
|
|||
*/
|
||||
board: Board;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a node in the SJDON notation.
|
||||
*/
|
||||
type NodeDescriptor =
|
||||
TextDescriptor |
|
||||
ElementDescriptor |
|
||||
FunctionNode;
|
||||
|
||||
/**
|
||||
* Represents a text-node in the SJDON notation.
|
||||
*/
|
||||
type TextDescriptor = string;
|
||||
|
||||
/**
|
||||
* Represents an html-element in the SJDON notation.
|
||||
*/
|
||||
type ElementDescriptor = [
|
||||
tag: string,
|
||||
// eslint-disable-next-line @typescript-eslint/array-type
|
||||
...args: (NodeDescriptor | Record<string, unknown>)[]
|
||||
];
|
||||
|
||||
/**
|
||||
* Represents a component in the SJDON notation.
|
||||
*/
|
||||
type FunctionNode = [
|
||||
fn: (...args: any[]) => NodeDescriptor, ...args: any[]
|
||||
];
|
||||
|
|
Loading…
Reference in a new issue