def test_make_move_pawn_promotion(self): """Verifies that making promition moves work appropriately.""" board, white = Board(), Player(Color.W) pawn_white = Pawn(white, [3, 1]) # d7 board.add_to_board(pawn_white) board.make_move(pawn_white, [3, 'Q']) # promotion at d8 square # ensure the new piece is a queen. self.assertTrue(isinstance(board.get_piece_at_position([3, 0]), Queen))
def test_get_piece_at_position(self): """Fetch a piece we add to the board, fetch an empty square and make sure we get None.""" board, white = Board(), Player(Color.W) pos_white = [4, 6] # e2 pawn_white = Pawn(white, pos_white) board.add_to_board(pawn_white) self.assertTrue(board.get_piece_at_position(pos_white) is pawn_white) self.assertTrue(board.get_piece_at_position([1, 1]) is None)
def test_make_move_pawn_double_jump(self): """Verifies that making a double jump opens up the piece for en passant.""" board, white = Board(), Player(Color.W) pawn_pos_white = [5, 6] # f2 pawn_white = Pawn(white, pawn_pos_white) board.add_to_board(pawn_white) board.make_move(pawn_white, [5, 4]) self.assertTrue(board.en_passant is pawn_white)
def test_check_if_empty(self): """Make sure check_if_empty returns True at first, and then False when we add a piece.""" board, white = Board(), Player(Color.W) pos_white = [4, 6] # e2 pawn_white = Pawn(white, pos_white) self.assertTrue(board.check_if_empty(pos_white)) board.add_to_board(pawn_white) self.assertFalse(board.check_if_empty(pos_white))
def test_remove_a_piece(self): """Add a piece, then remove it, and verify space is empty""" board, white = Board(), Player(Color.W) pos_white = [4, 6] # e2 pawn_white = Pawn(white, pos_white) board.add_to_board(pawn_white) # emptySpace self.assertFalse(board.check_if_empty(pos_white)) board.remove_from_board(pos_white) self.assertTrue(board.check_if_empty(pos_white))
def test_make_move_simple(self): """Makes a simple move, verifies values are appropriately updated.""" board, white = Board(), Player(Color.W) rook_white = Rook(white, [3, 2]) # d6 board.add_to_board(rook_white) board.make_move(rook_white, [5, 2]) # f6 # ensure make_move updated piece position self.assertTrue(rook_white.position == [5, 2]) # ensure make_move updated board position self.assertTrue(board.get_piece_at_position([5, 2]) is rook_white) self.assertTrue(board.get_piece_at_position([3, 2]) is None)
def test_board_with_passed_in_board(self): """Tests the creation of a board using a previous board's array. Each peice should have a copy created.""" board, white = Board(), Player(Color.W) pawn_white = Pawn(white, [3, 2]) # d6 board.add_to_board(pawn_white) new_board = Board(board.board) self.assertEqual(len(new_board.pieces), 1) self.assertFalse(new_board.check_if_empty([3, 2])) self.assertFalse(new_board.get_piece_at_position([3, 2]) is pawn_white)
def test_pawn_white_simple(self): """Tests that a simple forward move is in the pawn's move list.""" board, white = Board(), Player(Color.W) pawn_white = Pawn(white, [3, 4]) # d4 pawn_white.first_move = False board.add_to_board(pawn_white) correct_move = [3, 3] # d5 returned_moves = pawn_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 1) self.assertTrue(correct_move in returned_moves)
def test_pawn_black_simple(self): """Tests that a simple forward move is in the pawn's move list.""" board, black = Board(), Player(Color.B) pawn_black = Pawn(black, [3, 4]) # d4 pawn_black.first_move = False board.add_to_board(pawn_black) correct_move = [3, 5] # d3 returned_moves = pawn_black.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 1) self.assertTrue(correct_move in returned_moves)
def test_make_move_pawn_simple(self): """Makes a simple pawn move, verifies values are appropriately updated.""" board, white = Board(), Player(Color.W) pawn_white = Pawn(white, [3, 6]) # d2 board.add_to_board(pawn_white) board.make_move(pawn_white, [3, 4]) # d4 # ensure make_move updated piece position self.assertTrue(pawn_white.position == [3, 4]) # ensure make_move updated board position self.assertTrue(board.get_piece_at_position([3, 4]) is pawn_white) self.assertTrue(board.get_piece_at_position([3, 6]) is None)
def test_pawn_black_first_move(self): """Tests that two moves are available to a new unimpeded pawn.""" board, black = Board(), Player(Color.B) pawn_black = Pawn(black, [3, 1]) # d7 board.add_to_board(pawn_black) correct_move1 = [3, 2] # d6 correct_move2 = [3, 3] # d5 returned_moves = pawn_black.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 2) self.assertTrue(correct_move1 in returned_moves) self.assertTrue(correct_move2 in returned_moves)
def test_pawn_white_first_move(self): """Tests that two moves are available to a new unimpeded pawn.""" board, white = Board(), Player(Color.W) pawn_white = Pawn(white, [3, 6]) # d2 board.add_to_board(pawn_white) correct_move1 = [3, 5] # d3 correct_move2 = [3, 4] # d4 returned_moves = pawn_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 2) self.assertTrue(correct_move1 in returned_moves) self.assertTrue(correct_move2 in returned_moves)
def test_simple(self): """Verifies that a knight's returned movelist has the correct number of moves and that the boundaries of it's movelist is correct.""" board, white = Board(), Player(Color.W) knight_white = Knight(white, [3, 4]) # d4 board.add_to_board(knight_white) # c6, e6, f5, f3, e2, c2, b3, b5 correct_move_list = [[2, 2], [4, 2], [5, 3], [5, 5], [4, 6], [2, 6], [1, 5], [1, 3]] returned_moves = knight_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 8) for move in correct_move_list: self.assertTrue(move in returned_moves)
def test_add_to_board(self): """Add a piece, verify the square is taken and that the board's pieces list has the piece in it.""" board, white = Board(), Player(Color.W) pos_white = [4, 6] # e2 pawn_white = Pawn(white, pos_white) board.add_to_board(pawn_white) # fetch the piece using xy_conversion self.assertTrue(board.board[xy_to_num(pos_white)] is pawn_white) # assure that the piece is in the board's pieces list. self.assertTrue(pawn_white in board.pieces)
def test_castling_normal(self): """Ensures that castling is properly returned in the King's moved list.""" board, white = Board(), Player(Color.W) king_white = King(white, [4, 7]) # e1 board.add_to_board(king_white) rook_white = Rook(white, [7, 7]) # h1 board.add_to_board(rook_white) castle_move = [6, 7] # g1 returned_moves = king_white.get_legal_moves(board, True) self.assertTrue(castle_move in returned_moves)
def test_path_impeded(self): """Verifies that a rook's returned moves stops when encountering another of the rook's owner's pieces.""" board, white = Board(), Player(Color.W) rook_white = Rook(white, [3, 4]) # d4 board.add_to_board(rook_white) pawn_white = Pawn(white, [2, 4]) # c4 board.add_to_board(pawn_white) incorrect_move1 = [2, 4] # c4 returned_moves = rook_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 11) self.assertTrue(incorrect_move1 not in returned_moves)
def test_make_move_castle_kingside(self): """Ensures kingside castling moving works correctly.""" board, white = Board(), Player(Color.W) king_pos_white = [4, 7] # e1 rook_pos_white = [7, 7] # h1 king_white = King(white, king_pos_white) rook_white = Rook(white, rook_pos_white) board.add_to_board(king_white) board.add_to_board(rook_white) board.make_move(king_white, [6, 7]) # castle to g1 self.assertTrue(board.get_piece_at_position([6, 7]) is king_white) self.assertTrue(board.get_piece_at_position([5, 7]) is rook_white) self.assertTrue(board.get_piece_at_position([4, 7]) is None) self.assertTrue(board.get_piece_at_position([7, 7]) is None)
def test_castling_already_moved(self): """Ensures that castling is not returned in the King's moved list if the King has already moved.""" board, white = Board(), Player(Color.W) king_white = King(white, [4, 7]) # e1 king_white.first_move = False board.add_to_board(king_white) rook_white = Rook(white, [7, 7]) # h1 board.add_to_board(rook_white) castle_move = [6, 7] # g1 returned_moves = king_white.get_legal_moves(board, True) self.assertFalse(castle_move in returned_moves)
def test_path_impeded(self): """Verifies that a bishop's returned moves stops when encountering another of the bishop's owner's pieces.""" board, white = Board(), Player(Color.W) king_white = King(white, [3, 4]) # d4 board.add_to_board(king_white) pawn_white = Pawn(white, [2, 4]) # c4 board.add_to_board(pawn_white) incorrect_move1 = [2, 4] # c4 returned_moves = king_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 7) self.assertFalse(incorrect_move1 in returned_moves)
def test_pawn_black_promotion(self): """Tests that the four promotion moves are available to a promoting pawn.""" board, black = Board(), Player(Color.B) pawn_black = Pawn(black, [3, 6]) # d2 board.add_to_board(pawn_black) correct_move1 = [3, "Q"] # d1 correct_move2 = [3, "N"] # d1 correct_move3 = [3, "B"] # d1 correct_move4 = [3, "R"] # d1 returned_moves = pawn_black.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 4) self.assertTrue(correct_move1 in returned_moves) self.assertTrue(correct_move2 in returned_moves) self.assertTrue(correct_move3 in returned_moves) self.assertTrue(correct_move4 in returned_moves)
def test_pawn_white_promotion(self): """Tests that the four promotion moves are available to a promoting pawn.""" board, white = Board(), Player(Color.W) pawn_white = Pawn(white, [3, 1]) # d7 board.add_to_board(pawn_white) correct_move1 = [3, "Q"] # d8 correct_move2 = [3, "N"] # d8 correct_move3 = [3, "B"] # d8 correct_move4 = [3, "R"] # d8 returned_moves = pawn_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 4) self.assertTrue(correct_move1 in returned_moves) self.assertTrue(correct_move2 in returned_moves) self.assertTrue(correct_move3 in returned_moves) self.assertTrue(correct_move4 in returned_moves)
def test_simple(self): """Verifies that a rook's returned move list has the correct number of moves and that the boundaries of it's movelist is correct.""" board, white = Board(), Player(Color.W) rook_white = Rook(white, [3, 4]) # d4 board.add_to_board(rook_white) correct_move1 = [0, 4] # a4 correct_move2 = [7, 4] # h4 correct_move3 = [3, 0] # d8 correct_move4 = [3, 7] # d1 returned_moves = rook_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 14) self.assertTrue(correct_move1 in returned_moves) self.assertTrue(correct_move2 in returned_moves) self.assertTrue(correct_move3 in returned_moves) self.assertTrue(correct_move4 in returned_moves)
def test_simple(self): """Verifies that a bishops's returned movelist has the correct number of moves and that the boundaries of it's movelist is correct.""" board, white = Board(), Player(Color.W) bishop_white = Bishop(white, [3, 4]) # d4 board.add_to_board(bishop_white) correct_move1 = [7, 0] # h8 correct_move2 = [6, 7] # g1 correct_move3 = [0, 1] # a7 correct_move4 = [0, 7] # a1 returned_moves = bishop_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 13) self.assertTrue(correct_move1 in returned_moves) self.assertTrue(correct_move2 in returned_moves) self.assertTrue(correct_move3 in returned_moves) self.assertTrue(correct_move4 in returned_moves)
def test_get_all_legal_moves_simple(self): """Simply verify that getting the legal moves of two pieces sum to the returned value of get_all_legal_moves.""" board, white = Board(), Player(Color.W) pos_white = [4, 6] # e2 pawn_white = Pawn(white, pos_white) board.add_to_board(pawn_white) pos_white2 = [3, 3] # d5 rook_white = Rook(white, pos_white2) board.add_to_board(rook_white) combined_positions = [] for move in pawn_white.get_legal_moves(board, True): combined_positions += [pawn_white, move], for move in rook_white.get_legal_moves(board, True): combined_positions += [rook_white, move], self.assertEqual(board.get_all_legal_moves(white), combined_positions)
def test_simple(self): """Verifies that a king's returned movelist has the correct number of moves and that the boundaries of it's movelist is correct.""" board, white = Board(), Player(Color.W) king_white = King(white, [3, 4]) # d4 board.add_to_board(king_white) correct_move_list = [[3, 5]] # d3 correct_move_list += [2, 5], # c3 correct_move_list += [4, 5], # e3 correct_move_list += [4, 4], # e4 correct_move_list += [2, 4], # c4 correct_move_list += [4, 3], # e5 correct_move_list += [3, 3], # d5 correct_move_list += [2, 3], # c5 returned_moves = king_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 8) for move in correct_move_list: self.assertTrue(move in returned_moves)
def test_simple(self): """Verifies that a queen's returned movelist has the correct number of moves and that the boundaries of it's movelist is correct.""" board, white = Board(), Player(Color.W) queen_white = Queen(white, [3, 4]) # d4 board.add_to_board(queen_white) correct_move_list = [[7, 0]] # h8 correct_move_list += [6, 7], # g1 correct_move_list += [0, 1], # a7 correct_move_list += [0, 7], # a1 correct_move_list += [0, 4], # a4 correct_move_list += [7, 4], # h4 correct_move_list += [3, 0], # d8 correct_move_list += [3, 7], # d1 returned_moves = queen_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 27) for move in correct_move_list: self.assertTrue(move in returned_moves)
def test_castling_to_check(self): """Ensures that castling is not returned in the King's moved list if the King would be castling into check.""" board, white, black = Board(), Player(Color.W), Player(Color.B) king_white = King(white, [4, 7]) # e1 board.add_to_board(king_white) rook_white = Rook(white, [7, 7]) # h1 board.add_to_board(rook_white) castle_move = [6, 7] # g1 returned_moves = king_white.get_legal_moves(board, True) self.assertTrue(castle_move in returned_moves) rook_black = Rook(black, [6, 0]) # g7 board.add_to_board(rook_black) returned_moves = king_white.get_legal_moves(board, True) self.assertFalse(castle_move in returned_moves)
def test_path_impeded(self): """Verifies that a bishop's returned moves stops when encountering another of the bishop's owner's pieces.""" board, white = Board(), Player(Color.W) queen_white = Queen(white, [3, 4]) # d4 board.add_to_board(queen_white) pawn_white1 = Pawn(white, [2, 3]) # c5 board.add_to_board(pawn_white1) incorrect_move1 = [2, 3] # c6 pawn_white2 = Pawn(white, [2, 4]) # c4 board.add_to_board(pawn_white2) incorrect_move2 = [2, 4] # c4 returned_moves = queen_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 21) self.assertTrue(incorrect_move1 not in returned_moves) self.assertTrue(incorrect_move2 not in returned_moves)