def test_en_passant_non_pawn_moves_two_squares(self): """ Even if a non-pawn moves two spaces, en passant should be None. """ game = GameState() game.board['a']['1'] = 'R' game._make_move('a1a3') self.assertEqual(game.en_passant, None)
def test_en_passant_pawn_moves_two_squares(self): """ When a pawn moves two spaces, the square behind it should be set as the en passant square. """ game = GameState() game.board['a']['2'] = 'P' game._make_move('a2a4') self.assertEqual(game.en_passant, ('a', '3'))
def test_en_passant_pawn_moves_one_square(self): """ If pawn moves but it does not move two squares, en passant should be None. """ game = GameState() game.board['a']['2'] = 'P' game._make_move('a2a3') self.assertEqual(game.en_passant, None)
def test_black_turn_to_white(self): """ When black makes a move, the turn tracker should be set to white's turn. """ game = GameState() game.player = 'b' game._make_move('a1a2') self.assertEqual(game.player, 'w')
def test_white_turn_to_black(self): """ When white makes a move, the turn tracker should be set to black's turn. """ game = GameState() game.player = 'w' game._make_move('a1a2') self.assertEqual(game.player, 'b')
def test_en_passant_only_lasts_one_turn(self): """ Even if there was previously an en passant square, if no pawn moved two spaces this turn, en passant should be set to None. """ game = GameState() game.board['a']['1'] = 'R' game.en_passant = ('a', '3') game._make_move('a1a2') self.assertEqual(game.en_passant, None)
def test_castling_move_black_queen_side_rook(self): """ Moving black queen-side rook should disable it for castling. """ game = GameState() game.board['a']['8'] = 'r' game.board['a']['7'] = GameState.EMPTY game.castle_black_queen = True game._make_move('a8a7') self.assertFalse(game.castle_black_queen)
def test_castling_move_black_king(self): """Moving black king should disable all black castling.""" game = GameState() game.board['e']['8'] = 'k' game.board['e']['7'] = GameState.EMPTY game.castle_black_king = True game.castle_black_queen = True game._make_move('e8e7') self.assertFalse(game.castle_black_king) self.assertFalse(game.castle_black_queen)
def test_castling_move_white_queen_side_rook(self): """ Moving white queen-side rook should disable it for castling. """ game = GameState() game.board['a']['1'] = 'R' game.board['a']['2'] = GameState.EMPTY game.castle_white_queen = True game._make_move('a1a2') self.assertFalse(game.castle_white_queen)
def test_castling_move_white_king_side_rook(self): """ Moving white king-side rook should disable it for castling. """ game = GameState() game.board['h']['1'] = 'R' game.board['h']['2'] = GameState.EMPTY game.castle_white_king = True game._make_move('h1h2') self.assertFalse(game.castle_white_king)
def test_castling_move_white_king(self): """Moving white king should disable all white castling.""" game = GameState() game.board['e']['1'] = 'K' game.board['e']['2'] = GameState.EMPTY game.castle_white_king = True game.castle_white_queen = True game._make_move('e1e2') self.assertFalse(game.castle_white_king) self.assertFalse(game.castle_white_queen)
def test_castling_move_black_king_side_rook(self): """ Moving black king-side rook should disable it for castling. """ game = GameState() game.board['h']['8'] = 'r' game.board['h']['7'] = GameState.EMPTY game.castle_black_king = True game._make_move('h8h7') self.assertFalse(game.castle_black_king)
def test_halfmove_clock_resets_on_pawn_move(self): """ When a player moves a pawn, the halfmove clock should be reset to 0. """ game = GameState() game.board['a']['2'] = 'P' game.board['a']['3'] = GameState.EMPTY game.halfmove_clock = 3 game._make_move('a2a3') self.assertEqual(game.halfmove_clock, 0)
def test_halfmove_clock_resets_on_capture(self): """ When a player captures a piece, the halfmove clock should be reset to 0. """ game = GameState() game.board['a']['2'] = 'r' game.board['a']['3'] = 'R' game.halfmove_clock = 3 game._make_move('a2a3') self.assertEqual(game.halfmove_clock, 0)
def test_halfmove_clock_increments_after_black_player_makes_move(self): """ When either player makes a reversible move, the halfmove clock should be incremented. This tests for moves made by the black player. """ game = GameState() game.board['a']['2'] = 'r' game.halfmove_clock = 3 game._make_move('a2a3') self.assertEqual(game.halfmove_clock, 4)
def test_castling_move_other_piece(self): """ Moving any piece other than a king or rook should not disable castling. """ game = GameState() game.board['a']['8'] = 'b' game.board['b']['2'] = GameState.EMPTY game.castle_black_king = True game.castle_black_queen = True game._make_move('a1b2') self.assertTrue(game.castle_black_king) self.assertTrue(game.castle_black_queen)
def test_correctly_handles_capturing(self): """ When one piece lands on another, the other should be removed from the board, and moving piece should replace it. """ g = GameState() g.board = { 'a': {'1': 'R', '2': 'P', '3': ' ', '4': ' ', '5': ' ', '6': ' ', '7': 'p', '8': 'r'}, # noqa 'b': {'1': 'N', '2': 'P', '3': ' ', '4': ' ', '5': ' ', '6': ' ', '7': 'p', '8': 'n'}, # noqa 'c': {'1': 'B', '2': 'P', '3': ' ', '4': ' ', '5': 'p', '6': ' ', '7': ' ', '8': 'b'}, # noqa 'd': {'1': 'Q', '2': 'P', '3': ' ', '4': ' ', '5': 'p', '6': ' ', '7': ' ', '8': 'q'}, # noqa 'e': {'1': 'K', '2': ' ', '3': ' ', '4': 'P', '5': ' ', '6': ' ', '7': 'p', '8': 'k'}, # noqa 'f': {'1': 'B', '2': 'P', '3': 'N', '4': ' ', '5': ' ', '6': ' ', '7': 'p', '8': 'b'}, # noqa 'g': {'1': ' ', '2': 'P', '3': ' ', '4': ' ', '5': ' ', '6': ' ', '7': 'p', '8': 'n'}, # noqa 'h': {'1': 'R', '2': 'P', '3': ' ', '4': ' ', '5': ' ', '6': ' ', '7': 'p', '8': 'r'}, # noqa } g._make_move('e4d5') expected = """ --------------------------------- 8 | r | n | b | q | k | b | n | r | |-------------------------------| 7 | p | p | | | p | p | p | p | |-------------------------------| 6 | | | | | | | | | |-------------------------------| 5 | | | p | P | | | | | |-------------------------------| 4 | | | | | | | | | |-------------------------------| 3 | | | | | | N | | | |-------------------------------| 2 | P | P | P | P | | P | P | P | |-------------------------------| 1 | R | N | B | Q | K | B | | R | --------------------------------- a b c d e f g h """.strip('\n') # the outer newlines are only there to make this code readable self.assertEqual(g.board_text, expected)
def test_make_basic_move(self): """ Should be able to handle moving a piece from one square to another. """ g = GameState() g.board = { 'a': {'1': 'R', '2': 'P', '3': ' ', '4': ' ', '5': ' ', '6': ' ', '7': 'p', '8': 'r'}, # noqa 'b': {'1': 'N', '2': 'P', '3': ' ', '4': ' ', '5': ' ', '6': ' ', '7': 'p', '8': 'n'}, # noqa 'c': {'1': 'B', '2': 'P', '3': ' ', '4': ' ', '5': 'p', '6': ' ', '7': ' ', '8': 'b'}, # noqa 'd': {'1': 'Q', '2': 'P', '3': ' ', '4': ' ', '5': ' ', '6': ' ', '7': 'p', '8': 'q'}, # noqa 'e': {'1': 'K', '2': ' ', '3': ' ', '4': 'P', '5': ' ', '6': ' ', '7': 'p', '8': 'k'}, # noqa 'f': {'1': 'B', '2': 'P', '3': 'N', '4': ' ', '5': ' ', '6': ' ', '7': 'p', '8': 'b'}, # noqa 'g': {'1': ' ', '2': 'P', '3': ' ', '4': ' ', '5': ' ', '6': ' ', '7': 'p', '8': 'n'}, # noqa 'h': {'1': 'R', '2': 'P', '3': ' ', '4': ' ', '5': ' ', '6': ' ', '7': 'p', '8': 'r'}, # noqa } g._make_move('a2a4') expected = """ --------------------------------- 8 | r | n | b | q | k | b | n | r | |-------------------------------| 7 | p | p | | p | p | p | p | p | |-------------------------------| 6 | | | | | | | | | |-------------------------------| 5 | | | p | | | | | | |-------------------------------| 4 | P | | | | P | | | | |-------------------------------| 3 | | | | | | N | | | |-------------------------------| 2 | | P | P | P | | P | P | P | |-------------------------------| 1 | R | N | B | Q | K | B | | R | --------------------------------- a b c d e f g h """.strip('\n') # the outer newlines are only there to make this code readable self.assertEqual(g.board_text, expected)
def test_fullmove_number_increments_when_black_takes_a_turn(self): game = GameState() game.player = 'b' game.fullmove_number = 3 game._make_move('a2a3') self.assertEqual(game.fullmove_number, 4)
def test_fullmove_number_does_not_increment_when_white_moves(self): game = GameState() game.player = 'w' game.fullmove_number = 3 game._make_move('a2a3') self.assertEqual(game.fullmove_number, 3)
def test_en_passant_non_pawn_moves(self): """When a non-pawn piece moves, en passant should be None.""" game = GameState() game.board['a']['1'] = 'R' game._make_move('a1a2') self.assertEqual(game.en_passant, None)