Skip to content

Commit

Permalink
Tweak possible move calculations
Browse files Browse the repository at this point in the history
The move must be valid on the current board, but not necessarily on both
boards. This means that a piece may teleport into a position to block
check, since the king was not in check on their original board and thus
their move was valid on their original board.
  • Loading branch information
josephlou5 committed Jun 9, 2023
1 parent 777c693 commit 4ad266f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
- According to the linked rules (should have read it thoroughly!), there were
some potentially incorrect moves (either missing or unnecessarily present)
that could have been generated.
- Tweaked possible move calculations: move must be valid on current board, but
not necessarily on both boards
- According to the linked rules (should have read it thoroughly!!!), a piece
may teleport into a position to block check, since the move was valid on the
board it was on (the king was not in check on that board), and the move is
legal because the king is no longer in check.

## v2.1.0

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Here is a [description of the concept and rules][rules].

Notable game rules:

- A piece's move must be valid on the board it is on, which means that a piece
on Board B can block a check on Board A after teleporting (since the move was
valid on Board B, and the move overall was legal because the king is not
staying in check). See the above link for a more detailed argument on this.
- **Castling**: A king and rook may only castle if neither has moved already,
the king is not in check, the squares they will move to are vacant on both
boards, and the king does not move through check (on Board A) or into check.
Expand Down
8 changes: 6 additions & 2 deletions src/alicechess/_moves_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,12 @@ def _move_in_check(
check_pos = here_target_pos
else:
check_pos = king.pos
if _is_threatened(board, enemy_color, *check_pos):
return True
if moving_king or pos.bn == king.pos.bn:
# if the king is moving, can't move into check
# otherwise, if on the same board as the king, can't let the
# king be in check
if _is_threatened(board, enemy_color, *check_pos):
return True
# make sure move is valid after teleporting
board[there_target_pos] = board.pop(here_target_pos).move_to(
there_target_pos
Expand Down

0 comments on commit 4ad266f

Please sign in to comment.