Exemple #1
0
def betterEvaluationFunction(currentGameState: GameState) -> float:
    """
      Your extreme, unstoppable evaluation function (problem 4). Note that you can't fix a seed in this function.
    """
    score = currentGameState.getScore()
    numFoods = currentGameState.getNumFood()
    score -= 4.5 * numFoods
    pacPos = currentGameState.getPacmanPosition()
    numCapsules = len(currentGameState.getCapsules())
    ghostStates = [
        manhattanDistance(ghost, pacPos)
        for ghost in currentGameState.getGhostPositions()
    ]
    minGhost = min(ghostStates)
    index = ghostStates.index(minGhost)
    minGhost = 1.0 / minGhost
    if currentGameState.getGhostState(index + 1).scaredTimer > 5:
        minGhost *= -200
    score -= minGhost
    score -= numCapsules * 30
    if numFoods >= 1:
        foods = currentGameState.getFood().data
        num = 0
        arr = []
        for i in range(len(foods)):
            for j in range(len(foods[0])):
                if foods[i][j]:
                    arr.append((i, j))
                    num += 1
                    if num == numFoods:
                        break
            if num == numFoods:
                break
        nearest = min([util.manhattanDistance(pacPos, food) for food in arr])
        if numFoods > 1:
            score += 3.0 * pow(nearest, -1.0)
        else:
            score += 5.0 * pow(nearest, -1.0)
    return score