Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Tic Tac Toe Player
- """
- import math
- import runner as rn
- X = "X"
- O = "O"
- EMPTY = None
- def initial_state():
- """
- Returns starting state of the board.
- """
- return [[EMPTY, EMPTY, EMPTY],
- [EMPTY, EMPTY, EMPTY],
- [EMPTY, EMPTY, EMPTY]]
- def player(board):
- """
- Returns player who has the next turn on a board.
- """
- flat_board = [item for sublist in board for item in sublist] # flatten the list of list
- Xs = sum(x==X for x in flat_board) # count nb of Xs in board
- Os = sum(x==O for x in flat_board) # and Os
- if Xs < Os:
- res = X
- elif Xs > Os:
- res = O
- # break a tie check the starting user
- elif rn.user == X: # None check already done in runner before this call
- res = 0
- else:
- res = X
- return res
- def actions(board):
- """
- Returns set of all possible actions (i, j) available on the board.
- """
- raise NotImplementedError
- def result(board, action):
- """
- Returns the board that results from making move (i, j) on the board.
- """
- return [[O, EMPTY, EMPTY],
- [EMPTY, EMPTY, EMPTY],
- [EMPTY, EMPTY, EMPTY]]
- def winner(board):
- """
- Returns the winner of the game, if there is one.
- """
- raise NotImplementedError
- def terminal(board):
- """
- Returns True if game is over, False otherwise.
- """
- return False
- def utility(board):
- """
- Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
- """
- raise NotImplementedError
- def minimax(board):
- """
- Returns the optimal action for the current player on the board.
- """
- return [2,1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement