Example #1
0
            else:
                child = mm_move(board_child, provided.switch_player(player))    
                strategy[child[0]] = square
        
        if player == provided.PLAYERX:
            opt_s = max(strategy.keys())
        else:
            opt_s = min(strategy.keys())
        
        opt_m = strategy[opt_s]

    return opt_s, opt_m


def move_wrapper(board, player, trials):
    """
    Wrapper to allow the use of the same infrastructure that was used
    for Monte Carlo Tic-Tac-Toe.
    """
    move = mm_move(board, player)
    assert move[1] != (-1, -1), "returned illegal move (-1, -1)"
    return move[1]

# Test game with the console or the GUI.
# Uncomment whichever you prefer.
# Both should be commented out when you submit for
# testing to save time.

#provided.play_game(move_wrapper, 1, False)        
poc_ttt_gui.run_gui(3, provided.PLAYERO, move_wrapper, 1, False)
Example #2
0
    """
    dim = board.get_dim()
    scores = [[0 for dummy_col in range(dim)] for dummy_row in range(dim)]
    for dummy_trial in range(trials):
        board_copy = board.clone()
        mc_trial(board_copy, player)
        mc_update_scores(scores, board_copy, player)
    return get_best_move(board, scores)


# Test game with the console or the GUI.  Uncomment whichever
# you prefer.  Both should be commented out when you submit
# for testing to save time.

#provided.play_game(mc_move, NTRIALS, False)
poc_ttt_gui.run_gui(3, provided.PLAYERX, mc_move, NTRIALS, False)


def ttt_test_suite():
    """
    Test suite for tic tac toe Monte Carlo implementation.
    """
    board1 = provided.TTTBoard(3)
    print board1.get_dim()
    print board1
    print mc_trial(board1, provided.PLAYERX)
    print board1
    print '-----------------'
    board2 = provided.TTTBoard(3)
    next_move = mc_move(board2, provided.PLAYERX, 10)
    print next_move
    for dummy_trial in range(trials):
        board_copy = board.clone()
        mc_trial(board_copy, player)
        mc_update_scores(scores, board_copy, player)
    return get_best_move(board, scores)                    

def score_grid(dim):
    """
    Creates empty score grid for use in mc_move
    """
    return [ [0 for dummy_col in range(dim)] for dummy_row in range(dim)]

def get_high_score(empty_squares, scores):
    """
    Returns high score for get_best_move function
    """
    max_score = float("-inf")
   
    for coord in empty_squares:
        if scores[coord[0]][coord[1]] >= max_score:
            max_score = scores[coord[0]][coord[1]]
    return max_score


# Test game with the console or the GUI.  Uncomment whichever 
# you prefer.  Both should be commented out when you submit 
# for testing to save time.

# provided.play_game(mc_move, NTRIALS, False)        
poc_ttt_gui.run_gui(3, provided.PLAYERX, mc_move, NTRIALS, False)
        )  # we have to work on the copy, as we are going to have to go back up the tree and use the original board
        board_clone.move(next_move[0], next_move[1],
                         player)  # here we execute the move on our
        score, dummy_move = mm_move(
            board_clone, provided.switch_player(player)
        )  # here we call recursively depth-first search all the way down to each leaf, we then will compare the results at each level on the way back
        # print(board_clone)  # this will show us the board as we go up
        if score * SCORES[
                player] == 1:  # Case 1: the board was scored to be either 1 for X ( * 1) or -1 for Y ( * -1) so someone has won and it is the best choice for that player and we immediately come back
            return score, next_move  # note that here we return the actual score, not the ABSOLUTE VALUE which is only used to same space by using only 'max' instead of both 'max' and 'min'
        elif score * SCORES[player] > result[
                0]:  # Case 2: the board was scored higher than the score before or the base score, ex: we get '0' score so its greater than what we start with -> (-1, (-1, -1))
            result = score, next_move  # in this case we keep the result as it is and we don't return anything as there maay be other boards on this level to investigate (
        # else: result = result  # we don't have to write this, but it is good to know that we are passing the 'result' unchanged

    return result[0] * SCORES[player], result[
        1]  # this happens when we finished scanning the whole level and we did not find the winner on this level, we then either return '0' or the other's player winning number (-1 or 1)


def move_wrapper(board, player, trials):
    """ Wrapper to allow the use of the same infrastructure that was used for Monte Carlo Tic-Tac-Toe. """
    move = mm_move(board, player)
    assert move[1] != (-1, -1), "returned illegal move (-1, -1)"
    return move[1]


# Test game with the console or the GUI. Uncomment whichever you prefer. Both should be commented out when you submit for testing to save time.
# provided.play_game(move_wrapper, 1, False)
poc_ttt_gui.run_gui(3, provided.PLAYERO, move_wrapper, 1,
                    False)  # here we can choose the player
Example #5
0
    best_move = { "score":[], "square":[] }
  
    for option in board.get_empty_squares():
        option_score = scores[option[0]][option[1]]
        if (best_move["score"] == []) or (best_move["score"] < option_score):
            best_move["score"] = option_score
            best_move["square"] = [(option[0], option[1])]
        elif best_move["score"] == option_score:
            best_move["square"].append((option[0], option[1]))
    if len(best_move["square"]) == 1:
        return best_move["square"][0]
    else: 
        return random.choice(best_move["square"]) 
        

def mc_move(board, player, trials):
    """
    This function uses the Monte Carlo simulation 
    to return a move for the machine player in the form of a tuple.
    """
    scores = create_scores(board)
    
    for dummy in range(trials):
        board_clone = board.clone()
        mc_trial(board_clone, player)
        mc_update_scores(scores, board_clone, player)
    return get_best_move(board, scores)   

      
poc_ttt_gui.run_gui(size, player, mc_move, NTRIALS, False)
Example #6
0
        elif score == highest_score:  # if the next value is the same as the highest we simply appen it
            highest_scores.append((row, col))

    random_pick = random.choice(highest_scores)
    row = random_pick[0]
    col = random_pick[1]
    return (row, col)


def mc_move(board, player, trials):
    """ This function takes a current board, which player the machine player is, and the number of trials to run. The function should use the Monte Carlo simulation described above to
    return a move for the machine player in the form of a (row, column) tuple. Be sure to use the other functions you have written! """
    dim = board.get_dim()
    scores = [[0 for dummycol in range(dim)]
              for dummyrow in range(dim)]  # Create empty score board

    for dummy_trial in range(trials):
        completed_board = board.clone()
        mc_trial(completed_board, player)
        mc_update_scores(scores, completed_board, player)

    return get_best_move(board, scores)


# Test game with the console or the GUI.  Uncomment whichever you prefer.  Both should be commented out when you submit for testing to save time.
# provided.play_game(mc_move, NTRIALS, False)
poc_ttt_gui.run_gui(
    DIMENSION, provided.PLAYERX, mc_move, NTRIALS, False
)  # change between 'PLAYERX' and 'PLAYERO' to decide who is going to start
# poc_ttt_gui.run_gui(board dimension, which is the machine player, move function, number of trials per move, reverse argument indicating if you want to play the normal (False) or reverse (True) game.)