def test_en_passant_after_turn_expires(self): g = Game(PlayerType.HUMAN, PlayerType.HUMAN) g.apply_record('1. e4 f5 2. e5 d5 3. a4 a5 ') legal_moves = g.get_all_legal_moves(Color.WHITE) for m in legal_moves: if type(m) == EnPassantMove: self.assert_('En passant not allowed here')
def test_en_passant_on_correct_turn(self): g = Game(PlayerType.HUMAN, PlayerType.HUMAN) g.apply_record('1. e4 f5 2. e5 d5 ') legal_moves = g.get_all_legal_moves(Color.WHITE) found = False for m in legal_moves: if type(m) == EnPassantMove: found = True if not found: self.assert_('En passant not found in legal moves')
def test_can_short_castle(self): g = Game(PlayerType.HUMAN, PlayerType.HUMAN) g.apply_record('1. e4 e5 2. Bd3 d5 3. Nf3 f5 4. a4 g5') self.assertTrue(g.can_castle(Color.WHITE, True)) self.assertFalse(g.can_castle(Color.WHITE, False)) self.assertFalse(g.can_castle(Color.BLACK, True)) self.assertFalse(g.can_castle(Color.BLACK, False))
def play_constructed_game(g: castle.Game): g.board.pretty_print() while not g.finished: print(f'white short {g.can_castle(castle.Color.WHITE, True)}') print(f'white long {g.can_castle(castle.Color.WHITE, False)}') print(f'black short {g.can_castle(castle.Color.BLACK, True)}') print(f'black long {g.can_castle(castle.Color.BLACK, False)}') g.play_turn() winning_prefix = f'Game over by ' if g.winner == castle.Winner.DRAW: winning_prefix += 'stalemate' else: winning_prefix += 'checkmate' winning_text = f'{winning_prefix}. Winner: {g.winner.name.title()}' print(winning_text)
def test_can_castle_into_check(self): g = Game(PlayerType.HUMAN, PlayerType.HUMAN) g.apply_record('1. f4 e5 2. Nh3 g5 3. g4 f5 4. Bg2 Bc5') # can castle now self.assertTrue(g.can_castle(Color.WHITE, True)) # attack g1, which the king moves to g.apply_notation('Bc5') # can't castle through check self.assertFalse(g.can_castle(Color.WHITE, True))
def test_can_castle_through_check(self): g = Game(PlayerType.HUMAN, PlayerType.HUMAN) g.apply_record('1. e4 b6 2. Nf3 e5 3. g3 d5 4. Bg2 ') # can castle now self.assertTrue(g.can_castle(Color.WHITE, True)) # attack f1, which the king would pass through g.apply_notation('Ba6') # can't castle through check self.assertFalse(g.can_castle(Color.WHITE, True))
def test_can_castle_in_check(self): g = Game(PlayerType.HUMAN, PlayerType.HUMAN) g.apply_record('1. e4 e5 2. d4 f5 3. Bd3 d5 4. Nf3 c5 5. c4 ') # can castle now self.assertTrue(g.can_castle(Color.WHITE, True)) # put white in check g.apply_notation('Qa5') # can't castle out of check self.assertFalse(g.can_castle(Color.WHITE, True))
def test_castle_after_rook_movement(self): g = Game(PlayerType.HUMAN, PlayerType.HUMAN) g.apply_record('1. e4 e5 2. Bd3 d5 3. Nf3 f5 4. Rf1 g5 5. Rh1 c5') # white has moved rook self.assertFalse(g.can_castle(Color.WHITE, True)) # test all the others just to be safe self.assertFalse(g.can_castle(Color.WHITE, False)) self.assertFalse(g.can_castle(Color.BLACK, True)) self.assertFalse(g.can_castle(Color.BLACK, False))
def test_can_long_castle(self): g = Game(PlayerType.HUMAN, PlayerType.HUMAN) g.apply_record('1. d4 d5 2. e4 e5 3. Bf4 Qd7 4. Qg4 c6 5. Nc3') self.assertTrue(g.can_castle(Color.WHITE, False)) self.assertFalse(g.can_castle(Color.WHITE, True)) self.assertFalse(g.can_castle(Color.BLACK, True)) self.assertFalse(g.can_castle(Color.BLACK, False))
def test_king_moves(self): from castle import MoveParser g = Game(PlayerType.HUMAN, PlayerType.HUMAN) g.board.clear() # obstructed king with threat of check board = g.board board.place_piece(Piece(PieceType.KING, Color.WHITE), 'h1') board.place_piece(Piece(PieceType.KNIGHT, Color.BLACK), 'h2') board.place_piece(Piece(PieceType.ROOK, Color.BLACK), 'h3') self.assertEqual( { MoveParser.parse_move(g, 'Kg2'), MoveParser.parse_move(g, 'Kg1'), }, g.get_all_legal_moves(Color.WHITE)) # obstructed king without threat of check g.board.clear() board.place_piece(Piece(PieceType.KING, Color.WHITE), 'h1') board.place_piece(Piece(PieceType.KNIGHT, Color.BLACK), 'h2') self.assertEqual( { MoveParser.parse_move(g, 'Kg2'), MoveParser.parse_move(g, 'Kg1'), MoveParser.parse_move(g, 'Kh2'), }, g.get_all_legal_moves(Color.WHITE)) # unobstructed king g.board.clear() board.place_piece(Piece(PieceType.KING, Color.BLACK), 'e4') self.assertEqual( { board.square_from_notation('d5'), board.square_from_notation('e5'), board.square_from_notation('d4'), board.square_from_notation('f5'), board.square_from_notation('d3'), board.square_from_notation('f4'), board.square_from_notation('e3'), board.square_from_notation('f3'), }, board.get_moves(board.square_from_notation('e4')))
def test_en_passant_white(self): g = Game(PlayerType.HUMAN, PlayerType.HUMAN) g.apply_record('1. e4 f5 2. e5 d5 ') g.apply_notation('exd6') unsafe = g.board.square_from_notation('d5') target = g.board.square_from_notation('d6') attack = g.board.square_from_notation('e5') # attacker moved from here self.assertIsNone(attack.occupant) # to here self.assertEqual(Piece(PieceType.PAWN, Color.WHITE), target.occupant) # unsafe square was captured self.assertIsNone(unsafe.occupant)
def test_en_passant_black(self): g = Game(PlayerType.HUMAN, PlayerType.HUMAN) g.apply_record('1. a4 d5 2. c4 d4 3. e4 ') g.apply_notation('dxe3') unsafe = g.board.square_from_notation('e4') target = g.board.square_from_notation('e3') attack = g.board.square_from_notation('d4') # attacker moved from here self.assertIsNone(attack.occupant) # to here self.assertEqual(Piece(PieceType.PAWN, Color.BLACK), target.occupant) # unsafe square was captured self.assertIsNone(unsafe.occupant)
def test_castle_obstructed(self): g = Game(PlayerType.HUMAN, PlayerType.HUMAN) self.assertFalse(g.can_castle(Color.WHITE, True)) self.assertFalse(g.can_castle(Color.WHITE, False)) self.assertFalse(g.can_castle(Color.BLACK, True)) self.assertFalse(g.can_castle(Color.BLACK, False))