Skip to content

Instantly share code, notes, and snippets.

@chychen
Created May 15, 2015 10:28
Show Gist options
  • Save chychen/65745ea474489ca07e95 to your computer and use it in GitHub Desktop.
Save chychen/65745ea474489ca07e95 to your computer and use it in GitHub Desktop.
"P3-2"
class MinimaxAgent(MultiAgentSearchAgent):
"""
Your minimax agent
"""
def getAction(self, gameState):
"""
Returns the minimax action from the current gameState using self.depth
and self.evaluationFunction.
Here are some method calls that might be useful when implementing minimax.
gameState.getLegalActions(agentIndex):
Returns a list of legal actions for an agent
agentIndex=0 means Pacman, ghosts are >= 1
gameState.generateSuccessor(agentIndex, action):
Returns the successor game state after an agent takes an action
gameState.getNumAgents():
Returns the total number of agents in the game
# Choose one of the best actions
scores = [self.evaluationFunction(gameState, action) for action in legalMoves]
bestScore = max(scores)
bestIndices = [index for index in range(len(scores)) if scores[index] == bestScore]
chosenIndex = random.choice(bestIndices) # Pick randomly among the best
return legalMoves[chosenIndex]
"""
"[Project 3] YOUR CODE HERE"
"""
print gameState.getLegalActions(0)
print gameState.getLegalActions(1)
print gameState.getLegalActions(2)
print gameState.getLegalActions(3)
print "num::",gameState.getNumAgents()
print "depth::",self.depth
print range(0,self.depth)
"""
x = input("fdfdfd")
biggest = -10000
actionANS = ''
for action in gameState.getLegalActions(0):
candidate = self.MIN_Value(1,gameState.generateSuccessor(0, action))
print "candidata:::",candidate
if biggest < candidate:
biggest = candidate
actionANS = action
print actionANS
return actionANS
#util.raiseNotDefined()
def MAX_Value(self,dep,gameState):
biggest = -10000
for action in gameState.getLegalActions(0):
biggest = max(biggest,self.MIN_Value(dep,gameState.generateSuccessor(0, action)))
return biggest
def MIN_Value(self,dep,gameState):
smallest = 10000
for i in range(0,gameState.getNumAgents()-1):#012
if i == self.depth-2:#2
dep = dep + 1
print gameState.getLegalActions(i+1)
for action in gameState.getLegalActions(i+1):# i +1 == ghost 3
if dep == self.depth: #dep == 4
print self.evaluationFunction(gameState,action)
return self.evaluationFunction(gameState,action)
else:
smallest = min(smallest,self.MAX_Value(dep,gameState.generateSuccessor(i+1, action))) # i +1 == ghost 3
else:#01
for action in gameState.getLegalActions(i+1):# i +1 == ghost 1/2
smallest = min(smallest,self.MIN_Value(dep,gameState.generateSuccessor(i+1, action)))# i +1 == ghost 1/2
return smallest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment