Example #1
0
    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)
Example #2
0
def cb_back_prompt(args=None):
    if args != None and "board" in args and "window" in args:
        board = args["board"]
        window = args["window"]

        # 暂停走棋
        if board.is_stop():
            return

        # 回合数, 总步数 = 回合数 * 2
        depth = 5

        # 创建minimax树
        mtree = ai_value.chessman_create_minimax_tree(board, depth,
                Board.chessboard_evaluate,
                Board.get_all_possible_steps,
                Board.chessboard_ai_move,
                is_max=(board.curStepColor == Base.COLOR_RED))

        # 取得所有最佳招法列表
        nodelist = ai_value.chessman_get_minimax_moves(mtree,
                Board.chessboard_evaluate,
                is_max=(board.curStepColor == Base.COLOR_RED))

        # 随机选择一种走法
        node_last = random.choice(nodelist)
        move_path = node_last.get_elder()
        node_first = move_path[1]
        chessboard = node_first.get_data()

        # 走棋
        pos_change = Board.chessboard_get_step_by_board(board.board, chessboard.board)
        Board.chessboard_ai_move(window, board, pos_change[0], pos_change[1])
Example #3
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))
Example #4
0
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)
Example #5
0
 def __init__(self):
     self.board = Board.Board()
     #self.player1 = Player.Player('black')
     #self.player2 = Player.Player('white')
     self.turn = 'black'
     self.player_moving = False
     self.status = True
     self.winner = None
Example #6
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))
Example #7
0
    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)
Example #8
0
    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)
Example #9
0
    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)
Example #10
0
def main():
    print("Пример 1", '\n')
    board = Board()
    board.field = [([None] * 8) for i in range(8)]
    board.field[0][3] = Queen(WHITE)
    queen = board.get_piece(0, 3)
    for row in range(7, -1, -1):
        for col in range(8):
            if queen.can_move(board, 0, 3, row, col):
                print('x', end=' ')
            else:
                cell = board.cell(row, col)[1]
                cell = cell if cell != ' ' else '-'
                print(cell, end=' ')
        print()
    print("Пример 2", '\n')
    row0 = 4
    col0 = 5

    board = Board()
    board.field = [([None] * 8) for i in range(8)]
    board.field[row0][col0] = Bishop(BLACK)
    bishop = board.get_piece(row0, col0)

    for row in range(7, -1, -1):
        for col in range(8):
            if bishop.can_move(board, row0, col0, row, col):
                print('x', end=' ')
            else:
                cell = board.cell(row, col)[1]
                cell = cell if cell != ' ' else '-'
                print(cell, end=' ')
        print()
    print("Пример 3", '\n')
    row0 = 2
    col0 = 2
    knight = Knight(WHITE)
    board = Board()
    for row in range(7, -1, -1):
        for col in range(8):
            if row == row0 and col == col0:
                print(knight.char(), end=' ')
            elif knight.can_move(board, row0, col0, row, col):
                print('x', end=' ')
            else:
                print('-', end=' ')
        print()
Example #11
0
def main():
    # 初始化
    pygame.init()
    # 设置窗口大小 图片大小是460*532
    #  window = pygame.display.set_mode((460, 532 + 28 + 50))
    #  window = pygame.display.set_mode((Board.WINDOW_WIDTH, Board.WINDOW_HEIGHT))
    # 设置窗口标题
    pygame.display.set_caption('Chinese Chess')

    pygame.event.set_blocked([MOUSEMOTION])
    pygame.event.set_allowed([QUIT, MOUSEBUTTONDOWN, MOUSEBUTTONUP])

    # 窗口
    chesswindow = Board.ChessWindow()

    # 象棋棋盘类
    chessbord = Board.ChessBoard()

    # 操作面板按钮初始化
    args = {"window": chesswindow, "board": chessbord}
    chesswindow.operatePanel.button_init(args)

    chessbord.redrawBorad(chesswindow)

    mainloop = True

    # 事件循环
    while mainloop:
        # 更新显示
        pygame.display.update()
        moveResult = 0

        # 等待并从队列中获取一个事件
        event = pygame.event.wait()

        if event.type == pygame.QUIT:  # 如果关闭窗口,退出
            print("press pygame.QUIT")
            mainloop = False
            break
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:  # 如果按下Esc键,退出
                print("press pygame.K_ESCAPE")
                mainloop = False
                break
        elif event.type == pygame.MOUSEBUTTONDOWN or event.type == pygame.MOUSEBUTTONUP:
            (xPos, yPos) = pygame.mouse.get_pos()
            mouse = pygame.mouse.get_pressed()
            if not mouse[0]:
                row = (yPos - Board.BOARD_TOP) // Board.BOARD_GAP
                col = (xPos - Board.BOARD_LEFT) // Board.BOARD_GAP
                if (row >= 0 and row <= 9) and (col >= 0
                                                and col <= 8):  # 鼠标点击在棋盘内
                    moveResult = chessbord.moveChess(chesswindow, row, col)
            chesswindow.operatePanel.button_process((xPos, yPos), mouse)
        else:
            print("press othre key: %s" % event.type)

        chessbord.redrawBorad(chesswindow)
        chessbord.showTipInfo(chesswindow)

    pygame.quit()
    sys.exit()
Example #12
0
 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"))
Example #13
0
 def setUp(self):
     self.board = Board.init_default()
Example #14
0
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))
Example #15
0
 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)
Example #16
0
    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])
Example #17
0
 def setUp(self):
     self.empty_pos = Board([[None for _ in range(8)] for _ in range(8)])