Пример #1
0
def mcts_mcts(cp1, N1, cp2, N2, size, print_all=False, first=True):
    #mcts plays with mcts
    Game = HexBoard(size)
    A1_color = 1
    A2_color = 2
    root = Tree(Game, color=A2_color, parent=None, coordinate=None)
    if print_all:
        if first == True:
            print('First move (blue) is mcts')
            print('Second move (red) is mcts')
        else:
            print('First move (blue) is mcts')
            print('Second move (red) is mcts')

    while not Game.is_game_over():
        if first == True:
            #mcts moves first
            A1_move = monte_carlo_tree_search(root, cp=cp1, N=N1)
        else:
            A1_move = monte_carlo_tree_search(root, cp=cp2, N=N2)
        Game.place(coordinates=A1_move.location, color=A1_color)
        if Game.is_game_over():
            if print_all:
                Game.print()
            if first == True:
                #if method1 win, return True
                return Game.check_win(A1_color)
            else:
                #if method1 win, return True
                return Game.check_win(A2_color)
        else:
            root = Tree(Game,
                        color=A1_color,
                        parent=None,
                        coordinate=A1_move.location)
            if first == True:
                A2_move = monte_carlo_tree_search(root, cp=cp2, N=N2)
            else:
                A2_move = monte_carlo_tree_search(root, cp=cp1, N=N1)
            Game.place(coordinates=A2_move.location, color=A2_move.color)
            root = Tree(Game,
                        color=A2_color,
                        parent=None,
                        coordinate=A2_move.location)
    if print_all:
        Game.print()

    if first == True:
        #if method1 win or not
        return Game.check_win(A1_color)
    else:
        return Game.check_win(A2_color)
def simple_dijkstra(board: HexBoard, source, is_max):

    Q = set()
    V_set = board.get_all_vertices()
    dist = {}
    dist_clone = {}  # I made a clone of dist to retain the information in dist
    prev = {}
    for v in V_set:
        dist[v] = np.inf
        dist_clone[v] = np.inf  # clone
        prev[v] = None
        Q.add(v)
    dist[source] = 0
    dist_clone[source] = dist[source]  # clone

    while len(Q) != 0:
        u = min(dist, key=dist.get)
        Q.remove(u)

        color = board.BLUE if is_max else board.RED

        neighbors = board.get_neighbors(u)

        for v in neighbors:
            if v in Q:  # Only check neighbours that are also in "Q"
                len_u_v = -1 if board.is_color(
                    v,
                    color) else 1  # this isn't working as intended i think...
                ### BLOCK TO MAKE AI MORE AGGRESSIVE ###
                if board.border(
                        color, v
                ):  # If there is a move that reaches the border in the simulation
                    if board.check_win(color):  # And it results in a win
                        len_u_v = -2  # Make that move more valuable
                ### END OF AGGRO BLOCK ###
                alt = dist[u] + len_u_v
                if alt < dist[v]:
                    dist[v] = alt
                    dist_clone[v] = dist[v]
                    prev[v] = u
        # We pop "u" from the distance dict to ensure that the keys match the ones in "Q"
        dist.pop(
            u
        )  # This is also why we need the clone, or else we'll return an empty dict

    return dist_clone, prev
Пример #3
0
# sanity check that wins are detected
for i in range(0, 2):
    winner = HexBoard.RED if i == 0 else HexBoard.BLUE
    loser = HexBoard.BLUE if i == 0 else HexBoard.RED
    board = HexBoard(3)
    board.place((1, 1), loser)
    board.place((2, 1), loser)
    board.place((1, 2), loser)
    board.place((2, 2), loser)
    board.place((0, 0), winner)
    board.place((1, 0), winner)
    board.place((2, 0), winner)
    board.place((0, 1), winner)
    board.place((0, 2), winner)
    assert (board.check_win(winner) == True)
    assert (board.check_win(loser) == False)
    board.print()
endable_board = HexBoard(4)
# sanity check that random play will at some point end the game
while not endable_board.game_over:
    endable_board.place((np.random.randint(0, 4), np.random.randint(0, 4)),
                        HexBoard.RED)
assert (endable_board.game_over == True)
assert (endable_board.check_win(HexBoard.RED) == True)
assert (endable_board.check_win(HexBoard.BLUE) == False)
print("Randomly filled board")
endable_board.print()

neighbor_check = HexBoard(5)
assert (neighbor_check.get_neighbors((0, 0)) == [(1, 0), (0, 1)])
Пример #4
0
def human_compu(algor, method='random', depth=3):
    """A function that human play with computer, user can choose their prefered
    board size(3,4 recommanded), color, want to take first move"""
    global flag, INF, n_nodes, cutoff, Game, size

    print('Choose a board size:')
    size = int(input())
    Game = HexBoard(size)
    print('Choose a color: 1(BLUE) or 2(RED)')
    print('Blue: from left to right; Red: from top to bottom')
    opp_color = int(input())
    my_color = Game.get_opposite_color(opp_color)
    print('Do you want to start first? Yes(y) or No(n)')
    first = input()
    print('Game start!')
    # human first move do or not
    if first == 'y' or first == 'Yes':
        Game.print()
        x, y = xy_read(Game)
        Game.place(coordinates=(x, y), color=opp_color)
        Game.print()
        root = Tree(Game,
                    color=opp_color,
                    parent=None,
                    coordinate=(x, y),
                    my_color=my_color)

    else:
        first_color = my_color
        last_color = opp_color
        root = Tree(Game,
                    color=opp_color,
                    parent=None,
                    coordinate=None,
                    my_color=my_color)

    INF = 99999  # sufficient large number

    # human and computer play the game until one of them win
    while not Game.is_game_over():
        if algor == 'alphabeta':
            # varibales intialization
            n_nodes = 0
            cutoff = 0
            flag = 0
            my_move = alphabeta(n=root,
                                a=-INF,
                                b=INF,
                                d=depth,
                                method=method,
                                depth=depth,
                                my_color=my_color,
                                opp_color=opp_color)
            print('n_nodes=', n_nodes, '\n cutoff=', cutoff)

        elif algor == 'idtt':
            flag = 0
            transpositiontable = []
            n_nodes = 0
            cutoff = 0
            my_move = iterativedeepening(n=root,
                                         a=-INF,
                                         b=INF,
                                         DEPTH_stop=5,
                                         time_limit=5,
                                         my_color=my_color,
                                         opp_color=opp_color)
            print('n_nodes=', n_nodes, '\n cutoff=', cutoff)

        elif algor == "mcts":
            my_move = monte_carlo_tree_search(root, cp=1, N=1000)

        # retunred variable "my_move" is a node and contains info about computer's next step
        Game.place(coordinates=my_move.location, color=my_move.color)
        Game.print()

        if Game.is_game_over():
            break
        else:
            # read human's next move
            x, y = xy_read(Game)
            Game.place(coordinates=(x, y), color=opp_color)
            Game.print()
            root = Tree(Game,
                        color=opp_color,
                        parent=None,
                        coordinate=(x, y),
                        my_color=my_color)

    if Game.check_win(opp_color):
        print('Game over! You win :-)')
    else:
        print('Game over! You lose :-(')
Пример #5
0
def mcts_alphabeta(cp=1,
                   N=500,
                   method='dijkstra',
                   depth=3,
                   size=3,
                   print_all=False,
                   first=True,
                   time_limit=5):
    #mcts plays with method
    #method='dijkstra' or 'idtt'
    global flag, user_color, unuse_color, INF, n_nodes, cutoff
    Game = HexBoard(size)
    A1_color = 1
    A2_color = 2
    root = Tree(Game,
                color=A2_color,
                parent=None,
                coordinate=None,
                my_color=A1_color)
    if print_all:
        if first == True:
            print('First move (blue) is mcts')
            print('Second move (red) is ' + method)
        else:
            print('First move (blue) is ' + method)
            print('Second move (red) is mcts')

    while not Game.is_game_over():
        INF = 99999
        flag = 0
        n_nodes = 0
        cutoff = 0
        if first == True:
            #mcts moves first
            A1_move = monte_carlo_tree_search(root, cp=cp, N=N)
        else:
            #method moves first
            if method == 'dijkstra':
                A1_move = alphabeta(n=root,
                                    a=-INF,
                                    b=INF,
                                    d=depth,
                                    method=method,
                                    depth=depth,
                                    my_color=A1_color,
                                    opp_color=A2_color)
            elif method == 'idtt':
                A1_move = iterativedeepening(n=root,
                                             a=-INF,
                                             b=INF,
                                             DEPTH_stop=depth,
                                             time_limit=time_limit,
                                             my_color=A1_color,
                                             opp_color=A2_color)
        Game.place(coordinates=A1_move.location, color=A1_color)
        if Game.is_game_over():
            if print_all:
                Game.print()
            if first == True:
                #if method1 win, return True
                return Game.check_win(A1_color)
            else:
                #if method1 win, return True
                return Game.check_win(A2_color)
        else:
            root = Tree(Game,
                        color=A1_color,
                        parent=None,
                        coordinate=A1_move.location,
                        my_color=A2_color)
            flag = 0
            n_nodes = 0
            cutoff = 0
            if first == True:
                if method == 'idtt':
                    A2_move = iterativedeepening(n=root,
                                                 a=-INF,
                                                 b=INF,
                                                 DEPTH_stop=depth,
                                                 time_limit=time_limit,
                                                 my_color=A2_color,
                                                 opp_color=A1_color)
                elif method == 'dijkstra':
                    A2_move = alphabeta(n=root,
                                        a=-INF,
                                        b=INF,
                                        d=depth,
                                        method=method,
                                        depth=depth,
                                        my_color=A2_color,
                                        opp_color=A1_color)
            else:
                A2_move = monte_carlo_tree_search(root, cp=cp, N=N)
            Game.place(coordinates=A2_move.location, color=A2_move.color)
            root = Tree(Game,
                        color=A2_color,
                        parent=None,
                        coordinate=A2_move.location,
                        my_color=A1_color)
    if print_all:
        Game.print()

    if first == True:
        #if method1 win or not
        return Game.check_win(A1_color)
    else:
        return Game.check_win(A2_color)
Пример #6
0
def idtt_alphabeta(method='random',
                   idtt_depth=3,
                   depth2=3,
                   size=3,
                   print_all=False,
                   first=True,
                   time_limit=5):
    #idtt against alphabeta (random or dijkstra)
    #idtt_depth is idtt's depth; depth2 is method's deptth
    #first determins which moves first
    global flag, INF, n_nodes, cutoff
    Game = HexBoard(size)
    #firt move color=A1_color: blue
    A1_color = 1
    #second move color=A1_color: red
    A2_color = 2
    root = Tree(Game,
                color=A2_color,
                parent=None,
                coordinate=None,
                my_color=A1_color)
    if print_all:
        if first == True:
            print('First move (blue) is idtt')
            print('Second move (red) is ' + method)
        else:
            print('First move (blue) is ' + method)
            print('Second move (red) is idtt')

    while not Game.is_game_over():
        INF = 99999
        flag = 0
        n_nodes = 0
        cutoff = 0
        if first == True:
            #idtt moves first
            A1_move = iterativedeepening(n=root,
                                         a=-INF,
                                         b=INF,
                                         DEPTH_stop=idtt_depth,
                                         time_limit=time_limit,
                                         my_color=A1_color,
                                         opp_color=A2_color)
        else:
            #method2 moves first
            A1_move = alphabeta(n=root,
                                a=-INF,
                                b=INF,
                                d=depth2,
                                method=method,
                                depth=depth2,
                                my_color=A1_color,
                                opp_color=A2_color)
        Game.place(coordinates=A1_move.location, color=A1_color)
        if Game.is_game_over():
            if print_all:
                Game.print()
            if first == True:
                #if idtt win, return True
                return Game.check_win(A1_color)
            else:
                #if idtt win, return True
                return Game.check_win(A2_color)
        else:
            root = Tree(Game,
                        color=A1_color,
                        parent=None,
                        coordinate=A1_move.location,
                        my_color=A2_color)
            n_nodes = 0
            cutoff = 0
            flag = 0
            if first == True:
                A2_move = alphabeta(n=root,
                                    a=-INF,
                                    b=INF,
                                    d=depth2,
                                    method=method,
                                    depth=depth2,
                                    my_color=A2_color,
                                    opp_color=A1_color)
            else:
                A2_move = iterativedeepening(n=root,
                                             a=-INF,
                                             b=INF,
                                             DEPTH_stop=idtt_depth,
                                             time_limit=time_limit,
                                             my_color=A2_color,
                                             opp_color=A1_color)
            Game.place(coordinates=A2_move.location, color=A2_move.color)
            root = Tree(Game,
                        color=A2_color,
                        parent=None,
                        coordinate=A2_move.location,
                        my_color=A1_color)
    if print_all:
        Game.print()

    if first == True:
        #if method1 win or not
        return Game.check_win(A1_color)
    else:
        return Game.check_win(A2_color)
Пример #7
0
def alphabeta_randomVSdijkstra(method1='random',
                               method2='dijkstra',
                               depth1=3,
                               depth2=3,
                               size=3,
                               print_all=False,
                               first=True):
    #alphabeta: method1 and method2 can choose 'random' or 'dijkstra'
    #first==True: method1 moves first
    #first determins which moves first
    global flag, user_color, unuse_color, INF, n_nodes, cutoff
    Game = HexBoard(size)
    #firt move color=A1_color: blue
    A1_color = 1
    #second move color=A1_color: red
    A2_color = 2
    root = Tree(Game,
                color=A2_color,
                parent=None,
                coordinate=None,
                my_color=A1_color)
    if print_all:
        if first == True:
            print('First move (blue) is ' + method1)
            print('Second move (red) is ' + method2)
        else:
            print('First move (blue) is ' + method2)
            print('Second move (red) is ' + method1)

    while not Game.is_game_over():
        INF = 99999
        flag = 0
        n_nodes = 0
        cutoff = 0
        if first == True:
            #method1 moves first
            A1_move = alphabeta(n=root,
                                a=-INF,
                                b=INF,
                                d=depth1,
                                method=method1,
                                depth=depth1,
                                my_color=A1_color,
                                opp_color=A2_color)
        else:
            #method2 moves first
            A1_move = alphabeta(n=root,
                                a=-INF,
                                b=INF,
                                d=depth2,
                                method=method2,
                                depth=depth2,
                                my_color=A1_color,
                                opp_color=A2_color)
        Game.place(coordinates=A1_move.location, color=A1_color)
        if Game.is_game_over():
            if print_all:
                Game.print()
            if first == True:
                #if method1 win, return True
                return Game.check_win(A1_color)
            else:
                #if method1 win, return True
                return Game.check_win(A2_color)
        else:
            root = Tree(Game,
                        color=A1_color,
                        parent=None,
                        coordinate=A1_move.location,
                        my_color=A2_color)
            n_nodes = 0
            cutoff = 0
            flag = 0
            if first == True:
                A2_move = alphabeta(n=root,
                                    a=-INF,
                                    b=INF,
                                    d=depth2,
                                    method=method2,
                                    depth=depth2,
                                    my_color=A2_color,
                                    opp_color=A1_color)
            else:
                A2_move = alphabeta(n=root,
                                    a=-INF,
                                    b=INF,
                                    d=depth1,
                                    method=method1,
                                    depth=depth1,
                                    my_color=A2_color,
                                    opp_color=A1_color)
            Game.place(coordinates=A2_move.location, color=A2_move.color)
            root = Tree(Game,
                        color=A2_color,
                        parent=None,
                        coordinate=A2_move.location,
                        my_color=A1_color)
    if print_all:
        Game.print()

    if first == True:
        #if method1 win or not
        return Game.check_win(A1_color)
    else:
        return Game.check_win(A2_color)