jhlywa / chess.js

A TypeScript chess library for chess move generation/validation, piece placement/movement, and check/checkmate/draw detection
BSD 2-Clause "Simplified" License
3.6k stars 880 forks source link

How to get the from and to square from SAN? #462

Open ahofmeister opened 1 month ago

ahofmeister commented 1 month ago

We want to draw an error for a specific move, that is part of a puzzle. For example, the user should play e4 , but played d4. We now want to display an error from d2 to d4. We only have the SAN (d4 in this case) available.

A possible solution could be, that we create a temporary game, execute the move with the SAN and get the move back. Not sure how this is performance wise.

Do you have any other solution on how to get the from and to square, to draw an arrow only from the SAN?

ahofmeister commented 1 month ago

I can confirm the solution I have suggested

  const showArrowForCorrectMove = (currentLine: TreeNode[]) => {
    const tempGame = new Chess(game.fen());
    const move = tempGame.move(currentLine?.[heroMoveIndex]?.move);
    if (move) {
      setCorrectMoveArrow([move?.from, move?.to, getRedColor().hexCode]);
    }
  };

The SAN comes from currentLine?.[heroMoveIndex]?.move and the game.fen() is the "main" game. Don't worry about the rest. :)