Advertisement
JMFT100

Tic-Tac-Toe AI code

May 19th, 2021 (edited)
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.54 KB | None | 0 0
  1. """
  2. Tic Tac Toe Player
  3. """
  4.  
  5. import math, copy
  6.  
  7.  
  8. X = "X"
  9. O = "O"
  10. EMPTY = None
  11. turn = None
  12.  
  13. def initial_state():
  14.     """
  15.    Returns starting state of the board.
  16.    """
  17.     return [[EMPTY, EMPTY, EMPTY],
  18.             [EMPTY, EMPTY, EMPTY],
  19.             [EMPTY, EMPTY, EMPTY]]
  20.  
  21.  
  22. def player(board):
  23.     """
  24.    Returns player who has the next turn on a board.
  25.    """
  26.     global turn
  27.     counter = 0
  28.     for row in board:
  29.         for cell in row:
  30.             if cell != EMPTY:
  31.                 counter+=1
  32.    
  33.     x = counter % 2
  34.  
  35.     if x != 0:
  36.         turn = O
  37.         return O
  38.     else:
  39.         turn = X
  40.         return X
  41.  
  42. def actions(board):
  43.     """
  44.    Returns set of all possible actions (i, j) available on the board.
  45.    """
  46.     poss_actions = list()
  47.  
  48.     for row in range(len(board)):
  49.         for cell in range(len(board[row])):
  50.             if board[row][cell] == EMPTY:
  51.                 poss_actions.append((row, cell))
  52.    
  53.     return poss_actions
  54.            
  55.  
  56. def result(board, action):
  57.     """
  58.    Returns the board that results from making move (i, j) on the board.
  59.    """
  60.     global turn
  61.     copy_board = copy.deepcopy(board)
  62.     row = action[0]
  63.     cell = action[1]
  64.     if turn == X:
  65.         copy_board[row][cell] = X
  66.     elif turn == O:
  67.         copy_board[row][cell] = O
  68.     return copy_board
  69.  
  70.  
  71. def winner(board):
  72.     """
  73.    Returns the winner of the game, if there is one.
  74.    """
  75.     x_counter = 0
  76.     o_counter = 0
  77.  
  78.     for row in board:
  79.         for cell in row:
  80.             if cell == X:
  81.                 x_counter += 1
  82.             elif cell == O:
  83.                 o_counter += 1
  84.            
  85.         if x_counter == 3:
  86.             return X
  87.         elif o_counter == 3:
  88.             return O
  89.         x_counter = 0
  90.         o_counter = 0
  91.  
  92.            
  93. def terminal(board):
  94.     """
  95.    Returns True if game is over, False otherwise.
  96.    """
  97.     condition = False
  98.     for row in board:
  99.         for cell in row:
  100.             if cell == EMPTY:
  101.                condition = True
  102.    
  103.     if condition == True and winner(board) == None:
  104.         return False
  105.    
  106.     return True
  107.    
  108.  
  109.  
  110. def utility(board):
  111.     """
  112.    Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
  113.    """
  114.     if winner(board) == X:
  115.         return 1
  116.     elif winner(board) == O:
  117.         return -1
  118.     elif winner(board) == None:
  119.         return 0
  120.  
  121.  
  122. def minimax(board):
  123.     """
  124.    Returns the optimal action for the current player on the board.
  125.    """
  126.     if terminal(board):
  127.         return None
  128.  
  129.     print("Called upon.")
  130.     global turn
  131.     current_turn = turn
  132.  
  133.     #iterate over every possible action in board, and use MAX and MIN VALUE functions
  134.     possible_actions = actions(board)
  135.     v = 0
  136.     opt_action = None
  137.  
  138.     for action in possible_actions:
  139.         if turn == X:
  140.             maxval = MAX_VALUE(board, action)
  141.             if maxval == 1:
  142.                 v = maxval
  143.                 opt_action = action
  144.                 break
  145.             elif maxval > v:
  146.                 v = maxval
  147.                 opt_action = action
  148.             elif maxval == v:
  149.                 v = maxval
  150.                 opt_action = action
  151.         elif turn == O:
  152.             minval = MIN_VALUE(board, action)
  153.             if minval == -1:
  154.                 v = minval
  155.                 op_action = action
  156.                 break
  157.             elif minval > v:
  158.                 v = minval
  159.                 opt_action = action
  160.             elif minval == v:
  161.                 opt_action = action
  162.  
  163.     turn = current_turn
  164.     return opt_action
  165.  
  166.        
  167. def MAX_VALUE(board, action):
  168.     """
  169.    Returns the max value of the current action.
  170.    """
  171.     global turn
  172.     turn = X
  173.     v = float("-inf")
  174.     new_board = result(board, action)
  175.     pos_actions = actions(new_board)
  176.     if not terminal(new_board):
  177.         for action in pos_actions:
  178.             min_value = MIN_VALUE(new_board, action)
  179.             v = max(v, min_value)
  180.     else:
  181.         min_value = utility(new_board)
  182.         v = max(v, min_value)
  183.     return v
  184.  
  185.  
  186. def MIN_VALUE(board, action):
  187.     """
  188.    Returns the min value of the current action.
  189.    """
  190.     global turn
  191.     turn = O
  192.     v = float("inf")
  193.     new_board = result(board, action)
  194.     pos_actions = actions(new_board)
  195.     if not terminal(new_board):
  196.         for action in pos_actions:
  197.             max_value = MAX_VALUE(new_board, action)
  198.             v = min(v, max_value)
  199.     else:
  200.         max_value = utility(new_board)
  201.         v = min(v, max_value)
  202.     return v
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement