def init():
    initial_board_state = GameState(state=Board(), next_to_move=1)
    root = MonteCarloTreeSearchNode(state=initial_board_state, parent=None)
    mcts = MonteCarloTreeSearch(root)
    best_node = mcts.best_action(100)
    c_state = best_node.state
    c_board = c_state.board
    return c_state, c_board
Esempio n. 2
0
def init():
    state = np.zeros((3, 3))
    initial_board_state = TicTacToeGameState(state=state, next_to_move=1)
    root = MonteCarloTreeSearchNode(state=initial_board_state, parent=None)
    mcts = MonteCarloTreeSearch(root)
    best_node = mcts.best_action(1000)
    c_state = best_node.state
    c_board = c_state.board
    return c_state,c_board
Esempio n. 3
0
    def play(self, env: ConnectFourEnvironment):
        self._mcts = None
        assert (not env.terminated)
        root = MonteCarloTreeSearchNode(state=env, parent=None)
        mcts = MonteCarloTreeSearch(root, self.rollout_player)

        best_node = mcts.best_child(self.number_of_simulations)

        self._mcts = mcts
        env = best_node.state
        return env, env.get_last_action()
Esempio n. 4
0
 def computer(self):
     node = MonteCarloTreeSearchNode(self.state)
     tree = MonteCarloTreeSearch(node)
     best_node = tree.best_action(1000)
     temp = best_node.state.board
     for i in range(3):
         for j in range(3):
             if temp[i, j] != self.state.board[i, j]:
                 self.draw_chess(i, j, self.state.next_to_move)
                 break
     self.state = best_node.state
Esempio n. 5
0
    def action_values(self, env: ConnectFourEnvironment):
        assert (not env.terminated)
        root = MonteCarloTreeSearchNode(parent=None,
                                        action_value=0.0,
                                        state=env)
        mcts = MonteCarloTreeSearch(root, self.rollout_player)
        self._mcts = mcts

        return mcts.root_nodes.choices_q_norm()
Esempio n. 6
0
import logging

from hivemind.state import State

from mcts.node import MonteCarloTreeSearchNode
from mcts.search import MonteCarloTreeSearch

root = State()
node = MonteCarloTreeSearchNode(root)
search = MonteCarloTreeSearch(node)
r = search.best_action(100)
print(r.state)
def run_game(mode, number_of_simulations):
    global all_sprites_list, sprites
    global board

    board = BoardGUI()
    all_sprites_list = pygame.sprite.Group()
    sprites = [piece for row in board.array for piece in row if piece]
    all_sprites_list.add(sprites)

    gameover = False  # flaga końca gry
    winner = ""  # identyfikator zwycięzcy
    selected = False
    trans_table = dict()
    checkWhite = False
    player = 1

    previous_move = ''
    current_move = ''
    run_flag = 0

    #=====================================
    # Threading things
    is_simulation_running = False
    t1, t2 = None, None
    q1, q2 = queue.Queue(), queue.Queue()
    # =====================================

    screen.blit(bg, (0, 0))
    all_sprites_list.draw(screen)
    pygame.display.update()
    clock.tick(60)

    while not gameover:
        previous_move = current_move
        if player == 1:
            if mode == 0 or mode == 1:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()

                    elif event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_ESCAPE:
                            gameover = True

                    # wybór pionka do wykonania ruchu
                    elif event.type == pygame.MOUSEBUTTONDOWN and not selected:
                        board.unhighlight_optional_moves()
                        piece = select_piece("w")

                        # generuje "legalne" ruchy pionka
                        if piece != Empty and piece.color == "w":
                            # sprawdzenie dostępnych ruchów
                            player_moves = piece.gen_legal_moves(board)
                            # all_player_moves = board.get_all_legal_moves("w")

                            # podświetlenie dostępnych ruchów
                            board.highlight_optional_moves(player_moves)
                            selected = True

                    # pionek wybrany -> wybór ruchu
                    elif event.type == pygame.MOUSEBUTTONDOWN and selected:
                        board.unhighlight_optional_moves()
                        square = select_square()

                        # sprawdza czy wybrane pole jest w zasięgu dozwolonych ruchów
                        if square in player_moves:
                            oldx = piece.x
                            oldy = piece.y
                            dest = board.array[square[0]][square[1]]

                            # wykonanie ruchu
                            board.move_piece(piece, square[0], square[1])

                            if dest:  # aktualizacja 'duszków' względem stanu planszy
                                sprites = reload_sprites()
                                all_sprites_list.empty()
                                all_sprites_list.add(sprites)

                            selected = False
                            player = 2

                        # anuluje ruch, jezeli wybrane zostalo to samo pole
                        elif (piece.y, piece.x) == square:
                            piece.unhighlight()
                            selected = False

                        # ruch jest nieważny
                        else:
                            pygame.display.update()
                            # board.highlight_optional_moves(player_moves)
                            pygame.time.wait(1000)
            elif mode == 2:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()

                    elif event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_ESCAPE:
                            gameover = True
                board_mcts = Board(board=simplify_board(board.array))
                board_state = GameState(state=board_mcts, next_to_move=1)
                root = MonteCarloTreeSearchNode(state=board_state, parent=None)
                mcts = MonteCarloTreeSearch(root)

                if not is_simulation_running:
                    t1 = threading.Thread(target=mcts.best_action,
                                          args=(number_of_simulations, q1))
                    t1.daemon = True
                    t1.start()
                    is_simulation_running = True
                    # best_node = mcts.best_action(number_of_simulations)
                else:
                    if not q1.empty():
                        best_node = q1.get()
                        # t1.join()
                        q1.queue.clear()
                        is_simulation_running = False

                        c_state = best_node.state
                        c_board = c_state.board

                        x_from = c_state.current_move.pos_from.getX()
                        y_from = c_state.current_move.pos_from.getY()
                        # print("x_from", x_from)
                        # print("y_from", y_from)

                        x_to = c_state.current_move.pos_to.getX()
                        y_to = c_state.current_move.pos_to.getY()
                        # print("x_to", x_to)
                        # print("y_to", y_to)

                        piece = select_piece_xy("w", x_from, y_from)
                        square = (x_to, y_to)
                        dest = board.array[y_to][x_to]

                        board.move_piece(piece, y_to, x_to)  # wykonanie ruchu
                        if dest:
                            sprites = reload_sprites()
                            all_sprites_list.empty()
                            all_sprites_list.add(reload_sprites())
                        player = 2

        # drugi gracz
        elif player == 2:
            if mode == 0:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()

                    elif event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_ESCAPE:
                            gameover = True

                    elif event.type == pygame.MOUSEBUTTONDOWN and not selected:
                        board.unhighlight_optional_moves()
                        piece = select_piece("b")

                        if piece != Empty and piece.color == "b":
                            # sprawdzenie dostępnych ruchów
                            player_moves = piece.gen_legal_moves(board)
                            # print(player_moves)
                            # podświetlenie dostępnych ruchów
                            board.highlight_optional_moves(player_moves)
                            selected = True

                    elif event.type == pygame.MOUSEBUTTONDOWN and selected:
                        board.unhighlight_optional_moves()
                        square = select_square()

                        if square in player_moves:
                            oldx = piece.x
                            oldy = piece.y
                            dest = board.array[square[0]][square[1]]

                            # wykonanie ruchu
                            board.move_piece(piece, square[0], square[1])
                            if dest:
                                sprites = reload_sprites()
                                all_sprites_list.empty()
                                all_sprites_list.add(reload_sprites())

                            selected = False
                            player = 1

                        elif (piece.y, piece.x) == square:
                            piece.unhighlight()
                            selected = False
                        else:
                            pygame.display.update()
                            board.highlight_optional_moves(player_moves)
                            pygame.time.wait(1000)

            elif mode == 1 or mode == 2:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()

                    elif event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_ESCAPE:
                            gameover = True
                board_mcts = Board(board=simplify_board(board.array))
                # print(board_mcts.boardValues)
                board_state = GameState(state=board_mcts, next_to_move=-1)
                root = MonteCarloTreeSearchNode(state=board_state, parent=None)
                mcts = MonteCarloTreeSearch(root)
                if not is_simulation_running:
                    t2 = threading.Thread(target=mcts.best_action,
                                          args=(number_of_simulations, q2))
                    t2.daemon = True
                    t2.start()
                    is_simulation_running = True
                    # best_node = mcts.best_action(number_of_simulations)
                else:
                    if not q2.empty():
                        best_node = q2.get()
                        # t2.join()
                        q2.queue.clear()
                        is_simulation_running = False

                        c_state = best_node.state
                        c_board = c_state.board

                        x_from = c_state.current_move.pos_from.getX()
                        y_from = c_state.current_move.pos_from.getY()
                        # print("x_from", x_from)
                        # print("y_from", y_from)

                        x_to = c_state.current_move.pos_to.getX()
                        y_to = c_state.current_move.pos_to.getY()
                        # print("x_to", x_to)
                        # print("y_to", y_to)

                        piece = select_piece_xy("b", x_from, y_from)
                        square = (x_to, y_to)
                        dest = board.array[y_to][x_to]

                        board.move_piece(piece, y_to, x_to)  # wykonanie ruchu
                        if dest:
                            sprites = reload_sprites()
                            all_sprites_list.empty()
                            all_sprites_list.add(reload_sprites())
                        player = 1

        screen.blit(bg, (0, 0))
        all_sprites_list.draw(screen)
        pygame.display.update()
        clock.tick(60)

        arr = []
        for j in range(9):
            for piecee in board.array[j]:
                arr.append(piecee.color + piecee.symbol)

        # check end game
        if 'wN' not in arr:
            gameover = True
            winner = "Black"
        elif 'bN' not in arr:
            gameover = True
            winner = "White"

        current_move = arr[40]
        if previous_move == 'wN' and current_move == "_N":
            gameover = True
            winner = "White"
        elif previous_move == 'bN' and current_move == "_N":
            gameover = True
            winner = "Black"

        output_board = simplify_board(board.array)

    print("Wygrał: ", winner)
    if winner == "White":
        screen.blit(winner_white, (0, 0))
        pygame.display.update()
        time.sleep(5)
    elif winner == "Black":
        screen.blit(winner_black, (0, 0))
        pygame.display.update()
        time.sleep(5)
        print("invalid move")
        move = get_action(state)
    return move


c_state, c_board = init()
c_board.printBoard()
# graphics(c_board)
next_move = -1
while True:
    # move1 = get_action(c_state)
    # c_state = c_state.move(move1)
    # c_board = c_state.board
    # c_board.printBoard()
    print('next = ', next_move)
    print(c_board.boardValues)
    board_state = GameState(state=c_board, next_to_move=next_move)
    root = MonteCarloTreeSearchNode(state=board_state, parent=None)
    mcts = MonteCarloTreeSearch(root)
    best_node = mcts.best_action(100)
    c_state = best_node.state
    c_board = c_state.board
    c_board.printBoard()
    # graphics(c_board)
    next_move = -1 * next_move

    if c_state.is_game_over():
        break

print("Koniec")
Esempio n. 9
0
    def setup_board(self):
        select = input('是否先手? ')
        Game.first = True if select == 'y' else False
        if Game.first:
            Game.board = np.array([
                [6, 2, 4, 0, 0],
                [1, 5, 0, 0, 0],
                [3, 0, 0, 0, 0],
                [0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0]
            ])
            print('我方摆放: ')
            print(Game.board)
            chess = input('对方摆放: ')
            Game.board[2][4] = -int(chess[0])
            Game.board[3][3] = -int(chess[1])
            Game.board[3][4] = -int(chess[2])
            Game.board[4][2] = -int(chess[3])
            Game.board[4][3] = -int(chess[4])
            Game.board[4][4] = -int(chess[5])
            print(Game.board)
            Game.txt_strs.append(
                'R:A5-{};B5-{};C5-{};A4-{};B4-{};A3-{}'.format(
                    Game.board[0][0], Game.board[0][1], Game.board[0][2],
                    Game.board[1][0], Game.board[1][1], Game.board[2][0]))
            Game.txt_strs.append('B:E3{};D2{};E2{};C1{};D1{};E1{}'.format(
                Game.board[2][4], Game.board[3][3], Game.board[3][4],
                Game.board[4][2], Game.board[4][3], Game.board[4][4]))
            key = int(input('我方色子: '))
            state1 = State(Game.board.copy(), 1 if Game.first else -1, key)
            state2 = State(Game.board.copy(), 1 if Game.first else -1, key)
            state3 = State(Game.board.copy(), 1 if Game.first else -1, key)
            state4 = State(Game.board.copy(), 1 if Game.first else -1, key)
            node1 = MonteCarloTreeSearchNode(state1)
            node2 = MonteCarloTreeSearchNode(state2)
            node3 = MonteCarloTreeSearchNode(state3)
            node4 = MonteCarloTreeSearchNode(state4)
            mcts1 = MonteCarloTreeSearch(node1)
            mcts2 = MonteCarloTreeSearch(node2)
            mcts3 = MonteCarloTreeSearch(node3)
            mcts4 = MonteCarloTreeSearch(node4)

            pool = Pool(4)
            ress = pool.map(MonteCarloTreeSearch.best_action,
                            [mcts1, mcts2, mcts3, mcts4])
            pool.close()
            pool.join()

            max_cnt, best_res = 0, None
            for res in ress:
                cnt = 0
                for i in ress:
                    if (res == i).all():
                        cnt += 1
                if cnt > max_cnt:
                    max_cnt = cnt
                    best_res = res
            tmp = np.where((Game.board == best_res) == False)
            C = 'R{}'.format(best_res[tmp[0][1]][tmp[1][1]])
            Game.txt_strs.append(
                '{}:{};({},{})'.format(Game.index, key, C,
                                       self.chess_dir(tmp[0][1],
                                                      tmp[1][1])))
            Game.index += 1
            Game.board = best_res
            self.show_board()
            os.system('clear')
            print(Game.board)

        else:
            Game.board = np.array([
                [0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0],
                [0, 0, 0, 0, -3],
                [0, 0, 0, -5, -1],
                [0, 0, -4, -2, -6]
            ])
            print('我方摆放: ')
            print(Game.board)
            chess = input('对方摆放: ')
            Game.board[0][0] = int(chess[0])
            Game.board[0][1] = int(chess[1])
            Game.board[0][2] = int(chess[2])
            Game.board[1][0] = int(chess[3])
            Game.board[1][1] = int(chess[4])
            Game.board[2][0] = int(chess[5])
            print(Game.board)
            Game.txt_strs.append(
                'R:A5-{};B5-{};C5-{};A4-{};B4-{};A3-{}'.format(
                    Game.board[0][0], Game.board[0][1], Game.board[0][2],
                    Game.board[1][0], Game.board[1][1], Game.board[2][0]))
            Game.txt_strs.append(
                'B:E3{};D2{};E2{};C1{};D1{};E1{}'.format(
                    Game.board[2][4], Game.board[3][3], Game.board[3][4],
                    Game.board[4][2], Game.board[4][3], Game.board[4][4]))
Esempio n. 10
0
    def on_right_click(self, event):
        os.system('clear')
        i = (event.y - 20) // 88
        j = (event.x - 20) // 88
        if Game._focus is not None:
            tp = Game._focus
            Game._focus = None
            self.print_chess(tp[0], tp[1], 'empty', 0)
            self.print_chess(i, j, tp[2], tp[3])
            self.print_board();
            key1 = input('对方色子: ')
            C = 'B{}'.format(abs(tp[3])) if Game.first else 'R{}'.format(
                abs(tp[3]))
            Game.txt_strs.append('{}:{};({},{})'.format(Game.index, key1, C,
                                                        self.chess_dir(i, j)))
            Game.index += 1
            if self.game_over() is not None:
                if self.game_over() == 1:
                    print('先手方胜')
                else:
                    print('后手方胜')
                self.get_txt_file()
                return
            key2 = int(input('我方色子: '))
            state1 = State(Game.board.copy(), 1 if Game.first else -1, key2)
            state2 = State(Game.board.copy(), 1 if Game.first else -1, key2)
            state3 = State(Game.board.copy(), 1 if Game.first else -1, key2)
            state4 = State(Game.board.copy(), 1 if Game.first else -1, key2)
            node1 = MonteCarloTreeSearchNode(state1)
            node2 = MonteCarloTreeSearchNode(state2)
            node3 = MonteCarloTreeSearchNode(state3)
            node4 = MonteCarloTreeSearchNode(state4)
            mcts1 = MonteCarloTreeSearch(node1)
            mcts2 = MonteCarloTreeSearch(node2)
            mcts3 = MonteCarloTreeSearch(node3)
            mcts4 = MonteCarloTreeSearch(node4)

            pool = Pool(4)
            ress = pool.map(MonteCarloTreeSearch.best_action,
                            [mcts1, mcts2, mcts3, mcts4])
            pool.close()
            pool.join()

            max_cnt, best_res = 0, None
            for res in ress:
                cnt = 0
                for i in ress:
                    if (res == i).all():
                        cnt += 1
                if cnt > max_cnt:
                    max_cnt = cnt
                    best_res = res
            tmp = np.where((Game.board == best_res) == False)
            if Game.first:
                C = 'R{}'.format(best_res[tmp[0][1]][tmp[1][1]])
                Game.txt_strs.append(
                    '{}:{};({},{})'.format(Game.index, key2, C,
                                           self.chess_dir(tmp[0][1],
                                                          tmp[1][1])))
            else:
                C = 'B{}'.format(-best_res[tmp[0][0]][tmp[1][0]])
                Game.txt_strs.append(
                    '{}:{};({},{})'.format(Game.index, key2, C,
                                           self.chess_dir(tmp[0][0],
                                                          tmp[1][0])))
            Game.index += 1
            Game.board = best_res
            self.show_board()
            self.print_board()
            if self.game_over() is not None:
                if self.game_over() == 1:
                    print('先手方胜')
                else:
                    print('后手方胜')
                self.get_txt_file()
                return