Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def betterEvaluationFunction(currentGameState):
- """
- Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
- evaluation function (question 5).
- DESCRIPTION:
- This is essentially our reflex agent, but pertaining the current state only.
- 1. Start with score 0
- 2. Add 200 * (4 - # of capsules left)
- This incites pacman to eat capsules (if there are <4; qty can be varied arbitrarily)
- 3. Subtract twice the distance to the nearest capsule (if pacman is w/in 5 of it)
- This makes pacman beeline for a capsule once he gets close
- 4. Subtract the distance to the nearest piece of food (if NOT w/in 5 of a capsule)
- This makes pacman continuously move toward food
- 5. Subtract the distance to the nearest ghost if it is "afraid"
- This makes pacman try to eat vulnerable ghosts
- 6. Add the distance to the nearest "non-afraid" ghost (if pacman is w/in 3 of it)
- This makes pacman keep away from ghosts, but also not give undue thought to distant ghosts
- 7. Add 4 (if NOT w/in 3 of any ghost)
- This keeps pacman from trying to be too friendly with the ghosts :)
- 8. Subtract 500 if pacman occupies the same space as a ghost
- Prevents pacman from suiciding
- 9. Subtract 250 if pacman occupies a space adjacent to a ghost
- Keeps pacman from getting into the ghosts' attack range
- 10. Subtract the amount of food left on the board
- Gives pacman an incentive to eat food he is adjacent to
- 11. Add 4 times the current score
- Clobbers any corner cases where pacman gets stuck next to a piece of food
- """
- newPos = currentGameState.getPacmanPosition()
- newFood = currentGameState.getFood()
- newGhostStates = currentGameState.getGhostStates()
- newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
- newGhostPositions = [ghostState.getPosition() for ghostState in newGhostStates]
- newFoodCount = newFood.count()
- newFoodPositions = newFood.asList()
- newCapsulePositions = currentGameState.getCapsules()
- scaredGhosts = []
- numGhosts = len(newGhostStates)
- for i in xrange(0, numGhosts):
- if not newScaredTimes[i] == 0:
- scaredGhosts.append(newGhostPositions[i])
- score = 0
- targetCapsule = None
- capsuleDist = 999999
- for capsulePos in newCapsulePositions:
- tempDist = manhattanDistance(newPos, capsulePos)
- if tempDist < 5:
- targetCapsule = capsulePos
- capsuleDist = tempDist
- if not targetCapsule == None:
- score -= capsuleDist * 2
- score += 200 * (4 - len(newCapsulePositions))
- minDist = 999999
- bestFood = None
- if capsuleDist >= 5:
- for foodPos in newFoodPositions:
- currDist = manhattanDistance(newPos, foodPos)
- if currDist < minDist:
- minDist = currDist
- bestFood = foodPos
- if not newFoodCount == 0:
- score -= minDist
- minDist = 999999
- isOnGhost = False
- isNextToGhost = False
- closestGhost = None
- for ghostPos in newGhostPositions:
- currDist = manhattanDistance(newPos, ghostPos)
- if currDist == 0 and ghostPos not in scaredGhosts:
- isOnGhost = True
- if currDist == 1 and ghostPos not in scaredGhosts:
- isNextToGhost = True
- if currDist < minDist:
- minDist = currDist
- closestGhost = ghostPos
- if closestGhost in scaredGhosts:
- score -= minDist
- else:
- if minDist < 3:
- score += minDist
- else:
- score += 4
- score -= newFoodCount
- if isOnGhost:
- score -= 500
- if isNextToGhost:
- score -= 250
- score += currentGameState.getScore() * 4
- return score
Advertisement
Add Comment
Please, Sign In to add comment