Store state as an object
This commit is contained in:
parent
c94dc1cb8f
commit
e6cea57d65
2 changed files with 20 additions and 49 deletions
|
@ -25,7 +25,7 @@ export class Game
|
||||||
/**
|
/**
|
||||||
* The state of the game.
|
* The state of the game.
|
||||||
*
|
*
|
||||||
* @type {State}
|
* @type {IState}
|
||||||
*/
|
*/
|
||||||
#state;
|
#state;
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ export class Game
|
||||||
constructor(id)
|
constructor(id)
|
||||||
{
|
{
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.#state = new State(Game.#width, Game.#height);
|
this.#state = State.create(Game.#width, Game.#height);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -66,10 +66,12 @@ export class Game
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the current player.
|
* Gets the current player.
|
||||||
|
*
|
||||||
|
* @type {CellOwner}
|
||||||
*/
|
*/
|
||||||
get currentPlayer()
|
get currentPlayer()
|
||||||
{
|
{
|
||||||
return this.state.currentPlayer;
|
return this.state.turnCount % 2 === 0 ? "r" : "b";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -126,9 +128,7 @@ export class Game
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
turnCount: this.state.turnCount,
|
turnCount: this.state.turnCount,
|
||||||
board: {
|
board: this.state.board
|
||||||
...this.state.board
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,10 +140,7 @@ export class Game
|
||||||
*/
|
*/
|
||||||
load(data)
|
load(data)
|
||||||
{
|
{
|
||||||
this.state.turnCount = data.turnCount;
|
this.#state = data;
|
||||||
this.state.board.splice(0);
|
|
||||||
this.#state = new State(Game.#width, Game.#height);
|
|
||||||
Object.assign(this.state.board, data.board);
|
|
||||||
this.draw();
|
this.draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,7 +157,7 @@ export class Game
|
||||||
*/
|
*/
|
||||||
reset()
|
reset()
|
||||||
{
|
{
|
||||||
this.#state = new State(Game.#width, Game.#height);
|
this.#state = State.create(Game.#width, Game.#height);
|
||||||
this.draw();
|
this.draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,7 +191,7 @@ export class Game
|
||||||
{
|
{
|
||||||
if (this.board[i][x] === "")
|
if (this.board[i][x] === "")
|
||||||
{
|
{
|
||||||
this.board[i][x] = this.state.currentPlayer;
|
this.board[i][x] = this.currentPlayer;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,52 +4,26 @@
|
||||||
export class State
|
export class State
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The board of the game.
|
* Creates a new state.
|
||||||
*
|
|
||||||
* @type {Board}
|
|
||||||
*/
|
|
||||||
#board;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The number of turns that have been played.
|
|
||||||
*/
|
|
||||||
turnCount = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes a new instance of the {@link State `State`} class.
|
|
||||||
*
|
*
|
||||||
* @param {number} width
|
* @param {number} width
|
||||||
* The width of the board.
|
* The width of the board.
|
||||||
*
|
*
|
||||||
* @param {number} height
|
* @param {number} height
|
||||||
* The height of the board.
|
* The height of the board.
|
||||||
|
*
|
||||||
|
* @returns {IState}
|
||||||
|
* The newly created state.
|
||||||
*/
|
*/
|
||||||
constructor(width, height)
|
static create(width, height)
|
||||||
{
|
{
|
||||||
this.#board = /** @type {Board} */ (
|
return {
|
||||||
Array(height).fill("").map(
|
turnCount: 0,
|
||||||
|
board: Array(height).fill("").map(
|
||||||
() =>
|
() =>
|
||||||
{
|
{
|
||||||
return Array(width).fill(
|
return Array(width).fill("");
|
||||||
/** @type {CellOwner} */ (""));
|
})
|
||||||
}));
|
};
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the id of the current player.
|
|
||||||
*
|
|
||||||
* @type {CellOwner}
|
|
||||||
*/
|
|
||||||
get currentPlayer()
|
|
||||||
{
|
|
||||||
return this.turnCount % 2 === 0 ? "r" : "b";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the board of the game.
|
|
||||||
*/
|
|
||||||
get board()
|
|
||||||
{
|
|
||||||
return this.#board;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue