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.
Asked
Active
Viewed 2,119 times
5

Thierry Bonnechere
- 103
- 7
-
Any particular reason why you've tagged this question `php`? – Mark Baker Sep 25 '14 at 08:02
-
I'am using it in a wordpress website. – Thierry Bonnechere Sep 25 '14 at 12:17
-
About as relevant as saying you're running an nginx webserver, or that it's hosted in Indonesia.... chessboard.js doesn't care about any of that – Mark Baker Sep 25 '14 at 12:53
3 Answers
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"

Petr Chernyshev
- 96
- 1
- 7
-
2So 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