Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chessboard.js can't play a game with a set position

When I use the example 'only allow legal moves http://chessboardjs.com/examples#5000, I can move the white and black pieces. When I want to set a fen string in the config, replacing position: 'start' by position: 'r1bqkbnr/pppp1ppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R', I can't move the pieces that have already been moved and if I move a piece that has never been moved, all the other pieces get back to the start position.

like image 551
Thierry Bonnechere Avatar asked Sep 25 '14 07:09

Thierry Bonnechere


1 Answers

'r1bqkbnr/pppp1ppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R' is not valid FEN format for chess.js because the library doesn't know about any parameters (move: black or white, castles, e.t.c).

var game = new Chess();
game.validate_fen('r1bqkbnr/pppp1ppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R');

//Object {valid: false, error_number: 1, error: "FEN string must contain six space-delimited fields."}

You should use correct FEN for chess.js. For example:

var game = new Chess("rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e3 0 2");

// cfg.position == "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e3 0 2"

Or you can use game.load function. For example:

var game = new Chess();
game.load("rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e3 0 2");

// cfg.position == "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e3 0 2"
like image 187
Petr Chernyshev Avatar answered Sep 30 '22 07:09

Petr Chernyshev