Advertisement
jules0707

ttt

Apr 15th, 2023 (edited)
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. """
  2. Tic Tac Toe Player
  3. """
  4.  
  5. import math
  6. import runner as rn
  7.  
  8.  
  9. X = "X"
  10. O = "O"
  11. EMPTY = None
  12.  
  13.  
  14. def initial_state():
  15.     """
  16.    Returns starting state of the board.
  17.    """
  18.     return [[EMPTY, EMPTY, EMPTY],
  19.             [EMPTY, EMPTY, EMPTY],
  20.             [EMPTY, EMPTY, EMPTY]]
  21.  
  22.  
  23. def player(board):
  24.     """
  25.    Returns player who has the next turn on a board.
  26.    """
  27.     flat_board = [item for sublist in board for item in sublist] # flatten the list of list
  28.    
  29.     Xs = sum(x==X for x in flat_board) # count nb of Xs in board
  30.     Os = sum(x==O for x in flat_board) # and Os
  31.    
  32.     if Xs < Os:
  33.         res = X
  34.     elif Xs > Os:
  35.         res = O
  36.         # break a tie check the starting user
  37.     elif rn.user == X: # None check already done in runner before this call
  38.         res = 0
  39.     else:
  40.         res = X
  41.     return res
  42.  
  43.  
  44. def actions(board):
  45.     """
  46.    Returns set of all possible actions (i, j) available on the board.
  47.    """
  48.     raise NotImplementedError
  49.  
  50.  
  51. def result(board, action):
  52.     """
  53.    Returns the board that results from making move (i, j) on the board.
  54.    """
  55.     return [[O, EMPTY, EMPTY],
  56.             [EMPTY, EMPTY, EMPTY],
  57.             [EMPTY, EMPTY, EMPTY]]
  58.  
  59.  
  60. def winner(board):
  61.     """
  62.    Returns the winner of the game, if there is one.
  63.    """
  64.     raise NotImplementedError
  65.  
  66.  
  67. def terminal(board):
  68.     """
  69.    Returns True if game is over, False otherwise.
  70.    """
  71.     return False
  72.  
  73.  
  74. def utility(board):
  75.     """
  76.    Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
  77.    """  
  78.     raise NotImplementedError
  79.  
  80.  
  81. def minimax(board):
  82.     """
  83.    Returns the optimal action for the current player on the board.
  84.    """
  85.     return [2,1]
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement