def calculate_best_score(self, game_state: GameState, action): # pacman next_game_state = game_state.getPacmanNextState(action) next_pacman_position = next_game_state.getPacmanPosition() # ghost # next_ghost_position = tuple(map(lambda v: int(v), next_game_state.getGhostPosition(1))) # print('\nnext Ghost position = ', next_ghost_position) next_ghost_positions = next_game_state.getGhostPositions() next_food_layout = next_game_state.getFood() food_points = ReflexAgent.eaten_food_points if next_game_state.getNumFood() < game_state.getNumFood() else 0 ghost_penalization = ReflexAgent.ghost_penalization if self.pacman_will_die(next_pacman_position, next_ghost_positions) else 0 scores = [] for x in range(next_food_layout.width): for y in range(next_food_layout.height): if self.has_food(next_food_layout, x, y): pacman_distance_to_food = self.calculate_food_distance(game_state, pacman_position=next_pacman_position, food_position=(x, y)) distance_points = ReflexAgent.distance_coefficient / pacman_distance_to_food score = distance_points + food_points - ghost_penalization scores.append({'distance': pacman_distance_to_food, 'score': score, 'foodAt': [x, y], 'action': action}) if len(scores) == 0: score = ReflexAgent.all_food_eaten_points - ghost_penalization scores.append({'distance': 0, 'score': score, 'foodAt': None}) # best score first scores.sort(key=lambda s: s['score'], reverse=True) # get the best score return scores[0]