Allow saving and restoring save games

This commit is contained in:
Manuel Thalmann 2022-12-14 13:07:04 +01:00
parent 7a28a7e314
commit 01fea03112
No known key found for this signature in database
GPG key ID: 5FD9AD3CCDDBD27B
2 changed files with 44 additions and 0 deletions

View file

@ -73,6 +73,34 @@ export class Game
return this.state.board;
}
/**
* Dumps the state of the game.
*
* @returns {IState}
* The JSON string representing the state.
*/
dump()
{
return {
turnCount: this.state.turnCount,
board: {
...this.state.board
}
};
}
/**
* Loads the game from the specified {@link data `data`}.
*
* @param {IState} data
* The data to load.
*/
load(data)
{
this.state.turnCount = data.turnCount;
Object.assign(this.state.board, data.board);
}
/**
* Initializes the game.
*/

View file

@ -12,3 +12,19 @@ type Row = CellOwner[];
* Represents a game board.
*/
type Board = Row[];
/**
* Represents the state of a game.
*/
interface IState
{
/**
* The number of rounds that have been played.
*/
turnCount: number;
/**
* The board of the game.
*/
board: Board;
}