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 make illigal moves using chess.js? #434

Open
eliyahu119 opened this issue Aug 23, 2023 · 4 comments
Open

How to make illigal moves using chess.js? #434

eliyahu119 opened this issue Aug 23, 2023 · 4 comments

Comments

@eliyahu119
Copy link

Hi, I would like to know how to make illigal moves using this library,
I have seen sloppy:True, but it seems to be removed from the project.

@hasnul
Copy link

hasnul commented Mar 8, 2024

Curious. How would this work?

  • It would require suspending chess rules momentarily whilst sus moves are made and then restoring the rules. The "game state" before this foray into criminality would need to be saved.
  • When the rules are restored, the legality of the position would need to be checked. If it is illegal, then what? What should chess.js do? Restore the game state saved prior to rule-breaking actions?
  • Assuming a legal resulting position, the game state would need to be reloaded from fen probably.
  • Game history, as implemented by chess.js, would no longer be viable and undo() would not work since it relies on observing of chess rules. Can't undo from an arbitrary position that was obtained illegally to an earlier position. So history would have to be discarded? undo() can restore earlier game state, maybe.Unless, an indication of something like "from fantasy" is made somewhere, undoing from an illegally obtained position may be jarring because the undo() is not reversing legal chess move(s).
  • There may be issues in recording the moves played in the game.

Perhaps this is a frontend problem, not a chess.js one.

@eliyahu119
Copy link
Author

eliyahu119 commented Mar 13, 2024

  1. can you name a game state that breaks the game when loaded?
  2. why? the whole point is that I could keep truck of the moves.
  3. I think it's part of the issue isn't it? if there is a solution it must include that. this is why I want it to be part of chess.js in the first place and not built external tool.
  • sidenote, stockfish has this feature, you can play openly without rules and then stockfish will
    play accordingly to the new position regardless of the move you just took.
  • if some rules are not breakable keep them, but all rules are?

@dhirallin
Copy link

dhirallin commented May 19, 2024

It may be a bit of a case of us wanting our cake and eating it too.
I can tell you about my use-case. I've been implementing various chess variants into my interface using chess.js.
Chess.js is mostly used to interpret algebraec notation and validates normal moves.
I managed to implement fischer random. Although it was tricky.
Basically I use chess.js to validate most moves, except when the user is castling. In which case I manually modify the FEN.
However you run into problems where chess.js starts complaining about the castling rights being wrong. I managed to hack around that by removing castling rights before using chess.js to validate regular moves and then restore the castling rights afterwards. My implementation works, but it was defintiely a pain in the ass to implement.
Next I wanted to implement atomic, and suicide variants. These variants follow almost all the rules of regular chess, except they allow kings to touch each other. I couldn't find any decent hack for getting this to work with chess.js. I thought about removing kings and moving them to a basically random valid square before validating regular moves and then replacing them, but the code just got horribly messy and I determined it was basically impossible to implement Suicide chess that way. So now I'm looking at patching chess.js to remove certain checking. What you used to describe as 'semi-legal moves', in older versions.
Sure I could get rid of chess.js completely for these variants, but most of the moves in these variants are regular chess moves with regular rules and it would be an even bigger pain in the butt to manually interpret and validate all those moves.

@dhirallin
Copy link

dhirallin commented Oct 16, 2024

I can see the issue for illegal game states. For this to be useful there are two possible options. Either just turn off all rule checking for a game permanently (other than how the pieces move, algebraec notation etc). But then if you wanted to keep some rules selectively, the front end would have to manually validate them itself which would be a pain. Or alternatively have the ability to select which rules are turned on or off, but that's obviously pretty complicated for the scope of chess.js. It still seems like the only way to implement variants in chess.js, is to patch the source yourself in order to selectively remove certain validation checks.

If you are just talking about moving pieces in a non-standard way, e.g. moving a knight in a straight line instead of an L-shape, in such a way where the resulting position is legal -- I guess that's hypothetically possible, to implement in chess.js, it would probably require storing the moves in the move list using some long notation instead of SAN. You could just use remove() and put(), then validate the FEN aftewards and have your own move list in the front end. Since for that use case there's not much use for chess.js. It's not hard to implement your own 3-fold repetition checking etc.

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

3 participants