示例#1
0
    def test_left_diagonal(self):
        self.board.update(converter.long_alg("b2b3", self.board))
        moves = list(
            self.board.piece_at_square(
                Location.from_string("c1")).possible_moves(self.board))

        self.assertEqual(len(moves), 2)
        self.assertEqual(moves[0], converter.long_alg("c1b2", self.board))
        self.assertEqual(moves[1], converter.long_alg("c1a3", self.board))
示例#2
0
    def test_update_pawn_moves_two_steps(self):
        pawn = self.board.piece_at_square(Location.from_string("e2"))
        self.board.update(converter.long_alg("e2e4", self.board))

        self.assertIsInstance(pawn, Pawn)
        self.assertEqual(self.board.piece_at_square(Location.from_string("e4")), pawn)
        self.assertIsNone(self.board.piece_at_square(Location.from_string("e2")))
        self.assertIsNone(self.board.piece_at_square(Location.from_string("e3")))
        self.assertTrue(pawn.just_moved_two_steps)

        self.board.update(converter.long_alg("d2d4", self.board))

        self.assertFalse(pawn.just_moved_two_steps)
示例#3
0
    def test_advantage_as_result(self):
        self.assertEqual(self.board.advantage_as_result(converter.long_alg("e2e4", self.board),
                                                        piece_const.PieceValues()), 0)

        self.board.position[1][3] = None
        self.assertEqual(
            self.board.advantage_as_result(converter.long_alg("d1d7", self.board), piece_const.PieceValues()), 0)

        self.board.update(converter.short_alg("e3", color.white, self.board))

        self.assertEqual(
            self.board.advantage_as_result(
                converter.short_alg("Bd2", color.white, self.board), piece_const.PieceValues()), -1)
示例#4
0
    def test_update_pawn_moves_two_steps(self):
        pawn = self.board.piece_at_square(Location.from_string("e2"))
        self.board.update(converter.long_alg("e2e4", self.board))

        self.assertIsInstance(pawn, Pawn)
        self.assertEqual(
            self.board.piece_at_square(Location.from_string("e4")), pawn)
        self.assertIsNone(
            self.board.piece_at_square(Location.from_string("e2")))
        self.assertIsNone(
            self.board.piece_at_square(Location.from_string("e3")))
        self.assertTrue(pawn.just_moved_two_steps)

        self.board.update(converter.long_alg("d2d4", self.board))

        self.assertFalse(pawn.just_moved_two_steps)
示例#5
0
    def test_all_possible_moves_2(self):
        self.board.update(converter.long_alg("e2e4", self.board))

        moves = {"a7a6", "a7a5", "b7b6", "b7b5", "c7c6", "c7c5", "d7d6", "d7d5",
                 "e7e6", "e7e5", "f7f6", "f7f5", "g7g6", "g7g5", "h7h6", "h7h5",
                 "b8a6", "b8c6", "g8f6", "g8h6"}

        self.assertEqual(moves, {str(move) for move in self.board.all_possible_moves(color.black)})
示例#6
0
    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))
示例#7
0
    def test_capture(self):
        self.board.move_piece(Location.from_string("g1"),
                              Location.from_string("g7"))
        moves = list(
            self.board.piece_at_square(
                Location.from_string("f8")).possible_moves(self.board))

        self.assertEqual(len(moves), 1)
        self.assertEqual(moves[0], converter.long_alg("f8g7", self.board))
示例#8
0
    def test_advantage_as_result(self):
        self.assertEqual(
            self.board.advantage_as_result(
                converter.long_alg("e2e4", self.board),
                piece_const.PieceValues()), 0)

        self.board.position[1][3] = None
        self.assertEqual(
            self.board.advantage_as_result(
                converter.long_alg("d1d7", self.board),
                piece_const.PieceValues()), 0)

        self.board.update(converter.short_alg("e3", color.white, self.board))

        self.assertEqual(
            self.board.advantage_as_result(
                converter.short_alg("Bd2", color.white, self.board),
                piece_const.PieceValues()), -1)
示例#9
0
    def test_all_possible_moves_2(self):
        self.board.update(converter.long_alg("e2e4", self.board))

        moves = {
            "a7a6", "a7a5", "b7b6", "b7b5", "c7c6", "c7c5", "d7d6", "d7d5",
            "e7e6", "e7e5", "f7f6", "f7f5", "g7g6", "g7g5", "h7h6", "h7h5",
            "b8a6", "b8c6", "g8f6", "g8h6"
        }

        self.assertEqual(
            moves,
            {str(move)
             for move in self.board.all_possible_moves(color.black)})
示例#10
0
    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)
示例#11
0
    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))
示例#12
0
    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"))