GithubHelp home page GithubHelp logo

Comments (7)

Epitomaniac avatar Epitomaniac commented on May 22, 2024 2

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.

from react-chessboard.

Clariity avatar Clariity commented on May 22, 2024 1
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}
    />
  );
};

from react-chessboard.

Clariity avatar Clariity commented on May 22, 2024

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

from react-chessboard.

Clariity avatar Clariity commented on May 22, 2024

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

from react-chessboard.

Epitomaniac avatar Epitomaniac commented on May 22, 2024

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.

from react-chessboard.

Clariity avatar Clariity commented on May 22, 2024

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

from react-chessboard.

Epitomaniac avatar Epitomaniac commented on May 22, 2024

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?

from react-chessboard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.