From 8ce3754cb0f191351b6f32377bde358bf7548cd0 Mon Sep 17 00:00:00 2001 From: snsd0805 Date: Tue, 7 May 2024 21:52:34 +0800 Subject: [PATCH] feat: complete q8 --- bustersAgents.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/bustersAgents.py b/bustersAgents.py index a6fd060..1d522a1 100644 --- a/bustersAgents.py +++ b/bustersAgents.py @@ -149,5 +149,44 @@ class GreedyBustersAgent(BustersAgent): [beliefs for i, beliefs in enumerate(self.ghostBeliefs) if livingGhosts[i+1]] "*** YOUR CODE HERE ***" - raiseNotDefined() + + # find the position where the ghost most likely exist. + livingGhostMostExistPosition = [] + for ghost in livingGhostPositionDistributions: + max_belief = 0 + max_location = None + for ghost_position, belief in ghost.items(): + if belief > max_belief: + max_belief = belief + max_location = ghost_position + livingGhostMostExistPosition.append(max_location) + + # find the nearest ghost + min_distance = 1e9 + min_position = None + for ghost_position in livingGhostMostExistPosition: + distance = self.distancer.getDistance(pacmanPosition, ghost_position) + if distance < min_distance: + min_distance = distance + min_position = ghost_position + nearest_ghost_position = min_position + + # find the new position which is closet with the ghost + min_distance = 1e9 + min_action = None + for action in legal: + new_pacman_position = Actions.getSuccessor(pacmanPosition, action) + distance = self.distancer.getDistance(new_pacman_position, nearest_ghost_position) + if distance < min_distance: + min_distance = distance + min_action = action + next_action = min_action + + return next_action + + + + + + "*** END YOUR CODE HERE ***"