2

I am using chessboard.js script.

But I am facing a problem. I want to save a current game without end, save the current position and next time to use saved current position.

var cfg = { 
        position: 'r1bqkbnr/pppp1ppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R'
    };
var board = new ChessBoard('board', cfg);

Current position is showed correctly. But when I move, it goes to "start" state.

manlio
  • 18,345
  • 14
  • 76
  • 126
Wai Yan
  • 427
  • 2
  • 6
  • 23

2 Answers2

1

For storing in browser you could use cookies or localstorage.

Alternatively, you could send state to server and later read it using AJAX.

EDIT:

Looks like you have mistake in script. Instead of

new ChessBoard('board', cfg);

you should do

new ChessBoard('board', cfg.position);

Docs: http://chessboardjs.com/examples#1003

Community
  • 1
  • 1
Im0rtality
  • 3,463
  • 3
  • 31
  • 41
1

You can return the state of the board using

var saved_positions =  ChessBoard.objToFen(board.position())

...or if you're using the chess.js library for managing the game you can simply use

var saved_positions = game.fen()  


both will return a string containing the position of every piece on the board.


Then to load the saved board position with animations you can use:

 board.position(saved_position, true);

or to instantly set the positions

 board.position(saved_position, false);

Enjoy.

mike510a
  • 2,102
  • 1
  • 11
  • 27