如何在Chessboard.js中更改起始位置?

huangapple go评论65阅读模式
英文:

How to change the start position in Chessboard.js?

问题

在网站https://chessboardjs.com/examples#5000提供的代码中,当我尝试更改配置中的起始位置时,所有棋子在移动任何棋子时都会回到初始位置。我还尝试使用game.load('fen'),但结果相同。关于这方面的任何帮助将不胜感激。

我想要更改起始位置,就像在一些谜题中只有一些棋子,但所有常规国际象棋规则仍然适用。

我尝试使用game.load('fen'),并且更改'configs'中的'position'。

英文:

I am using the code given in the website https://chessboardjs.com/examples#5000.
Here, when I try to change the start position in the config, all the pieces snap back to the intial position when any piece is moved. I also tried using game.load('fen'), but it gives the same result. Any help regarding this would be greatly appreciated.

I want to change the starting position, like in the cases of puzzles where only some pieces are there but all the usual chess rules are followed.

I tried using game.load('fen') and also change the 'position' in 'configs'.

答案1

得分: 1

我自己后来弄明白了。问题在于chessboard.js和chess.js使用不同类型的FEN。在chessboard.js中,FEN只是:

'rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR'

而在chess.js中,末尾有额外的元素:

'rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 2'

为了实现期望的结果,请将配置中的位置更改为chessboard.js的位置:

var config = {
                draggable: true,
                position: 'rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR',
                onDragStart: onDragStart,
                onDrop: onDrop,
                onSnapEnd: onSnapEnd
            }

然后使用chess.js的FEN加载chess.js棋盘:

var game = new Chess()
game.load('rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 2')

我希望这个解决方案对将来的某人有帮助!

英文:

I figured this out later myself. The problem is that chessboard.js and chess.js use different types of FENs. In chessboard.js, the FEN is simply

'rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR'

whereas in chess.js, there are additional elements at the end:
'rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 2'

To achieve the desired result, change the position in the configs to the chessboard.js one

var config = {
                draggable: true,
                position: 'rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR',
                onDragStart: onDragStart,
                onDrop: onDrop,
                onSnapEnd: onSnapEnd
            }

and load the chess.js board with its own FEN

var game = new Chess()
game.load('rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 2')

I hope this solution helps someone in the future!

huangapple
  • 本文由 发表于 2023年2月20日 00:26:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定