def applyAction(state, action, agentIndex): """ Edits the state to reflect the results of the action. """ legal = AgentRules.getLegalActions(state, agentIndex) if (action not in legal): raise ValueError('Illegal action: ' + str(action)) agentState = state.getAgentState(agentIndex) # Update position. vector = Actions.directionToVector(action, AgentRules.AGENT_SPEED) agentState.updatePosition(vector) # Eat. nextPosition = agentState.getPosition() nearest = nearestPoint(nextPosition) if (agentState.isPacman() and manhattan(nearest, nextPosition) <= 0.9): AgentRules.consume(nearest, state, state.isOnRedTeam(agentIndex)) # Potentially change agent type. if (nextPosition == nearest): # Agents are pacmen when they are not on their own side. position = agentState.getPosition() agentState.setIsPacman(state.isOnRedTeam(agentIndex) != state.isOnRedSide(position))
def getSuccessor(self, agent, gameState, action): successor = gameState.generateSuccessor(agent, action) pos = successor.getAgentState(agent).getPosition() if (pos != util.nearestPoint(pos)): return successor.generateSuccessor(self.index, action) else: return successor
def getSuccessor(self, agent, gameState, action): # if action not in gameState.getLegalActions(agent): # action = Directions.STOP successor = gameState.generateSuccessor(agent, action) pos = successor.getAgentState(agent).getPosition() if (pos != util.nearestPoint(pos)): return successor.generateSuccessor(agent, action) else: return successor
def getAction(self, gameState): self.observationHistory.append(gameState) myState = gameState.getAgentState(self.index) myPos = myState.getPosition() if (myPos != util.nearestPoint(myPos)): # We're halfway from one position to the next. return gameState.getLegalActions(self.index)[0] else: return self.chooseAction(gameState)
def getDiscreteAgents(self): """ Get the agentTokens, but with interger positions. """ agentTokens = {} for (position, agent) in self._agentTokens.items(): agentTokens[util.nearestPoint(position)] = agent return agentTokens
def getSuccessor(self, gameState, action): """ Finds the next successor which is a grid position (location tuple). """ successor = gameState.generateSuccessor(self.index, action) pos = successor.getAgentState(self.index).getPosition() # Only half a grid position was covered. if (pos != util.nearestPoint(pos)): return successor.generateSuccessor(self.index, action) else: return successor
def getAction(self, gameState): """ Calls `CaptureAgent.chooseAction` on a grid position, but continues on partial positions. If you subclass `CaptureAgent`, you shouldn't need to override this method. It takes care of appending the current state on to your observation history (so you have a record of the game states of the game) and will call your `CaptureAgent.chooseAction` method if you're in a proper state. """ self.observationHistory.append(gameState) myState = gameState.getAgentState(self.index) myPos = myState.getPosition() if (myPos != util.nearestPoint(myPos)): # We're halfway from one position to the next. return gameState.getLegalActions(self.index)[0] else: return self.chooseAction(gameState)
def applyAction(state, action): """ Edits the state to reflect the results of the action. """ legal = PacmanRules.getLegalActions(state) if (action not in legal): raise ValueError('Illegal pacman action: ' + str(action)) pacmanState = state.getPacmanState() # Update position. vector = Actions.directionToVector(action, PacmanRules.PACMAN_SPEED) pacmanState.updatePosition(vector) # Eat. nextPosition = pacmanState.getPosition() nearest = nearestPoint(nextPosition) if (manhattan(nearest, nextPosition) <= 0.5): # Remove food PacmanRules.consume(nearest, state)
def getAction(self, gameState): self.observationHistory.append(gameState) myState = gameState.getAgentState(self.index) myPos = myState.getPosition() if (myPos != util.nearestPoint(myPos)): # We're halfway from one position to the next. return gameState.getLegalActions(self.index)[0] else: actions = gameState.getLegalActions(self.index) depth = 0 max = -float('inf') pinf = float('inf') ninf = -float('inf') bestaction = '' for action in actions: if action != 'Stop': value = self.max_value( gameState.generateSuccessor(self.index, action), depth, ninf, pinf) if value > max: max = value bestaction = action return bestaction
def snapToNearestPoint(self): """ Move the agent to the nearest point to its current location. """ self._position = util.nearestPoint(self._position)
def getNearestPosition(self): return util.nearestPoint(self._position)