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

Premoves #36

Closed
zarinda opened this issue Apr 26, 2022 · 1 comment
Closed

Premoves #36

zarinda opened this issue Apr 26, 2022 · 1 comment

Comments

@zarinda
Copy link

zarinda commented Apr 26, 2022

Hello, this is good chess board, but I can not use premoves, for example I play with white pieces and I do a move and my pieces to disable it is good, But when my opponent thinking I want to do a premove but I do not how can I do it, so it is very important tool because in blitzkrieg game when play in 2 or 3 minutes this tool is very important.

@mganjoo
Copy link
Owner

mganjoo commented Apr 26, 2022

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.

@mganjoo mganjoo closed this as completed Apr 26, 2022
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