我正在使用chess.js和chessboard.js运行一个项目。现在,我想存储用户退出页面时的历史记录和FEN,并在用户返回时恢复。恢复FEN很容易,但我不太确定是否要恢复历史。我在考虑将game.history()存储在数据库中,当游戏恢复时,使newGame.history() = game.history()。这样行得通吗?游戏恢复后,新的移动历史记录是否会被附加到先前的历史记录之后?谢谢!
发布于 2020-12-11 20:23:22
回答有点晚了,但不要使用历史()。请改用pgn()。当你保存游戏的pgn时,你同时保存了fen (最后一个位置的fen)。pgn()提供了一个字符串,您可以将其保存在任何位置,然后使用load-pgn(mysavedgameinpgnformat)重新加载游戏。例如,在客户端,这是我让这个库工作的唯一地方:
script(src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js")
script(type='text/javascript' src="javascripts/chess.js")
<script>
var game = new Chess();
console.log(game.fen());
game.move("e4");
console.log(game.fen());
console.log(game.history());
var storegamepgn = game.pgn(); //storegamepgn is a string which can be stored most anywhere
game = new Chess();
console.log(game.fen());
game.load_pgn(storegamepgn)
console.log(game.fen());
game.move("e5");
console.log(game.fen());
</script>https://stackoverflow.com/questions/63897994
复制相似问题