File size: 1,338 Bytes
8c486a8
 
 
 
 
 
 
 
49d1c75
 
 
8c486a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49d1c75
8c486a8
49d1c75
 
 
 
8c486a8
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""Interactive human agent for manual play and debugging.

Prints observations to stdout and reads commands from stdin.
"""

from __future__ import annotations

import sys
from typing import Any, Literal

from open_range.agents.observation import format_observation


class HumanAgent:
    """Interactive agent that prompts a human for commands.

    Satisfies the :class:`RangeAgent` protocol.
    """

    def __init__(self, prompt: str = "Enter command > ") -> None:
        self.prompt = prompt
        self.role: str = "red"

    def reset(self, briefing: str, role: Literal["red", "blue"]) -> None:
        """Display role and briefing to the human player."""
        self.role = role
        print(f"\n{'=' * 60}", file=sys.stderr)
        print(f"Role: {role.upper()}", file=sys.stderr)
        print(f"{'=' * 60}", file=sys.stderr)
        print(f"Briefing:\n{briefing}", file=sys.stderr)
        print(f"{'=' * 60}\n", file=sys.stderr)

    def act(self, observation: Any) -> str:
        """Print the observation and read a command from stdin."""
        print(
            f"\n[{self.role.upper()}] Observation:\n{format_observation(observation)}\n",
            file=sys.stderr,
        )
        try:
            return input(self.prompt).strip()
        except (EOFError, KeyboardInterrupt):
            return "echo quit"