agents.manual_agent

 1import asyncio
 2from typing import List, Optional, Tuple
 3
 4from agents.base_agent import BaseOthelloAgent
 5
 6
 7class ManualOthelloAgent(BaseOthelloAgent):
 8    r"""
 9    A human-controlled agent using terminal inputs.
10
11    Allows a user to manually select an action $a \in A(s)$ via terminal.
12    """
13
14    async def deliberate(
15        self, board: List[List[int]], valid_actions: List[List[int]]
16    ) -> Optional[Tuple[int, int]]:
17        """
18        Prompts the user for a move via terminal input.
19
20        Args:
21            board: The current $8 \times 8$ game board.
22            valid_actions: A list of valid move coordinates $[x, y]$.
23
24        Returns:
25            The chosen move $(x, y)$ or None.
26        """
27        color = "Black" if self.player_id == 1 else "White"
28        print(f"\n--- YOUR TURN (Player {self.player_id} - {color}) ---")
29
30        if not valid_actions:
31            print("You have no valid flanking moves. Your turn is skipped.")
32            await asyncio.sleep(2)
33            return None
34
35        print(f"Valid moves [x, y]: {valid_actions}")
36
37        while True:
38            # Use to_thread to prevent the asyncio loop from dropping websocket pings
39            user_input = await asyncio.to_thread(
40                input, "Enter coordinate 'x,y' (0-7): "
41            )
42
43            try:
44                parts = user_input.strip().split(",")
45                if len(parts) != 2:
46                    raise ValueError
47
48                target = [int(parts[0]), int(parts[1])]
49
50                if target in valid_actions:
51                    return (target[0], target[1])
52                else:
53                    print(
54                        "Invalid move. You must outflank at least one opponent disc. Try again."
55                    )
56            except ValueError:
57                print("Invalid input format. Please use 'x,y' (e.g., 2,4).")
58
59
60if __name__ == "__main__":
61    agent = ManualOthelloAgent()
62    print("Starting Manual Othello Agent...")
63    asyncio.run(agent.run())
class ManualOthelloAgent(agents.base_agent.BaseOthelloAgent):
 8class ManualOthelloAgent(BaseOthelloAgent):
 9    r"""
10    A human-controlled agent using terminal inputs.
11
12    Allows a user to manually select an action $a \in A(s)$ via terminal.
13    """
14
15    async def deliberate(
16        self, board: List[List[int]], valid_actions: List[List[int]]
17    ) -> Optional[Tuple[int, int]]:
18        """
19        Prompts the user for a move via terminal input.
20
21        Args:
22            board: The current $8 \times 8$ game board.
23            valid_actions: A list of valid move coordinates $[x, y]$.
24
25        Returns:
26            The chosen move $(x, y)$ or None.
27        """
28        color = "Black" if self.player_id == 1 else "White"
29        print(f"\n--- YOUR TURN (Player {self.player_id} - {color}) ---")
30
31        if not valid_actions:
32            print("You have no valid flanking moves. Your turn is skipped.")
33            await asyncio.sleep(2)
34            return None
35
36        print(f"Valid moves [x, y]: {valid_actions}")
37
38        while True:
39            # Use to_thread to prevent the asyncio loop from dropping websocket pings
40            user_input = await asyncio.to_thread(
41                input, "Enter coordinate 'x,y' (0-7): "
42            )
43
44            try:
45                parts = user_input.strip().split(",")
46                if len(parts) != 2:
47                    raise ValueError
48
49                target = [int(parts[0]), int(parts[1])]
50
51                if target in valid_actions:
52                    return (target[0], target[1])
53                else:
54                    print(
55                        "Invalid move. You must outflank at least one opponent disc. Try again."
56                    )
57            except ValueError:
58                print("Invalid input format. Please use 'x,y' (e.g., 2,4).")

A human-controlled agent using terminal inputs.

Allows a user to manually select an action $a \in A(s)$ via terminal.

async def deliberate( self, board: List[List[int]], valid_actions: List[List[int]]) -> Optional[Tuple[int, int]]:
15    async def deliberate(
16        self, board: List[List[int]], valid_actions: List[List[int]]
17    ) -> Optional[Tuple[int, int]]:
18        """
19        Prompts the user for a move via terminal input.
20
21        Args:
22            board: The current $8 \times 8$ game board.
23            valid_actions: A list of valid move coordinates $[x, y]$.
24
25        Returns:
26            The chosen move $(x, y)$ or None.
27        """
28        color = "Black" if self.player_id == 1 else "White"
29        print(f"\n--- YOUR TURN (Player {self.player_id} - {color}) ---")
30
31        if not valid_actions:
32            print("You have no valid flanking moves. Your turn is skipped.")
33            await asyncio.sleep(2)
34            return None
35
36        print(f"Valid moves [x, y]: {valid_actions}")
37
38        while True:
39            # Use to_thread to prevent the asyncio loop from dropping websocket pings
40            user_input = await asyncio.to_thread(
41                input, "Enter coordinate 'x,y' (0-7): "
42            )
43
44            try:
45                parts = user_input.strip().split(",")
46                if len(parts) != 2:
47                    raise ValueError
48
49                target = [int(parts[0]), int(parts[1])]
50
51                if target in valid_actions:
52                    return (target[0], target[1])
53                else:
54                    print(
55                        "Invalid move. You must outflank at least one opponent disc. Try again."
56                    )
57            except ValueError:
58                print("Invalid input format. Please use 'x,y' (e.g., 2,4).")

Prompts the user for a move via terminal input.

Args: board: The current $8 imes 8$ game board. valid_actions: A list of valid move coordinates $[x, y]$.

Returns: The chosen move $(x, y)$ or None.