def test_queen_at_h8_and_can_move_to_H_and_8_and_diagonal_a1_to_h8(self): board = Board(False) board.load_fen("7Q/8/8/8/8/8/8/8 w kQkq - 0 1") pos = (7, 7) possible_moves = moves( pos, [ (0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), ], ) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_normal_move(self): board = Board(False) board.load_fen("8/8/8/8/8/8/8/4K3 w kQkq - 0 1") self.assertEqual(board.at((4, 0)), WK) self.assertEqual(True, bool(board.move((4, 0), (4, 1), 5))) self.assertEqual(board.at((4, 0)), None) self.assertEqual(board.at((4, 1)), WK)
def test_queen_at_a1_and_can_move_to_A_and_1_and_diagonal_a1_to_h8(self): board = Board(False) board.load_fen("8/8/8/8/8/8/8/Q7 w kQkq - 0 1") pos = (0, 0) possible_moves = moves( pos, [ (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), ], ) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_knight_can_move_to_protect_the_king2(self): board = Board(False) board.load_fen("8/8/8/4N3/2q1K3/8/8/8 w kQkq - 0 1") pos = (4, 4) possible_moves = moves(pos, [ (2, 3) ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_knight_can_move_if_it_doesnt_protect_the_king(self): board = Board(False) board.load_fen("8/8/8/4N3/8/8/3K4/8 w kQkq - 0 1") pos = (4, 4) possible_moves = moves(pos, [ (3, 2), (5, 2), (6, 3), (6, 5), (5, 6), (3, 6), (2, 5), (2, 3) ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_knight_at_a1_and_ally_at_c2_can_move_to_1_position(self): board = Board(False) board.load_fen("8/8/8/8/8/8/2N5/N7 w kQkq - 0 1") pos = (0, 0) possible_moves = moves(pos, [ (1, 2) ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_knight_at_a1_can_move_to_2_positions(self): board = Board(False) board.load_fen("8/8/8/8/8/8/8/N7 w kQkq - 0 1") pos = (0, 0) possible_moves = moves(pos, [ (2, 1), (1, 2) ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_knight_at_e5_can_move_to_8_positions(self): board = Board(False) board.load_fen("8/8/8/4N3/8/8/8/8 w kQkq - 0 1") pos = (4, 4) possible_moves = moves(pos, [ (3, 2), (5, 2), (6, 3), (6, 5), (5, 6), (3, 6), (2, 5), (2, 3) ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_rook_at_a1_with_ally_in_b1_can_move_to_all_A(self): board = Board(False) board.load_fen("8/8/8/8/8/8/8/RR6 w kQkq - 0 1") pos = (0, 0) possible_moves = moves(pos, [ (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_white_pawn_at_e7_with_enemy_at_f8_should_be_promoted_at_f8(self): board = Board(False) board.load_fen("5p2/4P3/8/8/8/8/8/8 w kQkq f3 0 1") pos = (4, 6) possible_moves = moves(pos, [(4, 7), (5, 7)]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves) board.move((4, 6), (5, 7), 5) self.assertEqual(board.at((5, 7)), "white queen")
def test_black_pawn_at_e2_with_enemy_at_f1_should_be_promoted_at_f1(self): board = Board(False) board.load_fen("8/8/8/8/8/8/4p3/5P2 b kQkq f3 0 1") pos = (4, 1) possible_moves = moves(pos, [(4, 0), (5, 0)]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves) board.move((4, 1), (5, 0), 5) self.assertEqual(board.at((5, 0)), "black queen")
def test_rook_at_h8_with_allies_in_f8_and_h6_can_move_to_g8_and_h7(self): board = Board(False) board.load_fen("5R1R/8/7R/8/8/8/8/8 w kQkq - 0 1") pos = (7, 7) possible_moves = moves(pos, [ (6, 7), (7, 6) ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_B_at_a1_and_an_b_at_e5_can_move_to_b2_and_c3_and_d4_and_e5(self): board = Board(False) board.load_fen("8/8/8/4b3/8/8/8/B7 w kQkq - 0 1") pos = (0, 0) possible_moves = moves(pos, [ (1, 1), (2, 2), (3, 3), (4, 4) ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_promotion_move(self): board = Board(False) board.load_fen("8/4P3/8/8/8/8/8/8 w kQkq - 0 1") self.assertEqual(board.at((4, 6)), WP) self.assertEqual(True, bool(board.move((4, 6), (4, 7), 5))) # ToDo: verificar. Vai ser sempre rainha? self.assertEqual(board.at((4, 7)), WQ) self.assertEqual(board.at((4, 6)), None)
def test_rook_at_a1_with_ally_in_a2_can_move_to_all_1(self): board = Board(False) board.load_fen("8/8/8/8/8/8/R7/R7 w kQkq - 0 1") pos = (0, 0) possible_moves = moves(pos, [ (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_bishop_at_a8_can_move_in_diagonal_a8_to_h1(self): board = Board(False) board.load_fen("B7/8/8/8/8/8/8/8 w kQkq - 0 1") pos = (0, 7) possible_moves = moves(pos, [ (1, 6), (2, 5), (3, 4), (4, 3), (5, 2), (6, 1), (7, 0) ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_bishop_at_h8_can_move_in_diagonal_a1_to_g7(self): board = Board(False) board.load_fen("7B/8/8/8/8/8/8/8 w kQkq - 0 1") pos = (7, 7) possible_moves = moves(pos, [ (0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_moves_that_keeps_check_are_not_allowed(self): board = Board(False) board.load_fen("8/8/8/8/8/8/4P3/r3K3 w kQkq - 0 1") expected_moves_white = set([ ((4, 0), (3, 1)), ((4, 0), (5, 1)), ]) self.assertEqual(tuples(board.possible_moves(WHITE)), expected_moves_white)
def test_piece_attack_moves(self): board = Board(False) board.load_fen("8/8/8/8/8/p7/8/R3P3 b kQkq d3 0 1") expected_moves_white = set([ ((0, 0), (0, 1)), ((0, 0), (0, 2)), ((0, 0), (1, 0)), ((0, 0), (2, 0)), ((0, 0), (3, 0)), ]) self.assertEqual(tuples(board.piece_attack_moves((0, 0))), expected_moves_white)
def test_R_at_H8_can_move_to_all_H_and_8(self): board = Board(False) board.load_fen("7R/8/8/8/8/8/8/8 w kQkq - 0 1") pos = (7, 7) possible_moves = moves(pos, [ (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_R_at_e5_can_move_to_all_E_and_5(self): board = Board(False) board.load_fen("8/8/8/4R3/8/8/8/8 w kQkq - 0 1") pos = (4, 4) possible_moves = moves(pos, [ (4, 0), (4, 1), (4, 2), (4, 3), (4, 5), (4, 6), (4, 7), (0, 4), (1, 4), (2, 4), (3, 4), (5, 4), (6, 4), (7, 4), ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_R_at_a1_can_move_to_all_A_and_1(self): board = Board(False) board.load_fen("8/8/8/8/8/8/8/R7 w kQkq - 0 1") pos = (0, 0) possible_moves = moves(pos, [ (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_bishop_at_e5_surround_by_allies_distance_2(self): board = Board(False) board.load_fen("8/2B3B1/8/4B3/8/2B3B1/8/8 w kQkq - 0 1") pos = (4, 4) possible_moves = moves(pos, [ (3, 3), (5, 5), (3, 5), (5, 3), ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_bishop_at_e5_can_move_in_diagonals_a1_to_h8_and_b8_to_h2(self): board = Board(False) board.load_fen("8/8/8/4B3/8/8/8/8 w kQkq - 0 1") pos = (4, 4) possible_moves = moves(pos, [ (0, 0), (1, 1), (2, 2), (3, 3), (5, 5), (6, 6), (7, 7), (1, 7), (2, 6), (3, 5), (5, 3), (6, 2), (7, 1), ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_get_pieces(self): board = Board(False) board.load_fen("8/8/8/5R2/5p2/8/8/8 w kQkq - 0 1") pieces = board.get_pieces() self.assertEqual(pieces[0].name, "rook") self.assertEqual(pieces[0].color, "white") self.assertEqual(pieces[0].position, (5, 4)) self.assertEqual(pieces[1].name, "pawn") self.assertEqual(pieces[1].color, "black") self.assertEqual(pieces[1].position, (5, 3))
def test_left_en_passant_move(self): board = Board(False) board.load_fen("8/3p4/8/4P3/8/8/8/8 b kQkq - 0 1") self.assertEqual(True, bool(board.move((3, 6), (3, 4), 5))) self.assertEqual(board.at((4, 4)), WP) self.assertEqual(board.at((3, 4)), BP) self.assertEqual(True, bool(board.move((4, 4), (3, 5), 5))) self.assertEqual(board.at((4, 4)), None) self.assertEqual(board.at((3, 5)), WP) self.assertEqual(board.at((3, 4)), None)
def test_clone_board(self): board = Board(False) board.load_fen("4k3/8/8/5R2/8/8/8/3R1R2 w kQkq - 0 1") clone = board.clone() self.assertEqual(clone.at((5, 4)), WR) self.assertEqual(clone.at((4, 7)), BK) self.assertEqual(clone.at((3, 0)), WR) self.assertEqual(clone.at((5, 0)), WR) self.assertEqual(clone.color(), board.color()) self.assertEqual(clone.get_hash(), board.get_hash())
def test_kingside_castling_move(self): board = Board(False) board.load_fen("4k2r/8/8/8/8/8/8/8 b kQkq - 0 1") self.assertEqual(board.at((4, 7)), BK) self.assertEqual(board.at((7, 7)), BR) self.assertEqual(True, bool(board.move((4, 7), (6, 7), 5))) self.assertEqual(board.at((4, 7)), None) self.assertEqual(board.at((7, 7)), None) self.assertEqual(board.at((6, 7)), BK) self.assertEqual(board.at((5, 7)), BR)
def test_R_at_a1_and_r_at_d1_and_a4_cmup_to_enemy_position_including(self): board = Board(False) board.load_fen("8/8/8/8/r7/8/8/R2r w kQkq - 0 1") pos = (0, 0) possible_moves = moves(pos, [ (0, 1), (0, 2), (1, 0), (2, 0), (0, 3), (3, 0), ]) self.assertEqual(tuples(board.piece_moves(pos)), possible_moves)
def test_hindered_in_a_board_with_just_a_knight(self): board = Board(False) board.load_fen("8/8/8/4N3/8/8/8/8 w kQkq - 0 1") expected_hindered_white = set([ (3, 2), (5, 2), (6, 3), (6, 5), (5, 6), (3, 6), (2, 5), (2, 3) ]) expected_hindered_black = set() self.assertEqual(board.hindered(WHITE), expected_hindered_white) self.assertEqual(board.hindered(BLACK), expected_hindered_black) self.assertEqual(board.hindered(-1), expected_hindered_white)