def rollout(self): ''' Monte Carlo simulation: simulate a randomized game from the current node until it reaches an end of the game. Outputs: e: the result of the game (X player won:1, tie:0, lose: -1), an integer scalar. Hint: you could use PlayerRandom in problem 1. ''' ######################################### ## INSERT YOUR CODE HERE P1 = PlayerRandom() P2 = PlayerRandom() s = np.copy(self.s) e = TicTacToe.check(s) if (self.x == 1): while (e == None and len(np.where(s == 0)) > 0): r, c = P1.play(s, 1) s[r][c] = 1 e = TicTacToe.check(s) if (e == None and len(np.where(s == 0)) > 0): r, c = P2.play(s, -1) s[r][c] = -1 e = TicTacToe.check(s) else: while (e == None and len(np.where(s == 0)) > 0): r, c = P1.play(s, -1) s[r][c] = -1 e = TicTacToe.check(s) if (e == None and len(np.where(s == 0)) > 0): r, c = P2.play(s, 1) s[r][c] = 1 e = TicTacToe.check(s) ######################################### return e
def rollout(self): ''' Monte Carlo simulation: simulate a randomized game from the current node until it reaches an end of the game. Outputs: e: the result of the game (X player won:1, tie:0, lose: -1), an integer scalar. Hint: you could use PlayerRandom in problem 1. ''' s = np.copy(self.s) x = self.x while TicTacToe.check(s) is None: player_random = PlayerRandom() r, c = player_random.play(s, x=x) s[r, c] = x x = -x e = TicTacToe.check(s) return e
def expand(self): ''' Expand the current tree node for one-level. Add one child node for each possible next move in the game. Inputs: node: the current tree node to be expanded ''' # if the game has already ended, return/exit without expanding the tree if TicTacToe.check(self.s) is not None: return # if the game has not ended yet, expand the current node with one child node for each valid move all_moves = TicTacToe.avail_moves(self.s) for r, c in all_moves: # update s s = np.copy(self.s) s[r, c] = 1 if self.x == 1 else -1 node = Node(s, x=-self.x, parent=self) self.c.append(node)
def expand(self): ''' Expand the current tree node for one-level. Add one child node for each possible next move in the game. Inputs: node: the current tree node to be expanded ''' ######################################### ## INSERT YOUR CODE HERE # if the game has already ended, return/exit without expanding the tree if (TicTacToe.check(self.s) != None): return # if the game has not ended yet, expand the current node with one child node for each valid move valid_moves = TicTacToe.avail_moves(self.s) for move in valid_moves: new_s = np.copy(self.s) i = move[0] j = move[1] new_s[i][j] = self.x Child = Node(s=new_s, x=self.x * -1, parent=self) self.c.append(Child)