Advertisement
abbarnes

Tic Tac Toe MC Simulation [codeskulptor]

Dec 12th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.18 KB | None | 0 0
  1. """
  2. Monte Carlo Tic-Tac-Toe Player
  3. """
  4. #http://www.codeskulptor.org/#user43_s5eXtEgisZ8i3UR_7.py
  5.  
  6. import random
  7. import poc_ttt_gui
  8. import poc_ttt_provided as provided
  9. #import user43_pWrohuXpCeEdo0y_45 as simple_test
  10.  
  11. # Constants for Monte Carlo simulator
  12. # You may change the values of these constants as desired, but
  13. #  do not change their names.
  14. NTRIALS = 1000         # Number of trials to run
  15. SCORE_CURRENT = 1.0 # Score for squares played by the current player
  16. SCORE_OTHER = 5.0   # Score for squares played by the other player
  17.    
  18. # Add your functions here.
  19.  
  20. def mc_trial(board, player):
  21.     """
  22.    Takes a board and the player whose move it is.  
  23.    Completes the board with random moves, alternating players
  24.    """
  25.     trial_player = player
  26.     while not board.check_win():
  27.         move_space = random.choice(board.get_empty_squares())
  28.         board.move(move_space[0], move_space[1], trial_player)
  29.         trial_player = provided.switch_player(trial_player)
  30.     if not board.check_win():
  31.         print "Error- unfinished game returned"
  32.         return
  33.     else:
  34.         return
  35.    
  36. def add_score(scores, grid):
  37.     """
  38.    adds each element of grid to the corresponding element of scores
  39.    """
  40. #    print "Adding "
  41. #    print grid
  42. #    print "to "
  43. #    print scores
  44.    
  45.     #account for possibilty of empty list instead of list of 0's
  46.     #test program feeds [], not sure about hidden TTTBoard program
  47.     if scores == []:
  48.         return grid
  49.    
  50.     score_row = 0
  51.     score_col = 0
  52.     for grid_row in grid:
  53.         for grid_point in grid_row:
  54.             scores[score_row][score_col] += grid_point
  55.             score_col += 1
  56.         score_row+= 1
  57.         score_col = 0
  58.     return scores
  59.  
  60.    
  61.    
  62. def mc_update_scores(scores, board, player):
  63.     """
  64.    Takes grid of scores (nested list), completed board, and the machine player.
  65.    Appends scores with a score grid for the board.
  66.    """
  67.     #create empty score matching board dimensions
  68.     grid = [[0 for dummy_dim in range(board.get_dim())]
  69.             for dummy_dim in range(board.get_dim())]
  70.    
  71.     game_state = board.check_win()
  72.     if game_state == provided.DRAW:
  73. #        print "DRAW"
  74.         scores = add_score(scores, grid)
  75. #        print "MC Update Score returning "
  76. #        print scores
  77.         return
  78.     elif game_state == player:  
  79.         #print "WIN"
  80.         player_value = SCORE_CURRENT
  81.         opponent_value = -SCORE_OTHER
  82.     elif game_state == provided.switch_player(player):  
  83.         #print "LOSE"
  84.         player_value = -SCORE_CURRENT
  85.         opponent_value = SCORE_OTHER
  86.     #print "Player value: " + str(player_value)
  87.     #print "Opponent value: " + str(opponent_value)
  88.    
  89.     new_score = []
  90.     row_index = 0
  91.     for dummy_row in grid:
  92.         new_score_row = []
  93.         for col_index in range(board.get_dim()):
  94.             if board.square(row_index, col_index) == provided.EMPTY:
  95.                 new_score_row.append(0)
  96.             elif board.square(row_index, col_index) == player:
  97.                 new_score_row.append(player_value)
  98.             elif board.square(row_index, col_index) == provided.switch_player(player):
  99.                 new_score_row.append(opponent_value)
  100.             else:
  101.                 print "Error, unknown value in square " + str(row_index) + "," + str(col_index)
  102.         new_score.append(new_score_row)
  103.         row_index += 1
  104.     #print new_score
  105.     scores = add_score(scores, new_score)
  106.    
  107.     if not game_state:
  108.         print "Error- incomplete game sent to mc_update_scores function."
  109.         print "Printing incomplete board:"
  110.         print board
  111.         return
  112. #    print "MC Update Score returning "
  113. #    print scores
  114.     return
  115.  
  116. def get_best_move(board, scores):
  117.     """
  118.    Given a board/score, returns a random open square with score >= the max score.
  119.    """
  120.     empty_squares = board.get_empty_squares()
  121. #    print "All open moves: "
  122. #    print empty_squares
  123.     if empty_squares == []:
  124.         print "Error:  board full, no move available"
  125.         return
  126.     max_empty_squares = []
  127.     max_score = scores[empty_squares[0][0]][empty_squares[0][1]]
  128.     #print "Starting with max score of " + str(max_score)
  129.     for square in empty_squares:
  130.         if scores[square[0]][square[1]] == max_score:
  131.             max_empty_squares.append(square)
  132.         elif scores[square[0]][square[1]] > max_score:
  133.             max_score = scores[square[0]][square[1]]
  134.             max_empty_squares = [square]
  135.     random.shuffle(max_empty_squares)
  136. #    print "Possible moves culled to include: "
  137. #    print max_empty_squares
  138.     return max_empty_squares[0]    
  139.            
  140. def mc_move(board, player, trials):
  141.     """
  142.    returns a tuple indicating the best move for machine given a board, which player the machine is
  143.    and the # of trials to simulate
  144.    """
  145.     scores = [[0.0 for dummy_dim in range(board.get_dim())]
  146.             for dummy_dim in range(board.get_dim())]
  147.     for dummy_num in range(trials):
  148.         test_board = board.clone()
  149.         mc_trial(test_board, player)
  150.         mc_update_scores(scores, test_board, player)
  151.     next_move = get_best_move(board, scores)
  152.     #instructions say to return next_move, not to MAKE the move
  153.     #commenting out code that makes the move
  154.     #board.move(next_move[0], next_move[1], player)
  155.     #print board
  156.     return next_move
  157.  
  158.  
  159.  
  160. #simple_test.run_suite_mc_trial(mc_trial)
  161. #simple_test.run_suite_add_score(add_score)
  162. #simple_test.run_suite_mc_update_scores(mc_update_scores)
  163. #simple_test.run_suite_get_best_move(get_best_move)
  164. #simple_test.run_suite_mc_move(mc_move)
  165.  
  166.  
  167.  
  168. #my_board = provided.TTTBoard(3)
  169. #player = provided.PLAYERX
  170. #my_board.move(0,0,provided.PLAYERO)
  171. #
  172. #print my_board
  173. #print "Player is " + str(player)
  174. #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
  175. #print ""
  176. #mc_trial(my_board, player)
  177. #print my_board
  178. #print "Player is " + str(player)
  179. #mc_update_scores([], my_board, player)
  180.  
  181. # Test game with the console or the GUI.  Uncomment whichever
  182. # you prefer.  Both should be commented out when you submit
  183. # for testing to save time.
  184.  
  185. provided.play_game(mc_move, NTRIALS, False)        
  186. poc_ttt_gui.run_gui(4, provided.PLAYERO, mc_move, NTRIALS, False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement