lichess-org / scalachess

Chess API written in scala. Immutable and free of side effects.

Home Page:https://lichess.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support impossible checks when validate position

lenguyenthanh opened this issue · comments

Similar to this functionality IMPOSSIBLE_CHECK from shakmaty.

Not sure where it should be implemented, maybe in FenReader

Variants

  • Standard: #446
  • Chess960: re-use function from Standard: #458
  • ThreeChecks: re-use function from Standard: #458
  • KingOfTheHill: re-use function from Standard: #458
  • Antichess
  • Atomic
  • Crazyhouse
  • Horde
  • Racing Kings

What's the purpose of supporting illegal checks?

It's generally not feasible to prevent all illegal (as in proof-game solving) positions. So instead, we allow all positions that meet very basic validity requirements. There are interesting positions among those, so that's nice. This is the status quo, and now also required for backwards compatibility.

Now a tricky problem: Stockfish, on paper, assumes strict legality, and does not care what happens if it is asked to analyse/play a position that violates this (producing illegal moves, bad evals, crashes, UB). So can we safely pass a given position to current and future Stockfish versions? Not feasible to answer.

Instead, we checked the currently used implementation(s) of Stockfish for the assumptions it currently actually makes. This leads to the following (somewhat arbitrary looking) criteria for valid checks, as implemented in https://docs.rs/shakmaty/0.25.0/src/shakmaty/position.rs.html#2906-2937:

  • There can be at most two checkers
  • If the last move was a two-step pawn advance, as indicated by the en passant square, then the pushed pawn must be the only checker, or it has uncovered check by a single sliding piece
  • Multiple checkers cannot be aligned on the same file/rank/diagonal

It seems likely that these criteria are pretty stable.

They should be implemented in scalachess, too. I think the proper place is the strict validation of

def playable(strict: Boolean): Boolean =

Related: lichess-org/lila#9817

commented

Is this feature expected for standard chess only?

We should implement this for all variants (one by one). And the most important one is standard.

Since the goal of this issue is to make situation validation compatible with stockfish, do we need to implement it for the other variants like AntiChess? As I understand, stockfish only evaluates standard chess position or assumes the position it receives is for standard chess.

We use fairy stockfish for variants. So it would be nice to have those validation as well.

But they can be different from Standard. Not sure Shakmaty, implement those or not. If it does, we can just port from it :D

I think you can tick off anti chess because there are no checks at all.

Also, I think the logic for standard can be reused for crazy house.

Also for Horde, I think the standard rule should also work. If the variant already has validation to make sure that white does not have a king, then we can just check if the standard rule for impossible checks is violated for the black king. The double pawn push from the first rank does not count as an en passant.

Yeah, I think you're right, that makes only Atomic (as always) left.

I read the rules for Atomic chess, and as I understand it, the rules do not change for checks, the only different rules from standard are related to captures and king being side by side. Technically, the standard logic for impossible checks should work too.

OOPS: When the kings are connected, checks do not apply.

I noticed that there is already some validation for some these impossible check positions. For example I cannot continue playing this game or analyse it in horde chess. How does Lila do this validation if its not from scala chess?

image https://lichess.org/editor/5r2/8/4k2R/8/3pP3/8/PPPP1PPP/2PPPPP1_b_-_e3_0_1?variant=horde&color=white

This is client-side validation from https://github.com/niklasf/chessops. When we're done here we should review it all for consistency.

Is this closable?