Module pacai.agents.leftturn

Expand source code
from pacai.agents.base import BaseAgent
from pacai.core.directions import Directions

class LeftTurnAgent(BaseAgent):
    """
    An agent that turns left at every opportunity.
    Three lefts make a right, and two rights (six lefts) don't make a wrong.
    """

    def __init__(self, index, **kwargs):
        super().__init__(index, **kwargs)

    def getAction(self, state):
        legal = state.getLegalPacmanActions()
        current = state.getPacmanState().getDirection()
        if current == Directions.STOP:
            current = Directions.NORTH

        left = Directions.LEFT[current]
        if left in legal:
            return left

        if current in legal:
            return current

        if Directions.RIGHT[current] in legal:
            return Directions.RIGHT[current]

        if Directions.LEFT[left] in legal:
            return Directions.LEFT[left]

        return Directions.STOP

Classes

class LeftTurnAgent (index, **kwargs)

An agent that turns left at every opportunity. Three lefts make a right, and two rights (six lefts) don't make a wrong.

Expand source code
class LeftTurnAgent(BaseAgent):
    """
    An agent that turns left at every opportunity.
    Three lefts make a right, and two rights (six lefts) don't make a wrong.
    """

    def __init__(self, index, **kwargs):
        super().__init__(index, **kwargs)

    def getAction(self, state):
        legal = state.getLegalPacmanActions()
        current = state.getPacmanState().getDirection()
        if current == Directions.STOP:
            current = Directions.NORTH

        left = Directions.LEFT[current]
        if left in legal:
            return left

        if current in legal:
            return current

        if Directions.RIGHT[current] in legal:
            return Directions.RIGHT[current]

        if Directions.LEFT[left] in legal:
            return Directions.LEFT[left]

        return Directions.STOP

Ancestors

Static methods

def loadAgent(name, index, args={})

Inherited from: BaseAgent.loadAgent

Load an agent with the given class name. The name can be fully qualified or just the bare class name. If the bare name is given, the class should …

Methods

def final(self, state)

Inherited from: BaseAgent.final

Inform the agent about the result of a game.

def getAction(self, state)

Inherited from: BaseAgent.getAction

The BaseAgent will receive an AbstractGameState, and must return an action from Directions.

Expand source code
def getAction(self, state):
    legal = state.getLegalPacmanActions()
    current = state.getPacmanState().getDirection()
    if current == Directions.STOP:
        current = Directions.NORTH

    left = Directions.LEFT[current]
    if left in legal:
        return left

    if current in legal:
        return current

    if Directions.RIGHT[current] in legal:
        return Directions.RIGHT[current]

    if Directions.LEFT[left] in legal:
        return Directions.LEFT[left]

    return Directions.STOP
def observationFunction(self, state)

Inherited from: BaseAgent.observationFunction

Make an observation on the state of the game. Called once for each round of the game.

def registerInitialState(self, state)

Inherited from: BaseAgent.registerInitialState

Inspect the starting state.