Code Monkey home page Code Monkey logo

Comments (1)

mganjoo avatar mganjoo commented on June 24, 2024

Hi, thanks for checking out the library!

Pre-moves are a good application, but I do not have plans to add it to the core library as that involves state management, and this chessboard is not intended to maintain complex internal state.

However, I think it should be possible to implement pre-moves in your application using this library, assuming you manage state yourself in your application. Here's a potential way to do it:

(Assuming Player A = white, Player B = black)

  • Start with turn = white (Player A). When Player A makes a move, then update your copy of the game state (FEN) accordingly. It is now Player B's turn to make a move.
  • On Player B's board, set turn = black, so that Player B can make a move freely.
  • On Player A's board, set turn = white, so that Player A can also move. This isn't a real move, though - just a pre-move. You can customize styles so that moves appear in a different color. A detailed set of CSS styling properties is available on the API page.

Now, you have to handle two different events (you can use a combination of movestart or movefinished for this - details on API page):

  • For Player A (the person who can pre-move): when they move a piece, record the move in your application state, and also update board FEN for player A to reflect this move (we will revert this after Player B makes a move). Style board accordingly so that Player A knows that this is just a pre-move.
  • For Player B (the person whose turn it is), when they move a piece, update board FEN for Player A.
  • Immediately after that (or after a short delay), check if recorded move from Player A is valid:
  • If the pre-move was valid, update game FEN again. Use this new FEN to change board display for both Player A and B. It is now B's turn again, so set turn = black.
  • If the pre-move was not valid, then don't change the game FEN any further. Use the existing FEN to override board display for Player A, since their pre-move was ignored. Now it is Player A's turn, so set turn = white.

Let me know if the above makes sense! Closing issue.

from gchessboard.

Related Issues (18)

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.