【问题标题】:How to use chessboard positions in an if-else statement for chessboard.js如何在 chessboard.js 的 if-else 语句中使用棋盘位置
【发布时间】:2021-10-10 18:05:19
【问题描述】:

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

我尝试使用此代码将板的位置属性与我想要使用对象和 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');

我什至尝试将它与“开始”进行比较

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

【问题讨论】:

  • 您是否尝试记录 board.position 以查看其实际包含的内容?

标签: javascript jquery chessboard.js


【解决方案1】:

基于docsboard.position是一个函数。您想与该函数的输出进行比较,而不是函数本身:

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

(比较对象是否相等是more complicated;我建议坚持使用 FEN 字符串。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多