首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在chessboard.js的if-else语句中使用棋盘位置

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

Stack Overflow用户
提问于 2021-10-10 18:05:19
回答 1查看 21关注 0票数 0

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

我尝试使用这段代码,它使用object和FEN字符串将棋盘的position属性与我想要的位置进行比较,但两者都不起作用。

代码语言:javascript
复制
  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');
代码语言:javascript
复制
   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');

我甚至试着把它比作“开始”。

代码语言:javascript
复制
   if (board.position == 'start') {
    alert('The board is in the start position');
  } else {
    alert('The board is not in the start position');
EN

回答 1

Stack Overflow用户

发布于 2021-10-10 18:21:09

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69517731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档