Chess's "can't make any move that results in your king being in check" rule is surprisingly hard to implement.

As far as I can tell, to generate the list of valid moves for a piece, you kind of have to just clone the board a bunch of times, make each potential move, and then check for check on the resulting board state.

The obvious optimization (don't *clone* the board, just check if the target square is under attack) does not work because the king itself may be in the way (consider e.g. rook on a1, king on b1, Kc1 is not legal even though the rook currently can't get there).

Sign in to participate in the conversation

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!