Esempio n. 1
0
def time_decision():
    game = PentagoGame()
    arrays =  [
        [
            [None,    1, None, None, None, None],
            [None, None,    1, None,    0, None],
            [None,    0, None,    1, None,    0],
            [None, None, None, None,    1, None],
            [None, None, None,    0, None, None],
            [None, None, None, None, None, None],
        ],
        [
            [None, None, None, None, None, None],
            [None, None, None, None, None, None],
            [None, None, None, None, None, None],
            [None, None, None, None, None, None],
            [None, None, None, None, None, None],
            [None, None, None, None, None, None],
        ],
        [
            [   0,    0,    0,    0, None, None],
            [None, None,    1, None, None, None],
            [None, None,    1, None, None, None],
            [None, None,    1, None, None, None],
            [None, None, None, None, None, None],
            [None, None, None, None, None, None],
        ],
        [
            [None, None, None, None, None, None],
            [None, None, None, None, None, None],
            [None,    1, None, None, None, None],
            [   0,    0, None,    0,    0,    0],
            [None, None,     1, None,   1, None],
            [None, None,     1, None, None, None],
        ],
    ]

    times = []
    for b in map(array_to_bin, arrays):
        state = game.make_state(b=b)

        t0 = time.clock()
        alphabeta_cutoff_search(state, game, d=2)
        t1 = time.clock()
        d = t1 - t0
        times.append(d)
        print("%.3f" % d)

    print('search took %.3fs.' % sum(times))
    print("times = ", times)
Esempio n. 2
0
def move(array, token):
    b = array_to_bin(array)
    state = game.make_state(b=b)
    i, q, clockwise = alphabeta_cutoff_search(state, game, d=2)
    coor = (i // 6, i % 6)
    center = [(1, 1), (1, 4), (4, 1), (4, 4)][q]
    move = (coor, center, clockwise)
    print("token: %s  |  move: %s" % (state.num_moves % 2, move))
    return move
Esempio n. 3
0
def demo_play():
    array = [[None] * 6 for _ in range(6)]
    num_moves = 0
    state = GameState(array=array, num_moves=num_moves, utility=None)
    while True:
        move = alphabeta_cutoff_search(state, game, d=3)

        print("token: %s  |   move: %s" % (state.num_moves % 2, move))
        game.display(state)
        print()
        # input('                                       ....continue?')
        new_state = game.result(state, move)
        state = new_state
Esempio n. 4
0
def decision_test1():
    array = [
        [0, 0, 0, 0, None, None],
        [None, None, None, None, None, None],
        [None, None, None, None, None, None],
        [None, None, None, None, None, None],
        [None, None, None, None, None, None],
        [None, None, None, None, None, None],
    ]
    state = game.make_state(array=array, num_moves=1)
    move = alphabeta_cutoff_search(state, game, d=1)
    print('move:', move)
    assert move[0] == (0, 4)
    print("passes decision_test1")
Esempio n. 5
0
def decision_test2():
    array = [
        [0, None, None, None, None, None],
        [None, 0, None, None, None, None],
        [None, None, 0, None, None, None],
        [None, None, None, 0, None, None],
        [None, None, None, None, None, None],
        [None, None, None, None, None, None],
    ]
    state = GameState(array=array, num_moves=1, utility=None)
    move = alphabeta_cutoff_search(state, game, d=1)
    print(move)
    # assert  move[0] == (4, 4)
    print("passes decision_test2")
Esempio n. 6
0
def decision_test2():
    array = [
        [   0, None, None, None, None, None],
        [None,    0, None, None, None, None],
        [None, None,    0, None, None, None],
        [None, None, None,    0, None, None],
        [None, None, None, None, None, None],
        [None, None, None, None, None, None],
    ]
    b = array_to_bin(array)
    state = game.make_state(b=b, num_moves=1)
    move = alphabeta_cutoff_search(state, game, d=1)
    print('move:', move)
    assert move[0] == 28
    print("passes decision_test2")
Esempio n. 7
0
    player = 2
    computer = 1
    # action = game.alphabeta_cutoff_search(state,game)
    # state = game.result(state,action)
while not exit:
    print(state)
    if game.to_move(state) == player:
        entry = input("Please insert your move: <row> <column> ")
        if "exit" in entry:
            exit == True
        elif len(entry.split(" ")) == 2:
            row = int(entry.split(" ")[0])
            column = int(entry.split(" ")[1])
            state = game.result(state, (player, row, column))
    elif game.to_move(state) == computer:
        action = games.alphabeta_cutoff_search(state, game, d=4)
        state = game.result(state, action)
    else:
        print("Error")
        sys.exit(0)
    if game.terminal_test(state):
        print(state)
        result = game.utility(state, player)
        if result == 1:
            print("YOU WON")
        elif result == -1:
            print("YOU LOST")
        else:
            print("DRAW")
        sys.exit(0)
Esempio n. 8
0
def make_alphabeta_player(N=0):
    """ Returns a player function that uses alpha_beta search to depth N """
    if N == 0:
        return lambda g, s: games.alphabeta_search(s, g)
    else:
        return lambda g, s: games.alphabeta_cutoff_search(s, g, d=N)
Esempio n. 9
0
def move(array, token):
    num_moves = token
    state = GameState(array=array, num_moves=num_moves, utility=None)
    move = alphabeta_cutoff_search(state, game, d=2)
    print("token: %s  |  move: %s" % (state.num_moves % 2, move))
    return move
Esempio n. 10
0
def alphabeta_depth8(game, state):
    return games.alphabeta_cutoff_search(state, game, d=8)
Esempio n. 11
0
print(a_game)

# A player is represented by a search function that takes a game instance and a state and returns a move.   The game's methods (actions, result, utility and terminal_test) do the real work
#
# The aima code defines several search functions and some players based on them

# In[34]:

# minimax returns a move by using the minimax algorithm
# to search all the way down to leaves to choose best move
minimax = lambda g, s: games.minimax_decision(s, g)

# these return a move by using the alphabeta algorithm
# to search to a given depth to choose best move
dumb = lambda g, s: games.alphabeta_cutoff_search(s, g, d=1)
smart = lambda g, s: games.alphabeta_cutoff_search(s, g, d=3)
smarter = lambda g, s: games.alphabeta_cutoff_search(s, g, d=20)

# Smartest return a move by using the alphabeta algorithm
# to search down to leaves to choose best move
smartest = games.alphabeta_player

# random just returns a random move
random = games.random_player

# In[35]:

a_game = TicTacToe()
a_game.play_game(minimax, minimax)