Skip to content

Commit

Permalink
Tweak castling and en passant abilities
Browse files Browse the repository at this point in the history
  • Loading branch information
josephlou5 committed Jun 9, 2023
1 parent d98ebfe commit 777c693
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- Added command-line parsing to allow launching a game between builtin players
- Added threefold repetition rule
- Tweaked castling and en passant abilities
- 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.

## v2.1.0

Expand Down
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,27 @@ Here is a [description of the concept and rules][rules].
Notable game rules:

- **Castling**: A king and rook may only castle if neither has moved already,
the king is not in check, all squares between them are vacant on both boards,
and the king does not move through check or into check. After the castle, both
pieces will teleport to the other board.
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.
After the castle, both pieces will teleport to the other board.
- **En passant**: A pawn may capture another pawn through en passant if your
pawn is on board B and the enemy pawn advances two spaces, teleporting to the
space right next to yours on board B. (This results in a situation that looks
pawn is on Board B and the enemy pawn advances two spaces, teleporting to the
space right next to yours on Board B. (This results in a situation that looks
like regular en passant.) Note that due to teleporting to the other board
after each move, this can only be achieved by a pawn that _does not_ advance
two squares on its first move.
two squares on its first move. Also, if there is a piece on Board B where the
en passant move would go (i.e., your pawn can already capture a piece
normally), then en passant will not take place.
- **Fifty move rule**: If both players have made 50 moves each where no piece
has been captured or no pawn moved, then a player may claim a draw. However,
to simplify this case, the game will be automatically ended with a draw
(rather than allowing a player to claim a draw), although this is not the
official rule. This therefore overshadows the 75-move rule, where a draw is
automatically applied after 75 moves by both players with no captures or pawn
movements.
(rather than allowing a player to claim a draw). This therefore overshadows
the 75-move rule, where a draw is automatically applied after 75 moves by both
players with no captures or pawn movements.
- **Threefold repetition rule**: If a board position appears three times in a
game (not necessarily in a row), then a player may claim a draw. However, to
simplify this case, the game will be automatically ended with a draw (rather
than allowing a player to claim a draw).

## How to play

Expand Down
18 changes: 11 additions & 7 deletions src/alicechess/_moves_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,19 @@ def __init__(
# right rook: king will move to the right
dc = 1
valid_castle = True
# check that all the spaces in the middle are empty on
# both boards
# check that the spaces in the middle are empty
c = kc + dc
while c != rc:
if (bn, kr, c) in self._board:
# there's a piece there
valid_castle = False
break
if (1 - bn, kr, c) in self._board:
# there's a piece there on the other board
# there's a piece there on this board
valid_castle = False
break
if abs(c - kc) <= 2:
# the spaces being landed on must be empty on
# the other board as well
if (1 - bn, kr, c) in self._board:
valid_castle = False
break
c += dc
if not valid_castle:
continue
Expand Down Expand Up @@ -271,6 +272,9 @@ def __init__(
continue
capture_r = r + pawn.dr
capture_pos = (capture_r, pc)
if any((bn, *capture_pos) in self._board for bn in range(2)):
# there's a piece there; no en passant
continue
if self._move_in_check(
pawn.pos, capture_pos, en_passant=(pr, pc)
):
Expand Down

0 comments on commit 777c693

Please sign in to comment.