feat: complete q8

This commit is contained in:
snsd0805 2024-05-07 21:52:34 +08:00
parent 3d47ae1a15
commit 8ce3754cb0
Signed by: snsd0805
GPG Key ID: 569349933C77A854

View File

@ -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 ***"