Advertisement
JMFT100

Tic-Tac-Toe Problem.

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