如何在chessboard.js的if-else语句中使用棋盘位置



我正在尝试创建一个函数,使用if语句来检查棋盘是否处于特定位置(这个我使用start)使用棋盘.js库

我尝试使用这个代码,比较板的位置属性到我想要使用对象和FEN字符串的位置,但两者都不起作用。

if (board.position == {a1: "wR", a2: "wP", a7: "bP", , a8: "bR", b1: "wN", b2: "wP", b7: "bP", b8: "bN", c1: "wB", c2: "wP", c7: "bP", c8: "bB", d1: "wQ", d2: "wP", d7: "bP", d8: "bQ", e1: "wK", e2: "wP", e7: "bP", e8: "bK", f1: "wB", f2: "wP", f7: "bP", f8: "bB", g1: "wN", g2: "wP", g7: "bP", g8: "bN", h1: "wR", h2: "wP", h7: "bP", h8: "bR") {
alert('The board is in the start position');
} else {
alert('The board is not in the start position');
if (board.position == 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR') {
alert('The board is in the start position');
} else {
alert('The board is not in the start position');

我甚至试着将它与'start'进行比较

if (board.position == 'start') {
alert('The board is in the start position');
} else {
alert('The board is not in the start position');

根据文档,board.position是一个函数。您希望与该函数的输出进行比较,而不是与函数本身进行比较:

if (board.position('fen') == 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR')

(比较对象是否相等更为复杂;我建议坚持使用FEN字符串。)

最新更新