@@ -162,8 +162,62 @@ def getAction(self, gameState: GameState):
162162 All ghosts should be modeled as choosing uniformly at random from their
163163 legal moves.
164164 """
165- "*** YOUR CODE HERE ***"
166- util .raiseNotDefined ()
165+ numGhosts = gameState .getNumAgents ()
166+ agentIndex = 0
167+ maxVal = - 99999999
168+ bestAction = 0
169+ depth = 1
170+
171+
172+ successor = {}
173+ for i in range ((numGhosts - 1 )):
174+ successor [i ]= i + 1
175+ successor [(numGhosts - 1 )]= 0
176+ nextAgent = successor [agentIndex ]
177+
178+ for action in gameState .getLegalActions (agentIndex ):
179+ nextState = gameState .generateSuccessor (agentIndex ,action )
180+ actionVal = self .expectAgentNode (nextAgent ,depth ,nextState ,successor )
181+ if actionVal > maxVal :
182+ maxVal = actionVal
183+ bestAction = action
184+
185+ return bestAction
186+
187+ def expectAgentNode (self ,agentIndex ,depth ,gameState ,successor ):
188+ import numpy as np
189+ expectVal = []
190+ nextAgent = successor [agentIndex ]
191+
192+ if gameState .getLegalActions (agentIndex )== []:
193+ return self .evaluationFunction (gameState )
194+
195+ if nextAgent == 0 :
196+ for action in gameState .getLegalActions (agentIndex ):
197+ nextState = gameState .generateSuccessor (agentIndex ,action )
198+ expectVal .append (self .maxAgentNode (nextAgent , depth , nextState ,successor ))
199+ return np .mean (expectVal )
200+ else :
201+ for action in gameState .getLegalActions (agentIndex ):
202+ nextState = gameState .generateSuccessor (agentIndex ,action )
203+ expectVal .append (self .expectAgentNode (nextAgent , depth , nextState ,successor ))
204+ return np .mean (expectVal )
205+
206+
207+
208+ def maxAgentNode (self ,agentIndex ,depth ,gameState ,successor ):
209+ depth += 1
210+ maxVal = - 99999999
211+ nextAgent = successor [agentIndex ]
212+
213+ if depth == (self .depth + 1 ) or gameState .getLegalActions (agentIndex )== []:
214+ return self .evaluationFunction (gameState )
215+
216+
217+ for action in gameState .getLegalActions (agentIndex ):
218+ nextState = gameState .generateSuccessor (agentIndex , action )
219+ maxVal = max (maxVal , self .expectAgentNode (nextAgent ,depth ,nextState ,successor ))
220+ return maxVal
167221
168222def betterEvaluationFunction (currentGameState : GameState ):
169223 """
0 commit comments