master
huangjielun 2 years ago
parent 831eed19c4
commit e74b67de06

@ -74,7 +74,6 @@ class ReflexAgent(Agent):
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
#many ghosts
"*** YOUR CODE HERE ***"
GhostPos = successorGameState.getGhostPositions()
x_pacman,y_pacman = newPos
failedDist = min([(abs(each[0]- x_pacman) + abs(each[1]-y_pacman)) for each in GhostPos])
@ -172,8 +171,8 @@ class MinimaxAgent(MultiAgentSearchAgent):
Returns whether or not the game state is a losing state
"""
"*** YOUR CODE HERE ***"
'''
GhostIndex = [i for i in range(1, gameState.getNumAgents())]
def gameOver(state, d):
return state.isWin() or state.isLose() or d == self.depth
def min_value(state, d, ghost): # minimizer
@ -203,6 +202,46 @@ class MinimaxAgent(MultiAgentSearchAgent):
res.sort(key=lambda k: k[1])
return res[-1][0]
util.raiseNotDefined()
'''
def gameOver(gameState):
return gameState.isWin() or gameState.isLose()
#Be different with me
def min_value(gameState, depth, ghost):
value = float('inf')
if gameOver(gameState):
return self.evaluationFunction(gameState)
for action in gameState.getLegalActions(ghost):
if ghost == gameState.getNumAgents() - 1:
value = min(value, max_value(gameState.generateSuccessor(ghost, action), depth))
else:
value = min(value, min_value(gameState.generateSuccessor(ghost, action), depth, ghost + 1))
return value
def max_value(gameState, depth):
value = float('-inf')
depth = depth + 1
#Be different with me
if depth == self.depth or gameOver(gameState):
return self.evaluationFunction(gameState)
for action in gameState.getLegalActions(0):
value = max(value, min_value(gameState.generateSuccessor(0, action), depth, 1))
return value
nextAction = gameState.getLegalActions(0)
Max = float('-inf')
Result = None
for action in nextAction:
if (action != "stop"):
depth = 0
value = min_value(gameState.generateSuccessor(0, action), depth, 1)
if (value > Max):
Max = value
Result = action
return Result
util.raiseNotDefined()
class AlphaBetaAgent(MultiAgentSearchAgent):
"""
@ -235,7 +274,7 @@ class AlphaBetaAgent(MultiAgentSearchAgent):
return self.evaluationFunction(state)
value = float('-inf')
for action in state.getLegalActions(0):
if action == 'Stop':
if action == 'stop':
continue
value = max(value,min_value(state.generateSuccessor(0,action),depth,1,alpha,beta))
if value > beta:
@ -248,14 +287,12 @@ class AlphaBetaAgent(MultiAgentSearchAgent):
alpha = float('-inf')
beta = float('inf')
for action in state.getLegalActions(0):
if action == 'Stop':
if action == 'stop':
continue
tmpValue = min_value(state.generateSuccessor(0,action),0,1,alpha,beta)
if value < tmpValue:
value = tmpValue
actions = action
if value > beta:
return value
alpha = max(value,alpha)
return actions
return function(gameState)

Loading…
Cancel
Save