Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to prevent piece captures? #70

Closed
Epitomaniac opened this issue May 2, 2023 · 7 comments
Closed

How to prevent piece captures? #70

Epitomaniac opened this issue May 2, 2023 · 7 comments

Comments

@Epitomaniac
Copy link

There is no property for areCapturesAllowed. Is there a way to programmatically disallow captures?

@Clariity
Copy link
Owner

Clariity commented May 2, 2023

I'm curious as to how you'd want this work? To overlay the pieces? A chess position cannot exist with multiple pieces on the same square

@Clariity
Copy link
Owner

Clariity commented May 2, 2023

if you just want to prevent a capture you can return false in onPieceDrop and the move will be disallowed

@Epitomaniac
Copy link
Author

I'd like to test whether a user can set up the starting position correctly, I put the pieces randomly on the board and ask the user to set up the pieces; if the user puts a piece on top of another, the piece would be captured which is an unwanted result that breaks the app.

@Clariity
Copy link
Owner

Clariity commented May 2, 2023

what would you want to happen if a user puts a piece on top of another?

you could instead just return false in onPieceDrop if you don't want the move to happen

ultimately it's up to the react-chessboard user to pass the desired fen position to the board, so the logic can be programmed externally to the package to swap the pieces if desired

@Epitomaniac
Copy link
Author

Epitomaniac commented May 2, 2023

what would you want to happen if a user puts a piece on top of another?

I want the piece to snap back to where it was before the move was made, as if the move was illegal.

you could instead just return false in onPieceDrop if you don't want the move to happen

How do I check if the move was a capture?

@Clariity
Copy link
Owner

Clariity commented May 2, 2023

export const Example = () => {
  const [game, setGame] = useState(new Chess());

  function onDrop(sourceSquare, targetSquare) {
    if (game.get(targetSquare) !== null) {
      return false;
    }
      
    const gameCopy = { ...game };
    const move = gameCopy.move({
      from: sourceSquare,
      to: targetSquare,
      promotion: "q", // always promote to a queen for example simplicity
    });
    setGame(gameCopy);

    // illegal move
    if (move === null) return false;
    return true;
  }

  return (
    <Chessboard
      id="PlayVsRandom"
      position={game.fen()}
      onPieceDrop={onDrop}
    />
  );
};

@Epitomaniac
Copy link
Author

Epitomaniac commented May 2, 2023

So this statement makes sure the move has been a capture, right?
if (game.get(targetSquare) !== null)
I'll test it once I get back home, thanks for the reply and the wonderful library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants