5

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.

3 Answers3

5

'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"
  • 2
    So how can we convert `r1bqkbnr/pppp1ppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R` to valid chess.js fen? Coz we need that position of the board to start playing. – marukobotto Jan 15 '19 at 13:10
0

If you look at your FEN string it isn't made up of 6 space delimited sections like the one below, which is valid. You only have the first bit in yours.

"rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e3 0 2"
omarjebari
  • 4,861
  • 3
  • 34
  • 32
0

'r1bqkbnr/pppp1ppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R' this is only a part of FEN.

Example of valid FEN for chess.js:

"rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2"

In the examples above, "-" is missing. It didn't work for me without it.

Amir
  • 43
  • 6