Advertisement
Guest User

My TTT code

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