Question: PYTHON PROGRAMMING__ import sys import math import util # Minimax: # If there are no more choices to make, return the heuristic value # If

PYTHON PROGRAMMING__  import sys import math import util # Minimax: # If there are no more choices to make, return the heuristic value # If it's the player's turn, # we pick the best value of all children, or negative infinity # initialized should be to the min value # If it's the computer's turn, # we pick the least value of all the children, or positive infinity #how to say what node is the next , and which nothing is nothing left # isTerminal / isAvailable, # children # value NEGATIVE_INFINITY = -float('inf') INFINITY = float('inf') """ Max is going to take an array of values and tell us what the maximum one is but we really want both the value and moves that is associated with the maximum value. So we are going to return the pair that tells what value it was and the max value. Top level requirements: When we call Minimax our goal is to make the best move. We will think of the score as metadata about whether or not it is possible to win down a path. """ def minimax(node):# add depth var #import pdb; pdb.set_trace() if node.is_terminal(): return(node, node.value()) # <-- returns the score, not the choice that made the score minimax_results = [(child, minimax(child)) for child in node.children()] #Returns the child and the associated value if node.is_player_one: max_value_seen_so_far = NEGATIVE_INFINITY chosen_move = None for this_move, minimax_result in minimax_results: #for each child value calculated by minimax if the value found is #print("minimax_one : " + str(minimax_result)) #larger than the one we are currently tracking then store it ! next_next_move, this_result_value = minimax_result if max_value_seen_so_far < this_result_value: max_value_seen_so_far = this_result_value chosen_move = this_move #print("The Chosen move : " + str(chosen_move)) #Choosen move is picked based on the children not based on our children's children return (chosen_move, max_value_seen_so_far) #Returns the move and the max value that is associated with it #return max(child_values, default = NEGATIVE_INFINITY) else: min_value_seen_so_far = INFINITY chosen_move = None for this_move, minimax_result in minimax_results: #print("minimax : " + str(minimax_result)) next_next_move, this_result_value = minimax_result if min_value_seen_so_far > this_result_value: min_value_seen_so_far = this_result_value chosen_move = this_move return (chosen_move, min_value_seen_so_far) #Return the move and the min value that is associated with it # print(child_values) # return min(child_values, default= INFINITY) # Minimax should return two things: # the board that results from the next move # the value that is associated with the next move board def lastMove(self): perviousMove= None if last_lastmove in minimax(node): return move class GameBoard(object): def __init__(self, is_player_one, spaces): self.is_player_one = is_player_one self.spaces = spaces# list of each space 0-8, if unused it's None #self.spaces = spaces.getField().getAvailableMoves() def is_terminal(self): # is the board full? #all is function that will return True only when all the elements are Truthy #For example >>> # all([False, True, True]) # False # >>> all([True,True,True]) # True #isTheBoardFull = [space for space in self.spaces if space == None] isTheBoardFull = [space != None for space in self.spaces] print("Spaces :" + str(self.spaces)) allspaces = all(isTheBoardFull) or self.someoneWon() return allspaces def children(self): open_spaces = [idx for idx, space in enumerate(self.spaces) if space == None] #print("Hey made it :" + str(self.spaces)) child_boards = [] for open_space_idx in open_spaces: new_spaces = [s for s in self.spaces] #self.is_player_one is always going to be true, we assume it will never be false # thereby never allowing player_2 to making a move on the board if self.is_player_one: new_spaces[open_space_idx] = 'x' # player = not self.is_player_one # new_board = GameBoard(self.is_player_one, new_spaces) # child_boards.append(new_board) else: new_spaces[open_space_idx] = 'o' new_board = GameBoard(not self.is_player_one, new_spaces) child_boards.append(new_board) return child_boards #return the the value of a node def value(self): # any is a function that will return True when at least one of the elements is Truthy # For example >>> # any([False, True, False]) # True winningCombos = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [6,4,2]] my_mark = 'x' if self.is_player_one else 'o' if any([group[0] == group[1] and group[1] == group[2] and group[0] is my_mark for group in winningCombos]): # why the choice of "and" instead of "or" considering that the any returns True when at least one of the list elements is True. return (my_mark,1) elif self.someoneWon(): return -1 return 0 #This method tests whether someone has one the game ! def someoneWon(self): winningCombos = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [6,4,2]] my_mark = 'x' if self.is_player_one else 'o' #if all three are the same and the first one first one = emptyspace for winningCombos in winningCombos: if self.spaces[winningCombos[0]] == self.spaces[winningCombos[1]] == self.spaces[winningCombos[2]]: if self.spaces[winningCombos[0]] is not None: return True return False # return any([self.space[group[0]] == self.space[group[1]] == self.space[group[2]] and self.space[group[0]] != None for self.space[group] in winningCombos]) def X_won(self): return self.someoneWon() == 'x' def O_won(self): return self.someoneWon() == 'o' def tied(self): return all([space != None for space in self.spaces]) == True and self.winner() is None def get_PlayerSquares(self, player): return [i for i, value in enumerate(self.spaces) if value == player] def winner(self): winningCombos = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [6,4,2]] for player in ('x','o'): positions = self.get_Playersquares(player) for combo in winningCombos: win = True for spot in combo: if spot not in positions: win = False if win: return player return None # The underlining issue is a Nonetype object which is not iterable. # To fix the problem I put empty list in place of the noneType objects #emptySpace = "_" initial_board = GameBoard(True, ([None] * 9)) minimax(initial_board) 
#Remaining task now is based on the board I get out of the miniMax function I need to compare the state I was given to the new state to figure out what that last move is. How can figure out what the last# 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!