def chooseAction(self, observedState): """ Pacman will eat the nearest good capsule if the ghost is not scared, and eat good ghosts otherwise. Along the way, he will adjust his path to run into good ghosts. """ pacmanPos = observedState.getPacmanPosition() legalActs = [a for a in observedState.getLegalPacmanActions()] # find the closest ghost by sorting the distances goodGhost = ghosts.closestGhost(observedState, self.distancer, good=True) # position of closest good capsule to Pacman closeCapsule = capsule.closest(observedState, self.distancer) best_action = random.choice(legalActs) if observedState.scaredGhostPresent(): # targets capsules along the way target = mapH.subTarget(pacmanPos, goodGhost, closeCapsule) for dir in mapH.getDirs(pacmanPos, target): if dir in legalActs: return dir else: # find the closest bad ghost by sorting the distances badGhost = ghosts.getNearestBadGhost(observedState, self.distancer) # sorts out directions that lead to the bad ghost legalActs = [a for a in legalActs if a not in mapH.ghostDirs(pacmanPos, badGhost)] # targets good ghosts along the way target = mapH.subTarget(pacmanPos, closeCapsule, goodGhost) for dir in mapH.getDirs(pacmanPos, closeCapsule): if dir in legalActs: return dir return best_action
def chooseAction(self, observedState): """ Pacman will eat the nearest good capsule if the ghost is not scared, and chase the scared ghost otherwise. """ pacmanPos = observedState.getPacmanPosition() ghost_states = observedState.getGhostStates() # states have getPosition() and getFeatures() methods legalActs = [a for a in observedState.getLegalPacmanActions()] # find the closest ghost by sorting the distances closeGhost = ghosts.closestGhost(observedState, self.distancer, good=False) # find the closest bad ghost by sorting the distances badGhost = ghosts.getNearestBadGhost(observedState, self.distancer) # position of closest good capsule to Pacman closeCapsule = capsule.closest(observedState, self.distancer) best_action = random.choice(legalActs) if observedState.scaredGhostPresent(): # targets capsules and good ghosts on the way to the bad ghost target = mapH.subTarget(pacmanPos, badGhost, closeCapsule) target = mapH.subTarget(pacmanPos, target, closeGhost) for dir in mapH.getDirs(pacmanPos, target): if dir in legalActs: return dir else: # sorts out directions that lead to any close ghost legalActs = [a for a in legalActs if a not in mapH.ghostDirs(pacmanPos, closeGhost)] for dir in mapH.getDirs(pacmanPos, closeCapsule): if dir in legalActs: return dir return best_action
def chooseAction(self, observedState): """ Pacman will eat the nearest good capsule if the ghost is not scared, avoiding all ghosts, and eat the closest ghost otherwise. """ pacmanPos = observedState.getPacmanPosition() legalActs = [a for a in observedState.getLegalPacmanActions()] # find the closest ghost by sorting the distances closeGhost = ghosts.closestGhost(observedState, self.distancer, good=False) # position of closest good capsule to Pacman closeCapsule = capsule.closest(observedState, self.distancer) best_action = random.choice(legalActs) if observedState.scaredGhostPresent(): # targets capsules on the way to ghosts target = mapH.subTarget(pacmanPos, closeGhost, closeCapsule) # chases the ghost for dir in mapH.getDirs(pacmanPos, closeGhost): if dir in legalActs: return dir else: # sorts out directions that lead to any close ghost legalActs = [a for a in legalActs if a not in mapH.ghostDirs(pacmanPos, closeGhost)] # chases the capsule for dir in mapH.getDirs(pacmanPos, closeCapsule): if dir in legalActs: return dir return best_action