GithubHelp home page GithubHelp logo

Comments (13)

Clariity avatar Clariity commented on May 16, 2024 2

would an onPromotionDialogOpen prop help out here?

this function could return whether the move is valid and then not show the dialog

from react-chessboard.

DevArches avatar DevArches commented on May 16, 2024

Just logged it for me with that exact fen. Worked fine, returned all the values and the promotion piece I selected. Can you explain a bit more exactly what is happening when you select the promoted piece?

from react-chessboard.

kaustavsarkar avatar kaustavsarkar commented on May 16, 2024

I am writing some custom logic on top of the chessboard and for that I need to the onPieceDrop event to be triggered. However, for the case, where a pawn can promote, it is not triggering the onPieceDrop event.

from react-chessboard.

DevArches avatar DevArches commented on May 16, 2024

Are you doing anything with the onPromotionPieceSelect? Removing it and leaving it as default should work if you aren't using click to move. If you are what I did to fix it is to conditionally set the onPromotionPieceSelect to what I needed only if my promotionDialog state is set to open.

from react-chessboard.

kaustavsarkar avatar kaustavsarkar commented on May 16, 2024

Yes, I am using onPromotionPieceSelect to check which piece is being selected by the player. I was expecting to get the onPieceDrop event to determine if the promotion is valid or not and depending on that I could have shown the modal. Since, without this check, the chessboard does not check for valid move for promotion.

Upon playing around with the stories in the repo, I figure that onPieceDrop is called once the piece is selected for promotion. Which stops me from determining a valid move.

from react-chessboard.

kaustavsarkar avatar kaustavsarkar commented on May 16, 2024

Do you think there is a way around it? Or I can create a PR if you can show me the code.

from react-chessboard.

DevArches avatar DevArches commented on May 16, 2024

You should be checking if the promotion is valid before you get that far. There is a PR that will check at least if the pawn is in the correct position. For drag and drop I check if the promotion is valid with onDragOverSquare and set showPromotionDialog to false if not valid. (I haven't 100% finished this implementation yet but seems to be fine logically). Though yes this is a very hacky workaround in my opinion.
For click to move obviously with onSquareClick. For click to move, if it is a valid promotion I set a state that is promotionDialogOpen and then conditionally set the promotion dialog open and set a custom onPromotionPieceSelect. That way onPromotionPieceSelect is always set to default unless needed.

from react-chessboard.

kaustavsarkar avatar kaustavsarkar commented on May 16, 2024

I think the first check we could have is whether the pawn promotion is even valid.

After that, I would expect to have onPieceDrop triggered (considering, the drop would happen first and then I would select a piece, also the notation cxb8=Q, can be considered as the pawn took the piece at b8 and then got promoted to Queen, or whatever). That is just my preference TBH and I am flexible the way it is implemented.

@Clariity onPromotionDialogOpen could be helpful, ig.

@DevArches I will try your option out, I think that is the only way around for now.

Thanks folks for the quick response.. :)

from react-chessboard.

Clariity avatar Clariity commented on May 16, 2024

I've done a bit more thinking about this

We cannot check if the move is valid before the promotion piece is selected because until then, no move has been made as we don't know the piece the pawn is being promoted to so onPieceDrop cannot be called with the correct number of arguments

from react-chessboard.

kaustavsarkar avatar kaustavsarkar commented on May 16, 2024

We may not need to know which piece is being selected, we just need to know if the pawn can be moved to the next square.

If you consider the above-provided FEN ("1nb1kbnr/2P2ppp/r7/2p5/3p4/8/PP3PPP/RNBQKBNR w KQk - 0 9"), the pawn is at C7 and there is a BK at C8. So basically it is not a valid move. But there is a bK at b8 which can be captured by the pawn and makes it a valid move....

chess.js can provide this information about valid moves by using something like:

 isValidMove(chess, sourceSquare, targetSquare) {
        const moves = chess.moves({ verbose: true, square: sourceSquare });

        for (const move of moves) {
            if (targetSquare === move.to) {
                return true;
            }
        }
        return false;
    }
    ```

from react-chessboard.

kaustavsarkar avatar kaustavsarkar commented on May 16, 2024

Sorry Closed this mistakenly

from react-chessboard.

Clariity avatar Clariity commented on May 16, 2024

added prop in https://github.com/Clariity/react-chessboard/releases/tag/v3.2.0

from react-chessboard.

kaustavsarkar avatar kaustavsarkar commented on May 16, 2024

Thanks a lot for the PR!! :)

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.