Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Tic Tac Toe Player
- """
- import math, copy
- X = "X"
- O = "O"
- EMPTY = None
- turn = 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.
- """
- global turn
- counter = 0
- for row in board:
- for cell in row:
- if cell != EMPTY:
- counter+=1
- x = counter % 2
- if x != 0:
- turn = O
- return O
- else:
- turn = X
- return X
- def actions(board):
- """
- Returns set of all possible actions (i, j) available on the board.
- """
- poss_actions = list()
- for row in range(len(board)):
- for cell in range(len(board[row])):
- if board[row][cell] == EMPTY:
- poss_actions.append((row, cell))
- return poss_actions
- def result(board, action):
- """
- Returns the board that results from making move (i, j) on the board.
- """
- global turn
- copy_board = copy.deepcopy(board)
- row = action[0]
- cell = action[1]
- if turn == X:
- copy_board[row][cell] = X
- elif turn == O:
- copy_board[row][cell] = O
- return copy_board
- def winner(board):
- """
- Returns the winner of the game, if there is one.
- """
- x_counter = 0
- o_counter = 0
- for row in board:
- for cell in row:
- if cell == X:
- x_counter += 1
- elif cell == O:
- o_counter += 1
- if x_counter == 3:
- return X
- elif o_counter == 3:
- return O
- x_counter = 0
- o_counter = 0
- def terminal(board):
- """
- Returns True if game is over, False otherwise.
- """
- condition = False
- for row in board:
- for cell in row:
- if cell == EMPTY:
- condition = True
- if condition == True and winner(board) == None:
- return False
- return True
- def utility(board):
- """
- Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
- """
- if winner(board) == X:
- return 1
- elif winner(board) == O:
- return -1
- elif winner(board) == None:
- return 0
- def minimax(board):
- """
- Returns the optimal action for the current player on the board.
- """
- if terminal(board):
- return None
- print("Called upon.")
- global turn
- current_turn = turn
- #iterate over every possible action in board, and use MAX and MIN VALUE functions
- possible_actions = actions(board)
- v = 0
- opt_action = None
- for action in possible_actions:
- if turn == X:
- maxval = MAX_VALUE(board, action)
- if maxval == 1:
- v = maxval
- opt_action = action
- break
- elif maxval > v:
- v = maxval
- opt_action = action
- elif maxval == v:
- v = maxval
- opt_action = action
- elif turn == O:
- minval = MIN_VALUE(board, action)
- if minval == -1:
- v = minval
- op_action = action
- break
- elif minval > v:
- v = minval
- opt_action = action
- elif minval == v:
- opt_action = action
- turn = current_turn
- return opt_action
- def MAX_VALUE(board, action):
- """
- Returns the max value of the current action.
- """
- global turn
- turn = X
- v = float("-inf")
- new_board = result(board, action)
- pos_actions = actions(new_board)
- if not terminal(new_board):
- for action in pos_actions:
- min_value = MIN_VALUE(new_board, action)
- v = max(v, min_value)
- else:
- min_value = utility(new_board)
- v = max(v, min_value)
- return v
- def MIN_VALUE(board, action):
- """
- Returns the min value of the current action.
- """
- global turn
- turn = O
- v = float("inf")
- new_board = result(board, action)
- pos_actions = actions(new_board)
- if not terminal(new_board):
- for action in pos_actions:
- max_value = MAX_VALUE(new_board, action)
- v = min(v, max_value)
- else:
- max_value = utility(new_board)
- v = min(v, max_value)
- return v
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement