class TestKnight(TestCase): def setUp(self): self.empty_pos = Board([[None for _ in range(8)] for _ in range(8)]) def test_possible_moves(self): self.empty_pos.place_piece_at_square(Knight(color.white, Location.from_string("e4")), Location.from_string("e4")) knight = self.empty_pos.piece_at_square(Location.from_string("e4")) moves = knight.possible_moves(self.empty_pos) self.assertEqual(len(list(moves)), 8)
def test_possible_moves(self): self.board = Board([[None for _ in range(8)] for _ in range(8)]) my_king = King(color.white, Location.from_string("f3")) self.board.place_piece_at_square(my_king, Location.from_string("f3")) moves = ['f3f4', 'f3g3', 'f3f2', 'f3e3', 'f3g4', 'f3e4', 'f3g2', 'f3e2'] for i, move in enumerate(my_king.possible_moves(self.board)): self.assertEqual(move, converter.long_alg(moves[i], self.board))
def test_material_advantage_white_advantage(self): self.board = Board.init_default() self.board.position[7][0] = None self.assertEqual(self.board.material_advantage(color.white, piece_const.PieceValues()), 5) self.assertEqual(self.board.material_advantage(color.black, piece_const.PieceValues()), -5) self.board = Board.init_default() self.board.position[7][3] = None self.assertEqual(self.board.material_advantage(color.white, piece_const.PieceValues()), 9) self.assertEqual(self.board.material_advantage(color.black, piece_const.PieceValues()), -9) self.board = Board.init_default() self.board.position[7][2] = None self.assertEqual(self.board.material_advantage(color.white, piece_const.PieceValues()), 3.5) self.assertEqual(self.board.material_advantage(color.black, piece_const.PieceValues()), -3.5)
def test_move_piece(self): test = Board.init_default() pawn = test.position[1][4] test.position[1][4] = None test.position[3][4] = pawn self.board.move_piece(Location.from_string("e2"), Location.from_string("e4")) self.assertEqual(self.board, test)
def test_place_piece_at_square(self): test = Board.init_default() pawn = Pawn(color.white, Location.from_string("e3")) test.position[2][4] = pawn self.board.place_piece_at_square(pawn, Location.from_string("e3")) self.assertEqual(self.board, test)
def test_in_check(self): self.board = Board([[None for _ in range(8)] for _ in range(8)]) my_king = King(color.white, Location.from_string("f3")) self.board.place_piece_at_square(my_king, Location.from_string("f3")) self.board.place_piece_at_square(Rook(color.black, Location.from_string("f1")), Location.from_string("f1")) print(self.board.piece_at_square(Location.from_string("f1")).color) print(self.board) print(my_king.color) print(color.white == color.black) self.assertTrue(my_king.in_check(self.board)) self.board = Board.init_default() self.board.update(converter.long_alg("f2f3", self.board)) self.board.move_piece(Location.from_string("d8"), Location.from_string("g3")) self.assertTrue(self.board.get_king(color.white).in_check(self.board))
def test_init_default(self): white = color.white black = color.black test = Board([ # First rank [ Rook(white, Location(0, 0)), Knight(white, Location(0, 1)), Bishop(white, Location(0, 2)), Queen(white, Location(0, 3)), King(white, Location(0, 4)), Bishop(white, Location(0, 5)), Knight(white, Location(0, 6)), Rook(white, Location(0, 7)) ], # Second rank [Pawn(white, Location(1, file)) for file in range(8)], # Third rank [None for _ in range(8)], # Fourth rank [None for _ in range(8)], # Fifth rank [None for _ in range(8)], # Sixth rank [None for _ in range(8)], # Seventh rank [Pawn(black, Location(6, file)) for file in range(8)], # Eighth rank [ Rook(black, Location(7, 0)), Knight(black, Location(7, 1)), Bishop(black, Location(7, 2)), Queen(black, Location(7, 3)), King(black, Location(7, 4)), Bishop(black, Location(7, 5)), Knight(black, Location(7, 6)), Rook(black, Location(7, 7)) ] ]) self.assertEqual(self.board, test)
def test_material_advantage_black_advantage(self): self.board.position[0][0] = None self.assertEqual( self.board.material_advantage(color.white, piece_const.PieceValues()), -5) self.assertEqual( self.board.material_advantage(color.black, piece_const.PieceValues()), 5) self.board = Board.init_default() self.board.position[0][1] = None self.assertEqual( self.board.material_advantage(color.white, piece_const.PieceValues()), -3) self.assertEqual( self.board.material_advantage(color.black, piece_const.PieceValues()), 3)
def setUp(self): self.board = Board.init_default()
def test_copy(self): tester = Board.init_default() for num, row in enumerate(self.board.position): for index, piece in enumerate(row): self.assertEqual(piece, tester.position[num][index])
def test_remove_piece_at_square(self): test_board = Board.init_default() test_board.position[0][0] = None self.board.remove_piece_at_square(Location(0, 0)) self.assertEqual(self.board, test_board)
def setUp(self): self.empty_pos = Board([[None for _ in range(8)] for _ in range(8)])
class TestKing(TestCase): def setUp(self): self.board = Board.init_default() def test_in_check_as_result(self): self.assertFalse(self.board.get_king(color.white).in_check_as_result(self.board, converter.long_alg("e2e4", self.board))) self.board.move_piece(Location.from_string("e1"), Location.from_string("e3")) self.board.move_piece(Location.from_string("e8"), Location.from_string("e5")) # self.assertTrue(self.board.get_king(color.white).in_check_as_result(self.board, converter.long_alg("e3e4", self.board))) def test_add(self): self.assertEqual( len(list(self.board.get_king(color.white).add(lambda x: x.shift_up(), self.board))), 0) self.board.update(converter.long_alg("e2e4", self.board)) # King should be able to move up self.assertEqual( len(list(self.board.get_king(color.white).add(lambda x: x.shift_up(), self.board))), 1) # King should not be able to move down self.assertEqual( len(list(self.board.get_king(color.white).add(lambda x: x.shift_down(), self.board))), 0) # King should not be able to move left self.assertEqual( len(list(self.board.get_king(color.white).add(lambda x: x.shift_left(), self.board))), 0) # King should not be able to move right self.assertEqual( len(list(self.board.get_king(color.white).add(lambda x: x.shift_right(), self.board))), 0) # King should not be able to move up left self.assertEqual( len(list(self.board.get_king(color.white).add(lambda x: x.shift_up_left(), self.board))), 0) # King should not be able to move down right self.assertEqual( len(list(self.board.get_king(color.white).add(lambda x: x.shift_down_right(), self.board))), 0) # King should not be able to move down left self.assertEqual( len(list(self.board.get_king(color.white).add(lambda x: x.shift_down_left(), self.board))), 0) # King should not be able to move up right self.assertEqual( len(list(self.board.get_king(color.white).add(lambda x: x.shift_up_right(), self.board))), 0) def test_kingside_castle(self): self.board.update(converter.short_alg("e4", color.white, self.board)) self.board.update(converter.short_alg("Nf3", color.white, self.board)) self.board.update(converter.short_alg("Be2", color.white, self.board)) castle_move = Move( end_loc=Location.from_string("g1"), piece=King(color.white, Location.from_string("g1")), status=notation_const.KING_SIDE_CASTLE, start_loc=Location.from_string("e1") ) self.assertEqual( list(self.board.get_king(color.white).add_castle(self.board))[0], castle_move) def test_queenside_castle(self): self.board.remove_piece_at_square(Location.from_string("b1")) self.board.remove_piece_at_square(Location.from_string("c1")) self.board.remove_piece_at_square(Location.from_string("d1")) castle_move = Move( end_loc=Location.from_string("c1"), piece=King(color.white, Location.from_string("c1")), status=notation_const.QUEEN_SIDE_CASTLE, start_loc=Location.from_string("e1") ) self.assertEqual( list(self.board.get_king(color.white).add_castle(self.board))[0], castle_move) def test_possible_moves(self): self.board = Board([[None for _ in range(8)] for _ in range(8)]) my_king = King(color.white, Location.from_string("f3")) self.board.place_piece_at_square(my_king, Location.from_string("f3")) moves = ['f3f4', 'f3g3', 'f3f2', 'f3e3', 'f3g4', 'f3e4', 'f3g2', 'f3e2'] for i, move in enumerate(my_king.possible_moves(self.board)): self.assertEqual(move, converter.long_alg(moves[i], self.board)) def test_in_check(self): self.board = Board([[None for _ in range(8)] for _ in range(8)]) my_king = King(color.white, Location.from_string("f3")) self.board.place_piece_at_square(my_king, Location.from_string("f3")) self.board.place_piece_at_square(Rook(color.black, Location.from_string("f1")), Location.from_string("f1")) print(self.board.piece_at_square(Location.from_string("f1")).color) print(self.board) print(my_king.color) print(color.white == color.black) self.assertTrue(my_king.in_check(self.board)) self.board = Board.init_default() self.board.update(converter.long_alg("f2f3", self.board)) self.board.move_piece(Location.from_string("d8"), Location.from_string("g3")) self.assertTrue(self.board.get_king(color.white).in_check(self.board))
def setUp(self): self.test_board = Board.init_default() self.e_four_move = Move(end_loc=Location.from_string("e4"), piece=Pawn(color.white, Location.from_string("e4")), status=notation_const.MOVEMENT, start_loc=Location.from_string("e2"))