英文:
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论