You are on page 1of 1

def betterEvaluationFunction(currentGameState):

"""
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
evaluation function (question 5).
DESCRIPTION: <write something here so we know what you did>
"""
"*** YOUR CODE HERE ***"
newPos = currentGameState.getPacmanPosition()
newFood = currentGameState.getFood()
newGhostStates = currentGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
heuristic = 0
for st in newScaredTimes:
heuristic += st
ghostDistances = []
for gs in newGhostStates:
ghostDistances += [manhattanDistance(gs.getPosition(),newPos)]
foodList = newFood.asList()
wallList = currentGameState.getWalls().asList()
emptyFoodNeighbors = 0
foodDistances = []
def foodNeighbors(foodPos):
foodNeighbors = []
foodNeighbors.append((foodPos[0]-1,foodPos[1]))
foodNeighbors.append((foodPos[0],foodPos[1]-1))
foodNeighbors.append((foodPos[0],foodPos[1]+1))
foodNeighbors.append((foodPos[0]+1,foodPos[1]))
return foodNeighbors
for f in foodList:
neighbors = foodNeighbors(f)
for fn in neighbors:
if fn not in wallList and fn not in foodList:
emptyFoodNeighbors += 1
foodDistances += [manhattanDistance(newPos,f)]
inverseFoodDist = 0
if len(foodDistances) > 0:
inverseFoodDist = 1.0/(min(foodDistances))
heuristic += (min(ghostDistances)*((inverseFoodDist**4)))
heuristic += currentGameState.getScore()-(float(emptyFoodNeighbors)*4.5)
return heuristic

You might also like