我正在尝试创建一个使用if语句的函数,该函数使用chessboard.js library检查棋盘是否在特定位置(我使用的是start
我尝试使用这段代码,它使用object和FEN字符串将棋盘的position属性与我想要的位置进行比较,但两者都不起作用。
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');我甚至试着把它比作“开始”。
if (board.position == 'start') {
alert('The board is in the start position');
} else {
alert('The board is not in the start position');发布于 2021-10-10 18:21:09
基于docs,board.position是一个函数。您希望与该函数的输出进行比较,而不是与函数本身进行比较:
if (board.position('fen') == 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR')
(比较对象是否相等是more complicated;我建议坚持使用FEN字符串。)
https://stackoverflow.com/questions/69517731
复制相似问题