Esempio n. 1
0
    def applyAction(state, action):
        """
        Edits the state to reflect the results of the action.
        """
        legal = PacmanRules.getLegalActions(state)
        if action not in legal:
            raise Exception("Illegal action " + str(action))

        pacmanState = state.data.agentStates[0]

        # Update Configuration
        vector = Actions.directionToVector(action, PacmanRules.PACMAN_SPEED)
        pacmanState.configuration = pacmanState.configuration.generateSuccessor(vector)

        # Eat
        next = pacmanState.configuration.getPosition()
        nearest = nearestPoint(next)
        if manhattanDistance(nearest, next) <= 0.5:
            # Remove food
            PacmanRules.consume(nearest, state)
Esempio n. 2
0
 def getFurthestCorner(self, pacPos):
     poses = [(1, 1), (1, self.height - 2), (self.width - 2, 1),
              (self.width - 2, self.height - 2)]
     dist, pos = max([(manhattanDistance(p, pacPos), p) for p in poses])
     return pos
Esempio n. 3
0
    def checkDeath(state, agentIndex):
        agentState = state.data.agentStates[agentIndex]
        if state.isOnRedTeam(agentIndex):
            otherTeam = state.getBlueTeamIndices()
        else:
            otherTeam = state.getRedTeamIndices()
        if agentState.isPacman:
            for index in otherTeam:
                otherAgentState = state.data.agentStates[index]
                if otherAgentState.isPacman: continue
                ghostPosition = otherAgentState.getPosition()
                if ghostPosition == None: continue
                if manhattanDistance(
                        ghostPosition,
                        agentState.getPosition()) <= COLLISION_TOLERANCE:
                    # award points to the other team for killing Pacmen
                    if otherAgentState.scaredTimer <= 0:
                        AgentRules.dumpFoodFromDeath(state, agentState,
                                                     agentIndex)

                        score = KILL_POINTS
                        if state.isOnRedTeam(agentIndex):
                            score = -score
                        state.data.scoreChange += score
                        agentState.isPacman = False
                        agentState.configuration = agentState.start
                        agentState.scaredTimer = 0
                        agentState.frozenTimer = 0
                        agentState.freezeTimer = 0
                        agentState.jumpTimer = 0

                    else:
                        score = KILL_POINTS
                        if state.isOnRedTeam(agentIndex):
                            score = -score
                        state.data.scoreChange += score
                        otherAgentState.isPacman = False
                        otherAgentState.configuration = otherAgentState.start
                        otherAgentState.scaredTimer = 0
                        otherAgentState.frozenTimer = 0
                        otherAgentState.freezeTimer = 0
                        otherAgentState.jumpTimer = 0

        else:  # Agent is a ghost
            for index in otherTeam:
                otherAgentState = state.data.agentStates[index]
                if not otherAgentState.isPacman: continue
                pacPos = otherAgentState.getPosition()
                if pacPos == None: continue
                if manhattanDistance(
                        pacPos,
                        agentState.getPosition()) <= COLLISION_TOLERANCE:
                    #award points to the other team for killing Pacmen
                    if agentState.scaredTimer <= 0:
                        AgentRules.dumpFoodFromDeath(state, otherAgentState,
                                                     agentIndex)

                        score = KILL_POINTS
                        if not state.isOnRedTeam(agentIndex):
                            score = -score
                        state.data.scoreChange += score
                        otherAgentState.isPacman = False
                        otherAgentState.configuration = otherAgentState.start
                        otherAgentState.scaredTimer = 0
                        otherAgentState.frozenTimer = 0
                        otherAgentState.freezeTimer = 0
                        otherAgentState.jumpTimer = 0

                    else:
                        score = KILL_POINTS
                        if state.isOnRedTeam(agentIndex):
                            score = -score
                        state.data.scoreChange += score
                        agentState.isPacman = False
                        agentState.configuration = agentState.start
                        agentState.scaredTimer = 0
                        agentState.frozenTimer = 0
                        agentState.freezeTimer = 0
                        agentState.jumpTimer = 0
Esempio n. 4
0
def noisyDistance(pos1: int, pos2: int) -> int:
    return int(
        manhattanDistance(pos1, pos2) + random.choice(SONAR_NOISE_VALUES))
Esempio n. 5
0
    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:
            action = 'STOP'
        cost = Costs[action]
        state.data.scoreChange += -cost if state.isOnRedTeam(
            agentIndex) else cost

        if action == 'FROZEN' or action == 'STOP':
            return
        if action == 'FREEZE':
            agentState = state.data.agentStates[agentIndex]
            agentState.freezeTimer = 0
            position = agentState.configuration.getPosition()
            for index in range(state.getNumAgents()):
                if index == agentIndex:
                    continue
                otherAgentState = state.data.agentStates[index]
                otherPosition = otherAgentState.getPosition()
                if otherPosition != None and manhattanDistance(
                        otherPosition, position) <= 3:
                    otherAgentState.frozenTimer = FROZEN_TIME
        else:
            # Update Configuration
            agentState = state.data.agentStates[agentIndex]

            # Jump Cooldown
            if "Jump_" in action:
                agentState.jumpTimer = 0

            speed = 1.0
            # if agentState.isPacman: speed = 0.5
            vector = Actions.directionToVector(action, speed)
            oldConfig = agentState.configuration
            agentState.configuration = oldConfig.generateSuccessor(vector)

            # Eat
            next = agentState.configuration.getPosition()
            nearest = nearestPoint(next)

            if next == nearest:
                isRed = state.isOnRedTeam(agentIndex)
                # Change agent type
                agentState.isPacman = [
                    isRed, state.isRed(agentState.configuration)
                ].count(True) == 1
                # if he's no longer pacman, he's on his own side, so reset the num carrying timer
                #agentState.numCarrying *= int(agentState.isPacman)
                if agentState.numCarrying > 0 and not agentState.isPacman:
                    score = agentState.numCarrying if isRed else -1 * agentState.numCarrying
                    state.data.scoreChange += score

                    agentState.numReturned += agentState.numCarrying
                    agentState.numCarrying = 0

                    redCount = 0
                    blueCount = 0
                    for index in range(state.getNumAgents()):
                        agentState = state.data.agentStates[index]
                        if index in state.getRedTeamIndices():
                            redCount += agentState.numReturned
                        else:
                            blueCount += agentState.numReturned
                    if redCount >= (TOTAL_FOOD /
                                    2) - MIN_FOOD or blueCount >= (
                                        TOTAL_FOOD / 2) - MIN_FOOD:
                        state.data._win = True

            if agentState.isPacman and manhattanDistance(nearest, next) <= 0.9:
                AgentRules.consume(nearest, state,
                                   state.isOnRedTeam(agentIndex))
Esempio n. 6
0
 def canKill(pacmanPosition, ghostPosition):
     return manhattanDistance(ghostPosition, pacmanPosition) <= COLLISION_TOLERANCE