def move_count(test_state, team): w_moves = get_all_moves(test_state, 'W') b_moves = get_all_moves(test_state, 'B') if team == 'W': return len(w_moves) - len(b_moves) else: return len(b_moves) - len(w_moves)
def minimax(self, position, depth, alpha, beta, maximizingPlayer): if depth == 0: return self.evaluate(position) endgame = check_end(position) if endgame: if(endgame == self.team_type): return float("inf") if(endgame == self.enemy): return float("-inf") return 0 # ??? How should tying be considered? Not likely to occur, but useful if maximizingPlayer: maxEval = float("-inf") for move in get_all_moves(position, self.team_type): child = update_board(position.copy(), move) eval = self.minimax(child, depth - 1, alpha, beta, False) maxEval = max(maxEval, eval) alpha = max(alpha, eval) if beta <= alpha: break return maxEval else: minEval = float("inf") for move in get_all_moves(position, self.enemy): child = update_board(position.copy(), move) eval = self.minimax(child, depth - 1, alpha, beta, True) minEval = min(minEval, eval) beta = min(beta, eval) if beta <= alpha: break return minEval
def simulate(test_state, team): """simulate the utility of current state by random picking a step""" player = team while not terminal_test(test_state): action = random.choice(list(get_all_moves(test_state, team))) test_state = update_board(test_state, action) v = utility(test_state, player) return -v
def check_end(board_state): # Check the board to see if the game can continue # If the game is over return the winner: 'W', 'B', or 'T' # Otherwise, return None if len(get_all_moves(board_state, 'W')) != 0 or len(get_all_moves(board_state, 'B')) != 0: return None white_count = sum(row.count('W') for row in board_state) black_count = sum(row.count('B') for row in board_state) if white_count == black_count: return 'T' elif white_count > black_count: return 'W' else: return'B'
def get_move(self, board_state): # board state will be an board_size by board_size array with the current state of the game. # Possible values are: 'W', 'B', or '-' # Return your desired move (If invalid, instant loss) # Example move: ('W', (1, 6)) moves = get_all_moves(board_state, self.team_type) if len(moves) > 0: return random.choice(moves) return (self.team_type, None)
def expand(n): if not n.children and not terminal_test(n.state): n.children = { MCT_Node(state=update_board(n.state, action), team=reverse(n.team), move=action, parent=n): action for action in get_all_moves(n.state, n.team) } return select(n)
def terminal_test(state): moves = get_all_moves(state) if len(moves) == 0: return True
def mobility(board_state, player): return len(get_all_moves(board_state, player))
def get_move(self, board_state): moves = get_all_moves(board_state, self.team_type) if len(moves) > 0: return random.choice(moves) return (self.team_type, None)