Answers.Body: <p>It looks like your engine is using very simple for loop and arrays. Your code simply loop over again and again, possibly repeat each search iteration.</p>
  
  <p>That’s obviously not good. Chess engine can’t run fast if it has to loop like that. If the position had no check surely all CPU time wasted?</p>
  
  <p>My recommendation is change your data structure to a bitboard. Bitboard will allow you run check detection quickly, just do a AND the pieces your opponent can attack and your king position.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-28T23:12:57.023Z
Answers.Text: Doesn't JavaScript only give float number types? How would you make a bitboard using them?
Answers.UserDisplayName: Oscar Smith
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-28T23:33:16.463Z
Answers.Text: @OscarSmith I'm not JS expert. I coded my engine in C++, never did it on JS. You may be right, a JS expert is needed here. 64-bit integer representation in JS sounds more like a Stackoverflow problem. In any case I think JS is a bad choice for chess engine. One could just do it in C++ and compile it to JS.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-28T19:31:11.657Z
Answers.LastActivityDate: 2020-01-28T19:31:11.657Z
Answers.UserDisplayName: SmallChess
Body: <p>I am trying to write a simple chess engine in Javascript. It performs very slowly but I have finally rooted down the problem to check detection. How might I speed up this check detection? If you have any questions or need any clarifications feel free to ask.</p>
  
  <p>Check detection takes approximately 10-20 milliseconds.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>function availableInvalidSpots(board, player) {
      let validPositions = [];
      let playerPositions = getAllPieces(player, board);
      for (position = 0; position &lt; playerPositions.length; position++) {
          validPositions.push(getUncheckedMoves(playerPositions[position], board));
      }
      return validPositions;
  }
  
  function getPiecePosition(piece, board) {
      for (rowCount = 0; rowCount &lt; 8; rowCount++) {
          for (columnCount = 0; columnCount &lt; 8; columnCount++) {
              if (board[rowCount][columnCount].pieceName == piece) {
                  return {
                      x: columnCount,
                      y: rowCount
                  };
              }
          }
      }
  }
  
  function getPlayerInfo(pieceType) {
      return (pieceType == blackPieces ? {
          king: "blackKing",
          opponent: whitePieces
      } : {
          king: "whiteKing",
          opponent: blackPieces
      });
  }
  
  function setPlayerInfo(player, board) {
      let PLAYER = getPlayerInfo(player);
      let opponent = PLAYER.opponent;
      king = PLAYER.king;
      kingPosition = getPiecePosition(king, board);
      opponentMoves = availableInvalidSpots(board, opponent);
  }
  
  function isPlayerInCheck(player, board) {
      setPlayerInfo(player, board);
  
      for (opponentMove = 0; opponentMove &lt; opponentMoves.length; opponentMove++) {
          for (square = 0; square &lt; opponentMoves[opponentMove].move.length; square++) {
              if (opponentMoves[opponentMove].move[square].to.x == kingPosition.x &amp;&amp; opponentMoves[opponentMove].move[square].to.y == kingPosition.y) {
                  return true;
              }
          }
      }
      return false;
  }
  </code></pre>
  
Tags: <engines><programming><check><performance>
Title: How can I speed up my check detection in Javascript?
ThreadText: # How can I speed up my check detection in Javascript?
  
  (Asked by: username_0 on 2020-01-27)
  
  I am trying to write a simple chess engine in Javascript. It performs very slowly but I have finally rooted down the problem to check detection. How might I speed up this check detection? If you have any questions or need any clarifications feel free to ask.
  
  Check detection takes approximately 10-20 milliseconds.
  
  Here is the code:
  
  ```
  function availableInvalidSpots(board, player) {
      let validPositions = [];
      let playerPositions = getAllPieces(player, board);
      for (position = 0; position < playerPositions.length; position++) {
          validPositions.push(getUncheckedMoves(playerPositions[position], board));
      }
      return validPositions;
  }
  
  function getPiecePosition(piece, board) {
      for (rowCount = 0; rowCount < 8; rowCount++) {
          for (columnCount = 0; columnCount < 8; columnCount++) {
              if (board[rowCount][columnCount].pieceName == piece) {
                  return {
                      x: columnCount,
                      y: rowCount
                  };
              }
          }
      }
  }
  
  function getPlayerInfo(pieceType) {
      return (pieceType == blackPieces ? {
          king: "blackKing",
          opponent: whitePieces
      } : {
          king: "whiteKing",
          opponent: blackPieces
      });
  }
  
  function setPlayerInfo(player, board) {
      let PLAYER = getPlayerInfo(player);
      let opponent = PLAYER.opponent;
      king = PLAYER.king;
      kingPosition = getPiecePosition(king, board);
      opponentMoves = availableInvalidSpots(board, opponent);
  }
  
  function isPlayerInCheck(player, board) {
      setPlayerInfo(player, board);
  
      for (opponentMove = 0; opponentMove < opponentMoves.length; opponentMove++) {
          for (square = 0; square < opponentMoves[opponentMove].move.length; square++) {
              if (opponentMoves[opponentMove].move[square].to.x == kingPosition.x && opponentMoves[opponentMove].move[square].to.y == kingPosition.y) {
                  return true;
              }
          }
      }
      return false;
  }
  
  ```
  
  **Tags:** engines, programming, check, performance
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-01-28. Score: 2 | Accepted answer
  
  It looks like your engine is using very simple for loop and arrays. Your code simply loop over again and again, possibly repeat each search iteration.
  
  That’s obviously not good. Chess engine can’t run fast if it has to loop like that. If the position had no check surely all CPU time wasted?
  
  My recommendation is change your data structure to a bitboard. Bitboard will allow you run check detection quickly, just do a AND the pieces your opponent can attack and your king position.
  
  > **Comments:**
  > **username_2**: Doesn't JavaScript only give float number types? How would you make a bitboard using them?  
  > **username_1**: @username_2 I'm not JS expert. I coded my engine in C++, never did it on JS. You may be right, a JS expert is needed here. 64-bit integer representation in JS sounds more like a Stackoverflow problem. In any case I think JS is a bad choice for chess engine. One could just do it in C++ and compile it to JS.  
  
  ---
  
  