Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Tic Tac Toe Player
- """
- import math
- X = "X"
- O = "O"
- EMPTY = None
- def main():
- board = [[O, O, X],
- [X, X, O],
- [EMPTY, X, EMPTY]]
- print(utility(board))
- 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.
- """
- xo_count = 0
- for row in board:
- for square in row:
- if square is not EMPTY:
- xo_count += 1
- if xo_count % 2 == 0:
- return X
- else:
- return O
- raise NotImplementedError
- def actions(board):
- """
- Returns set of all possible actions (i, j) available on the board.
- """
- possible_places = set()
- for i in range(1, 4) :
- for j in range(1, 4) :
- if board[j-1][i-1] is EMPTY:
- possible_places.add((i, j))
- return possible_places
- raise NotImplementedError
- def result(board, action):
- """
- Returns the board that results from making move (i, j) on the board.
- """
- players_turn = player(board)
- board_copy = board
- i, j = action
- board_copy[j-1][i-1] = players_turn
- return board_copy
- raise NotImplementedError
- def winner(board):
- """
- Returns the winner of the game, if there is one.
- """
- mid = board[1][1]
- 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:
- return mid
- for row in board:
- if row.count(row[0]) == 3 and row[0] != EMPTY:
- return row[0]
- for column in range(3):
- if board[0][column] == board[1][column] and board[0][column] == board[2][column] and board[1][column] != EMPTY:
- return board[0][column]
- return None
- raise NotImplementedError
- def terminal(board):
- """
- Returns True if game is over, False otherwise.
- """
- if winner(board) != EMPTY or actions(board) == {}:
- return True
- else:
- return False
- raise NotImplementedError
- 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
- else:
- return 0
- raise NotImplementedError
- def minimax(board):
- """
- Returns the optimal action for the current player on the board.
- """
- pos_actions = actions(board)
- temp_max = [-2,(0, 0)]
- temp_min = [2,(0, 0)]
- for action in pos_actions:
- upd_board = result(board, action)
- if player(board) == X:
- if max_value(upd_board) > temp_max[0]:
- temp_max[0] = max_value(upd_board)
- temp_max[1] = action
- else:
- if min_value(upd_board) < temp_min[0]:
- temp_min[0] = min_value(upd_board)
- temp_min[1] = action
- if player(board) == X:
- return temp_max[1]
- else:
- return temp_min[1]
- raise NotImplementedError
- def max_value(board):
- """
- Returns the best option/action for X for a given board
- """
- if terminal(board):
- return utility(board)
- v = -2
- for action in actions(board):
- v = max(v, min_value(result(board, action)))
- if v == 1:
- break
- return v
- def min_value(board):
- """
- Returns the best option/action for O for a given board
- """
- if terminal(board):
- return utility(board)
- v = 2
- for action in actions(board):
- v = min(v, max_value(result(board, action)))
- if v == -1:
- break
- return v
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement