0

I am trying to create a function that uses an if-statement to check if a chessboard is in a specific position (this one I am using start) using the chessboard.js library

I tried using this code that compares the board's position property to the position I want using the object and FEN string but both doesn't work.

  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');

I even tried comparing it to 'start'

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

1 Answers1

1

Based on the docs, board.position is a function. You want to compare against the output of that function, not the function itself:

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

(Comparing objects for equality is more complicated; I'd suggest sticking with the FEN strings.)

Daniel Beck
  • 20,653
  • 5
  • 38
  • 53