Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dump / restore game state to / from json. #21

Open
macdja38 opened this issue Jun 7, 2017 · 7 comments
Open

dump / restore game state to / from json. #21

macdja38 opened this issue Jun 7, 2017 · 7 comments
Assignees
Labels

Comments

@macdja38
Copy link

macdja38 commented Jun 7, 2017

This is a feature request

It would be nice to see a simple api to dump game state to json and restore it. Something like

let json = game.dump()
game.restore(json)

This could be used to save games in a database or create save/restore points.

@brozeph brozeph self-assigned this Jun 7, 2017
@brozeph
Copy link
Owner

brozeph commented Jun 7, 2017

@macdja38 - thank you for this issue report!

I did something similar to this for a stdlib hosted micro service (https://stdlib.com/brozeph/chess), but I didn't use JSON as the format.

The code implementation is here: https://github.com/brozeph/stdlib-chess/blob/master/f/game/index.js

Effectively, I maintained state for the moves using a base64 encoded string... but I like the idea of being able to quickly and easily serialize/deserialize as JSON. I'll spend some time on this as time allows - it may take me a hot minute to get to it, though.

@brozeph
Copy link
Owner

brozeph commented Apr 18, 2018

@Piterden - thank you for sharing! It looks like you're saving each move into a table and then restoring the board by re-applying each move individually. This is effectively what I did in the stdlib implementation.

An alternative, I recently noticed, which I forgot about in my original reply on this issue thread, that there is a way to load a game from a move history: https://github.com/brozeph/node-chess/blob/master/src/game.js#L86. One possibility might be to serialize the move history as JSON (JSON.stringify(gameClient.game.moveHistory)) and then use that when creating a game subsequently for reload... if this sounds interesting or worthwhile, I could implement a load method from the game clients that does just this (as an alternative to the create method).

@Piterden
Copy link
Collaborator

Unfortunately, for my case the load method wouldn't be implementable, cause I would need to use the DB for store, anyway. Btw, I also calculate which turn is now from those data.

@Piterden
Copy link
Collaborator

@macdja38 #41 not json, but FEN

@macdja38
Copy link
Author

@macdja38 #41 not json, but FEN

Nice, thanks!

@brozeph
Copy link
Owner

brozeph commented Mar 1, 2021

Should we keep this issue open, or does the FEN load address this need?

@Piterden Piterden added the Idea label Jan 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants