master
huangjielun 2 years ago
parent f1c3255457
commit 0f23be4a5b

@ -72,9 +72,45 @@ class ReflexAgent(Agent):
newFood = successorGameState.getFood()
newGhostStates = successorGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
#many ghosts
"*** YOUR CODE HERE ***"
return successorGameState.getScore()
GhostPos = successorGameState.getGhostPositions()
x_pacman,y_pacman = newPos
failedDist = min([(abs(each[0]- x_pacman) + abs(each[1]-y_pacman)) for each in GhostPos])
if failedDist != 0 and failedDist < 4:
ghostScore = -11 / failedDist
else :
ghostScore = 0
nearestFood = float('inf')
width = newFood.width
height = newFood.height
if failedDist >= 2:
dx = [1,0,-1,0]
dy = [0,1,0,-1]
List = []
d = {}
List.append(newPos)
d.update({(x_pacman,y_pacman) : 1})
while List:
tempPos = List[0]
List.pop(0)
temp_x,temp_y = tempPos
if newFood[temp_x][temp_y]:
nearestFood = min(nearestFood,(abs(temp_x - x_pacman) + abs(temp_y - y_pacman)))
break
for i in range(len(dx)):
x = temp_x + dx[i]
y = temp_y + dy[i]
if 0 <= x < width and 0 <= y < height:
tempPos =(x,y)
if tempPos not in d:
d[tempPos] = 1
List.append(tempPos)
if nearestFood != float('inf'):
foodScore = 10 / nearestFood
else :
foodScore = 0
return successorGameState.getScore() + foodScore + ghostScore
def scoreEvaluationFunction(currentGameState):
"""

Loading…
Cancel
Save