コード例 #1
0
ファイル: play.py プロジェクト: MikeynJerry/ece517
 def getLegalActions(state, ghostIndex):
     """
     Ghosts cannot stop, and cannot turn around unless they
     reach a dead end, but can turn 90 degrees at intersections.
     """
     conf = state.getGhostState(ghostIndex).configuration
     possibleActions = Actions.getPossibleActions(conf, state.data.layout.walls)
     reverse = Actions.reverseDirection(conf.direction)
     if Directions.STOP in possibleActions:
         possibleActions.remove(Directions.STOP)
     if reverse in possibleActions and len(possibleActions) > 1:
         possibleActions.remove(reverse)
     return possibleActions
コード例 #2
0
ファイル: play.py プロジェクト: MikeynJerry/ece517
 def getLegalActions(state):
     """
     Returns a list of possible actions.
     """
     return Actions.getPossibleActions(
         state.getPacmanState().configuration, state.data.layout.walls
     )
コード例 #3
0
ファイル: agents.py プロジェクト: shubham14101/Pacman-Bot
	def get_action(self, game_state):
		# Read variables from state
		ghost_state = game_state.get_ghost_state(self.agent_idx)
		legal_actions = game_state.get_legal_actions(self.agent_idx)
		pos = game_state.get_ghost_position(self.agent_idx)
		is_scared = ghost_state.scared_timer > 0

		action_vectors = [Actions.direction_to_vector(a) for a in legal_actions]
		new_positions = [(pos[0] + a[0], pos[1] + a[1]) for a in action_vectors]
		pacman_position = game_state.get_pacman_position()

		# Select best actions given the state
		distances_to_pacman = [manhattan_distance(pos, pacman_position) for pos in new_positions]
		if is_scared:
			best_score = max(distances_to_pacman)
			best_prob = self.prob_scared
		else:
			best_score = min(distances_to_pacman)
			best_prob = self.prob_attack
		best_actions = [action for action, distance in zip(legal_actions, distances_to_pacman) if
		                distance == best_score]

		# choose action
		selection_prob = [0] * len(legal_actions)
		for idx, action in enumerate(legal_actions):
			if action in best_actions:
				selection_prob[idx] += best_prob / len(best_actions)
			selection_prob[idx] += (1 - best_prob) / len(legal_actions)

		action = np.random.choice(legal_actions, p = selection_prob)
		return action
コード例 #4
0
ファイル: capture.py プロジェクト: lefake/mma_jdis2019
 def getLegalActions(state, agentIndex):
     """
 Returns a list of legal actions (which are both possible & allowed)
 """
     agentState = state.getAgentState(agentIndex)
     conf = agentState.configuration
     possibleActions = Actions.getPossibleActions(conf,
                                                  state.data.layout.walls)
     if AgentRules.canFreeze(agentState):
         possibleActions.append('FREEZE')
     return AgentRules.filterForAllowedActions(agentState, possibleActions)
コード例 #5
0
ファイル: play.py プロジェクト: MikeynJerry/ece517
    def applyAction(state, action, ghostIndex):

        legal = GhostRules.getLegalActions(state, ghostIndex)
        if action not in legal:
            raise Exception("Illegal ghost action " + str(action))

        ghostState = state.data.agentStates[ghostIndex]
        speed = GhostRules.GHOST_SPEED
        if ghostState.scaredTimer > 0:
            speed /= 2.0
        vector = Actions.directionToVector(action, speed)
        ghostState.configuration = ghostState.configuration.generateSuccessor(vector)
コード例 #6
0
 def getCostOfActions(self, actions):
     """
     Returns the cost of a particular sequence of actions.  If those actions
     include an illegal move, return 999999.  This is implemented for you.
     """
     if actions == None: return 999999
     x, y = self.startingPosition
     for action in actions:
         dx, dy = Actions.directionToVector(action)
         x, y = int(x + dx), int(y + dy)
         if self.walls[x][y]: return 999999
     return len(actions)
コード例 #7
0
ファイル: play.py プロジェクト: MikeynJerry/ece517
    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)
コード例 #8
0
ファイル: capture.py プロジェクト: lefake/mma_jdis2019
    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))