Skip to content

Commit 4f1e5da

Browse files
committed
Fix parsing of castling moves in variant 960 PGNs (fixes niklasf#705)
1 parent 2dbb07b commit 4f1e5da

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

chess/pgn.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,12 +1523,14 @@ def read_game(handle: TextIO, *, Visitor: Any = GameBuilder) -> Any:
15231523
# Initial position.
15241524
fen = headers.get("FEN", VariantBoard.starting_fen)
15251525
try:
1526-
board_stack = [VariantBoard(fen, chess960=headers.is_chess960())]
1526+
board = VariantBoard(fen, chess960=headers.is_chess960())
15271527
except ValueError as error:
15281528
visitor.handle_error(error)
15291529
skipping_game = True
15301530
else:
1531-
visitor.visit_board(board_stack[0])
1531+
board.chess960 = board.chess960 or board.has_chess960_castling_rights()
1532+
board_stack = [board]
1533+
visitor.visit_board(board)
15321534

15331535
# Fast path: Skip entire game.
15341536
if skipping_game:

test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3915,6 +3915,18 @@ def test_atomic_validity(self):
39153915
board = chess.variant.AtomicBoard("3N1NB1/2N1Q1N1/3RkR2/2NP1PN1/3NKN2/8/8/n7 w - - 0 1")
39163916
self.assertEqual(board.status(), chess.STATUS_VALID)
39173917

3918+
def test_atomic960(self):
3919+
pgn = io.StringIO(textwrap.dedent("""\
3920+
[Variant "Atomic"]
3921+
[FEN "rkrbbnnq/pppppppp/8/8/8/8/PPPPPPPP/RKRBBNNQ w KQkq - 0 1"]
3922+
3923+
1. g3 d5 2. Nf3 e5 3. Ng5 Bxg5 4. Qf3 Ne6 5. Qa3 a5 6. d4 g6 7. c3 h5 8. h4 Qh6 9. Bd2 Qxd2 10. O-O-O *
3924+
"""))
3925+
game = chess.pgn.read_game(pgn)
3926+
self.assertTrue(game.board().chess960)
3927+
self.assertEqual(game.end().parent.board().fen(), "rkr1b1n1/1pp2p2/4n1p1/p2pp2p/3P3P/Q1P3P1/PP2PP2/RK3N2 w Qkq - 0 10")
3928+
self.assertEqual(game.end().board().fen(), "rkr1b1n1/1pp2p2/4n1p1/p2pp2p/3P3P/Q1P3P1/PP2PP2/2KR1N2 b kq - 1 10")
3929+
39183930

39193931
class RacingKingsTestCase(unittest.TestCase):
39203932

0 commit comments

Comments
 (0)