Compare commits
3 commits
023c725aae
...
aeeff83e01
Author | SHA1 | Date | |
---|---|---|---|
aeeff83e01 | |||
21d940d3ea | |||
c091e5c808 |
2 changed files with 87 additions and 36 deletions
|
@ -4,6 +4,7 @@ const saveGameKey = "connect-force-save";
|
||||||
const newGameClass = "new-game";
|
const newGameClass = "new-game";
|
||||||
const saveGameClass = "save-game";
|
const saveGameClass = "save-game";
|
||||||
const loadGameClass = "load-game";
|
const loadGameClass = "load-game";
|
||||||
|
const undoClass = "undo-game";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a component which represents the specified {@link game `game`}.
|
* Gets a component which represents the specified {@link game `game`}.
|
||||||
|
@ -35,6 +36,10 @@ export function App(game)
|
||||||
{
|
{
|
||||||
game.load(JSON.parse(localStorage.getItem(saveGameKey)));
|
game.load(JSON.parse(localStorage.getItem(saveGameKey)));
|
||||||
}
|
}
|
||||||
|
else if (target.classList.contains(undoClass))
|
||||||
|
{
|
||||||
|
game.undo();
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (let y = 0; y < game.board.length; y++)
|
for (let y = 0; y < game.board.length; y++)
|
||||||
|
@ -49,7 +54,6 @@ export function App(game)
|
||||||
{
|
{
|
||||||
if (game.addChip(x, y))
|
if (game.addChip(x, y))
|
||||||
{
|
{
|
||||||
game.state.turnCount++;
|
|
||||||
game.draw();
|
game.draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,7 +76,7 @@ export function App(game)
|
||||||
`Player ${Constants.PLAYER_NAMES[game.winner]} wins!` :
|
`Player ${Constants.PLAYER_NAMES[game.winner]} wins!` :
|
||||||
`It's player "${Constants.PLAYER_NAMES[game.currentPlayer]}"s turn`
|
`It's player "${Constants.PLAYER_NAMES[game.currentPlayer]}"s turn`
|
||||||
],
|
],
|
||||||
[MenuBar, game]
|
[MenuBar]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,44 +135,18 @@ export function Field(field)
|
||||||
/**
|
/**
|
||||||
* Renders a menu bar.
|
* Renders a menu bar.
|
||||||
*
|
*
|
||||||
* @param {import("./Game.js").Game} game
|
|
||||||
* The game controlled by the menu bar.
|
|
||||||
*
|
|
||||||
* @returns {NodeDescriptor}
|
* @returns {NodeDescriptor}
|
||||||
* The rendered element.
|
* The rendered element.
|
||||||
*/
|
*/
|
||||||
export function MenuBar(game)
|
export function MenuBar()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
"div",
|
"div",
|
||||||
{ className: "menu-bar" },
|
{ className: "menu-bar" },
|
||||||
[
|
[Button, ["New Game", { className: newGameClass }]],
|
||||||
Button,
|
[Button, ["Save Game", { className: saveGameClass }]],
|
||||||
[
|
[Button, ["Load Game", { className: loadGameClass }]],
|
||||||
"New Game",
|
[Button, ["Undo Last Move", { className: undoClass }]]
|
||||||
{
|
|
||||||
className: newGameClass
|
|
||||||
}
|
|
||||||
]
|
|
||||||
],
|
|
||||||
[
|
|
||||||
Button,
|
|
||||||
[
|
|
||||||
"Save Game",
|
|
||||||
{
|
|
||||||
className: saveGameClass
|
|
||||||
}
|
|
||||||
]
|
|
||||||
],
|
|
||||||
[
|
|
||||||
Button,
|
|
||||||
[
|
|
||||||
"Load Game",
|
|
||||||
{
|
|
||||||
className: loadGameClass
|
|
||||||
}
|
|
||||||
]
|
|
||||||
]
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,13 @@ export class Game
|
||||||
*/
|
*/
|
||||||
static #height = 6;
|
static #height = 6;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The previous states of the game.
|
||||||
|
*
|
||||||
|
* @type {IState[]}
|
||||||
|
*/
|
||||||
|
#previousStates = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The state of the game.
|
* The state of the game.
|
||||||
*
|
*
|
||||||
|
@ -140,7 +147,7 @@ export class Game
|
||||||
*/
|
*/
|
||||||
load(data)
|
load(data)
|
||||||
{
|
{
|
||||||
this.#state = data;
|
this.setState([], data);
|
||||||
this.draw();
|
this.draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,10 +164,27 @@ export class Game
|
||||||
*/
|
*/
|
||||||
reset()
|
reset()
|
||||||
{
|
{
|
||||||
this.#state = State.create(Game.#width, Game.#height);
|
this.setState([], State.create(Game.#width, Game.#height));
|
||||||
this.draw();
|
this.draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverts the last move.
|
||||||
|
*/
|
||||||
|
undo()
|
||||||
|
{
|
||||||
|
this.#state = this.#previousStates.pop();
|
||||||
|
|
||||||
|
if (!this.#state)
|
||||||
|
{
|
||||||
|
this.reset();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.draw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces the content of the board with the current state.
|
* Replaces the content of the board with the current state.
|
||||||
*/
|
*/
|
||||||
|
@ -171,6 +195,55 @@ export class Game
|
||||||
SuiWeb.render([App, this], container);
|
SuiWeb.render([App, this], container);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value located at the specified {@link path `path`} to the specified {@link value `value`}.
|
||||||
|
*
|
||||||
|
* @param {(keyof any)[]} path
|
||||||
|
* The path of the value to set.
|
||||||
|
*
|
||||||
|
* @param {any} value
|
||||||
|
* The value to set.
|
||||||
|
*/
|
||||||
|
setState(path, value)
|
||||||
|
{
|
||||||
|
this.#previousStates.push(this.#state);
|
||||||
|
this.#state = { ...this.#state };
|
||||||
|
this.#state.turnCount++;
|
||||||
|
|
||||||
|
if (path.length === 0)
|
||||||
|
{
|
||||||
|
this.#state = value;
|
||||||
|
}
|
||||||
|
else if (path.length === 1)
|
||||||
|
{
|
||||||
|
this.#state = {
|
||||||
|
...this.#state,
|
||||||
|
[path[0]]: value
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
let target = /** @type {any} */ (this.#state);
|
||||||
|
|
||||||
|
while (path.length > 1)
|
||||||
|
{
|
||||||
|
if (Array.isArray(target[path[0]]))
|
||||||
|
{
|
||||||
|
target[path[0]] = [...target[path[0]]];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
target[path[0]] = { ...target[path[0]] };
|
||||||
|
}
|
||||||
|
|
||||||
|
target = target[path[0]];
|
||||||
|
path = path.slice(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
target[path[0]] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a chip to the board indicated by the {@link x `x`} and the {@link y `y`} coordinate.
|
* Adds a chip to the board indicated by the {@link x `x`} and the {@link y `y`} coordinate.
|
||||||
*
|
*
|
||||||
|
@ -191,7 +264,7 @@ export class Game
|
||||||
{
|
{
|
||||||
if (this.board[i][x] === "")
|
if (this.board[i][x] === "")
|
||||||
{
|
{
|
||||||
this.board[i][x] = this.currentPlayer;
|
this.setState(["board", i, x], this.currentPlayer);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue