pacai.agents.userinput

 1import typing
 2
 3import pacai.core.action
 4import pacai.core.agent
 5import pacai.core.agentaction
 6import pacai.core.gamestate
 7
 8class UserInputAgent(pacai.core.agent.Agent):
 9    """
10    An agent that makes moves based on input from a user.
11    """
12
13    def __init__(self,
14            remember_last_action: bool = True,
15            **kwargs: typing.Any) -> None:
16        super().__init__(**kwargs)
17
18        self._remember_last_action: bool = remember_last_action
19        """
20        If no valid user action was provided, keep using the most recent valid action instead of just stopping.
21        """
22
23    def get_action_full(self,
24            state: pacai.core.gamestate.GameState,
25            user_inputs: list[pacai.core.action.Action],
26            ) -> pacai.core.agentaction.AgentAction:
27        legal_actions = state.get_legal_actions()
28
29        # Specifically note if we used an input from the user.
30        # If we did, then we can clear the input buffer.
31        # This allows users to input actions without needing to be frame perfect,
32        # e.g., a user can input a turn before the agent is in the intersection.
33        used_user_input = False
34
35        # If actions were provided, take the most recent one.
36        intended_action = None
37        if (len(user_inputs) > 0):
38            intended_action = user_inputs[-1]
39            used_user_input = True
40
41            # If the intended action is not legal, then ignore it.
42            if (intended_action not in legal_actions):
43                intended_action = None
44                used_user_input = False
45
46        # If we got no legal input from the user, then assume the last action.
47        if (self._remember_last_action and (intended_action is None)):
48            intended_action = state.get_last_agent_action()
49            used_user_input = False
50
51        # If the action is illegal, then just stop.
52        if (intended_action not in legal_actions):
53            intended_action = pacai.core.action.STOP
54            used_user_input = False
55
56        return pacai.core.agentaction.AgentAction(intended_action, clear_inputs = used_user_input)
class UserInputAgent(pacai.core.agent.Agent):
 9class UserInputAgent(pacai.core.agent.Agent):
10    """
11    An agent that makes moves based on input from a user.
12    """
13
14    def __init__(self,
15            remember_last_action: bool = True,
16            **kwargs: typing.Any) -> None:
17        super().__init__(**kwargs)
18
19        self._remember_last_action: bool = remember_last_action
20        """
21        If no valid user action was provided, keep using the most recent valid action instead of just stopping.
22        """
23
24    def get_action_full(self,
25            state: pacai.core.gamestate.GameState,
26            user_inputs: list[pacai.core.action.Action],
27            ) -> pacai.core.agentaction.AgentAction:
28        legal_actions = state.get_legal_actions()
29
30        # Specifically note if we used an input from the user.
31        # If we did, then we can clear the input buffer.
32        # This allows users to input actions without needing to be frame perfect,
33        # e.g., a user can input a turn before the agent is in the intersection.
34        used_user_input = False
35
36        # If actions were provided, take the most recent one.
37        intended_action = None
38        if (len(user_inputs) > 0):
39            intended_action = user_inputs[-1]
40            used_user_input = True
41
42            # If the intended action is not legal, then ignore it.
43            if (intended_action not in legal_actions):
44                intended_action = None
45                used_user_input = False
46
47        # If we got no legal input from the user, then assume the last action.
48        if (self._remember_last_action and (intended_action is None)):
49            intended_action = state.get_last_agent_action()
50            used_user_input = False
51
52        # If the action is illegal, then just stop.
53        if (intended_action not in legal_actions):
54            intended_action = pacai.core.action.STOP
55            used_user_input = False
56
57        return pacai.core.agentaction.AgentAction(intended_action, clear_inputs = used_user_input)

An agent that makes moves based on input from a user.

UserInputAgent(remember_last_action: bool = True, **kwargs: Any)
14    def __init__(self,
15            remember_last_action: bool = True,
16            **kwargs: typing.Any) -> None:
17        super().__init__(**kwargs)
18
19        self._remember_last_action: bool = remember_last_action
20        """
21        If no valid user action was provided, keep using the most recent valid action instead of just stopping.
22        """
def get_action_full( self, state: pacai.core.gamestate.GameState, user_inputs: list[pacai.core.action.Action]) -> pacai.core.agentaction.AgentAction:
24    def get_action_full(self,
25            state: pacai.core.gamestate.GameState,
26            user_inputs: list[pacai.core.action.Action],
27            ) -> pacai.core.agentaction.AgentAction:
28        legal_actions = state.get_legal_actions()
29
30        # Specifically note if we used an input from the user.
31        # If we did, then we can clear the input buffer.
32        # This allows users to input actions without needing to be frame perfect,
33        # e.g., a user can input a turn before the agent is in the intersection.
34        used_user_input = False
35
36        # If actions were provided, take the most recent one.
37        intended_action = None
38        if (len(user_inputs) > 0):
39            intended_action = user_inputs[-1]
40            used_user_input = True
41
42            # If the intended action is not legal, then ignore it.
43            if (intended_action not in legal_actions):
44                intended_action = None
45                used_user_input = False
46
47        # If we got no legal input from the user, then assume the last action.
48        if (self._remember_last_action and (intended_action is None)):
49            intended_action = state.get_last_agent_action()
50            used_user_input = False
51
52        # If the action is illegal, then just stop.
53        if (intended_action not in legal_actions):
54            intended_action = pacai.core.action.STOP
55            used_user_input = False
56
57        return pacai.core.agentaction.AgentAction(intended_action, clear_inputs = used_user_input)

Get an action for this agent given the current state of the game. Agents may keep internal state, but the given state should be considered the source of truth. Calls to this method may be subject to a timeout (enforced by the isolator).

By default, this method just calls get_action(). Agent classes should typically just implement get_action(), and only implement this if they need additional functionality.