def decrement_timer(ghost_state):
     """Decrement ghost's scared timer."""
     timer = ghost_state.scared_timer
     if timer == 1:
         ghost_state.configuration.position = nearest_point(
             ghost_state.configuration.position)
     ghost_state.scared_timer = max(0, timer - 1)
Пример #2
0
    def __str__(self):
        """Generate string representation of state."""
        width, height = self.layout.width, self.layout.height
        map = Grid(width, height)
        if isinstance(self.food, tuple):
            self.food = reconstitute_grid(self.food)
        for x in range(width):
            for y in range(height):
                food, walls = self.food, self.layout.walls
                map[x][y] = self._food_wall_str(food[x][y], walls[x][y])

        for agent_state in self.agent_states:
            if agent_state is None:
                continue
            if agent_state.configuration is None:
                continue
            x, y = [
                int(i)
                for i in util.nearest_point(agent_state.configuration.position)
            ]
            agent_dir = agent_state.configuration.direction
            if agent_state.is_pacman:
                map[x][y] = self._pac_str(agent_dir)
            else:
                map[x][y] = self._ghost_str(agent_dir)

        for x, y in self.capsules:
            map[x][y] = 'o'

        return str(map) + ("\n_score: %d\n" % self.score)
 def get_successor(self, game_state, action):
     """Find the next successor: a grid position (location tuple)."""
     successor = game_state.generate_successor(self.index, action)
     pos = successor.get_agent_state(self.index).get_position()
     if pos != nearest_point(pos):
         # Only half a grid position was covered
         return successor.generate_successor(self.index, action)
     else:
         return successor
Пример #4
0
    def apply_action(state, action, agent_index):
        """Edit the state to reflect the results of the action."""
        legal = AgentRules.get_legal_actions(state, agent_index)
        if action not in legal:
            raise Exception("Illegal action " + str(action))

        # Update Configuration
        agent_state = state.data.agent_states[agent_index]
        speed = 1.0
        # if agent_state.is_pacman: speed = 0.5
        vector = Actions.direction_to_vector(action, speed)
        old_config = agent_state.configuration
        agent_state.configuration = old_config.generate_successor(vector)

        # Eat
        next = agent_state.configuration.get_position()
        nearest = nearest_point(next)

        if next == nearest:
            is_red = state.is_on_red_team(agent_index)
            # Change agent type
            agent_state.is_pacman =\
                [is_red, state.is_red(agent_state.configuration)
                 ].count(True) == 1
            # if he's no longer pacman, he's on his own side,
            # so reset the num carrying timer
            # agent_state.num_carrying *= int(agent_state.is_pacman)
            if agent_state.num_carrying > 0 and not agent_state.is_pacman:
                score = (agent_state.num_carrying if is_red else -1 *
                         agent_state.num_carrying)
                state.data.score_change += score

                agent_state.num_returned += agent_state.num_carrying
                agent_state.num_carrying = 0

                red_count = 0
                blue_count = 0
                for index in range(state.get_num_agents()):
                    agent_state = state.data.agent_states[index]
                    if index in state.get_red_team_indices():
                        red_count += agent_state.num_returned
                    else:
                        blue_count += agent_state.num_returned
                if (((red_count >= (TOTAL_FOOD / 2) - MIN_FOOD)
                     or (blue_count >= (TOTAL_FOOD / 2) - MIN_FOOD))):
                    state.data._win = True

        if agent_state.is_pacman and manhattan_distance(nearest, next) <= 0.9:
            AgentRules.consume(nearest, state,
                               state.is_on_red_team(agent_index))
Пример #5
0
    def get_action(self, game_state):
        """
        Calls choose_action on a grid position, but continues on half positions.
        If you subclass CaptureAgent, you shouldn't need to override this method.  It
        takes care of appending the current game_state on to your observation history
        (so you have a record of the game states of the game) and will call your
        choose action method if you're in a state (rather than halfway through your last
        move - this occurs because Pacman agents move half as quickly as ghost agents).

        """
        self.observation_history.append(game_state)

        my_state = game_state.get_agent_state(self.index)
        my_pos = my_state.get_position()
        if my_pos != nearest_point(my_pos):
            # We're halfway from one position to the next
            return game_state.get_legal_actions(self.index)[0]
        else:
            return self.choose_action(game_state)
Пример #6
0
    def apply_action(state, action):
        """
        Edits the state to reflect the results of the action.
        """
        legal = PacmanRules.get_legal_actions(state)
        if action not in legal:
            raise Exception("Illegal action " + str(action))

        pacman_state = state.data.agent_states[0]

        # Update Configuration
        vector = Actions.direction_to_vector(action, PacmanRules.PACMAN_SPEED)
        pacman_state.configuration = pacman_state.configuration.generate_successor(vector)

        # Eat
        next = pacman_state.configuration.get_position()
        nearest = nearest_point(next)
        if manhattan_distance(nearest, next) <= 0.5:
            # Remove food
            PacmanRules.consume(nearest, state)
Пример #7
0
 def decrement_timer(ghost_state):
     timer = ghost_state.scared_timer
     if timer == 1:
         ghost_state.configuration.pos = nearest_point(
             ghost_state.configuration.pos)
     ghost_state.scared_timer = max(0, timer - 1)
Пример #8
0
 def decrement_timer(state):
     timer = state.scared_timer
     if timer == 1:
         state.configuration.pos = nearest_point(state.configuration.pos)
     state.scared_timer = max(0, timer - 1)
Пример #9
0
 def get_successor(self, game_state, action):
     successor = game_state.generate_successor(self.index, action)
     pos = successor.get_agent_state(self.index).get_position()
     if pos != util.nearest_point(pos):
         return successor.generate_successor(self.index, action)
     return successor