agents.manual_agent

 1import asyncio
 2from typing import List, Optional, Tuple, Union
 3
 4from agents.base_agent import BaseBSAgent
 5
 6
 7class ManualBSAgent(BaseBSAgent):
 8    """
 9    A human-controlled agent that reads target coordinates from the terminal.
10    """
11
12    async def deliberate(
13        self,
14        my_ships: List[List[Union[int, str]]],
15        my_shots: List[List[int]],
16        valid_actions: List[List[int]],
17    ) -> Optional[Union[List[int], Tuple[int, int]]]:
18        r"""
19        Prompts the user for a coordinate via the terminal.
20
21        Args:
22            my_ships: 10x10 grid with your ship names.
23            my_shots: 10x10 grid with your shot history.
24            valid_actions: List of coordinates $[x, y]$ that haven't been targeted yet.
25
26        Returns:
27            The coordinate $[x, y]$ entered by the user.
28        """
29        print(f"\n--- YOUR TURN (Player {self.player_id}) ---")
30
31        while True:
32            # Prevent the asyncio loop from freezing while waiting for human input
33            user_input = await asyncio.to_thread(input, "Enter target coordinate 'x,y' (e.g., 3,4): ")
34
35            try:
36                # Parse the "x,y" string into a list of two integers
37                parts = user_input.strip().split(",")
38                if len(parts) != 2:
39                    raise ValueError
40
41                x, y = int(parts[0]), int(parts[1])
42                target = [x, y]
43
44                if target in valid_actions:
45                    return target
46                else:
47                    print("Invalid coordinate. Either out of bounds or already fired upon. Try again.")
48            except ValueError:
49                print("Invalid input format. Please use 'x,y' with numbers from 0 to 9.")
50
51
52if __name__ == "__main__":
53    agent = ManualBSAgent()
54    print("Starting Manual Battleship Agent...")
55    asyncio.run(agent.run())
class ManualBSAgent(agents.base_agent.BaseBSAgent):
 8class ManualBSAgent(BaseBSAgent):
 9    """
10    A human-controlled agent that reads target coordinates from the terminal.
11    """
12
13    async def deliberate(
14        self,
15        my_ships: List[List[Union[int, str]]],
16        my_shots: List[List[int]],
17        valid_actions: List[List[int]],
18    ) -> Optional[Union[List[int], Tuple[int, int]]]:
19        r"""
20        Prompts the user for a coordinate via the terminal.
21
22        Args:
23            my_ships: 10x10 grid with your ship names.
24            my_shots: 10x10 grid with your shot history.
25            valid_actions: List of coordinates $[x, y]$ that haven't been targeted yet.
26
27        Returns:
28            The coordinate $[x, y]$ entered by the user.
29        """
30        print(f"\n--- YOUR TURN (Player {self.player_id}) ---")
31
32        while True:
33            # Prevent the asyncio loop from freezing while waiting for human input
34            user_input = await asyncio.to_thread(input, "Enter target coordinate 'x,y' (e.g., 3,4): ")
35
36            try:
37                # Parse the "x,y" string into a list of two integers
38                parts = user_input.strip().split(",")
39                if len(parts) != 2:
40                    raise ValueError
41
42                x, y = int(parts[0]), int(parts[1])
43                target = [x, y]
44
45                if target in valid_actions:
46                    return target
47                else:
48                    print("Invalid coordinate. Either out of bounds or already fired upon. Try again.")
49            except ValueError:
50                print("Invalid input format. Please use 'x,y' with numbers from 0 to 9.")

A human-controlled agent that reads target coordinates from the terminal.

async def deliberate( self, my_ships: List[List[Union[str, int]]], my_shots: List[List[int]], valid_actions: List[List[int]]) -> Union[List[int], Tuple[int, int], NoneType]:
13    async def deliberate(
14        self,
15        my_ships: List[List[Union[int, str]]],
16        my_shots: List[List[int]],
17        valid_actions: List[List[int]],
18    ) -> Optional[Union[List[int], Tuple[int, int]]]:
19        r"""
20        Prompts the user for a coordinate via the terminal.
21
22        Args:
23            my_ships: 10x10 grid with your ship names.
24            my_shots: 10x10 grid with your shot history.
25            valid_actions: List of coordinates $[x, y]$ that haven't been targeted yet.
26
27        Returns:
28            The coordinate $[x, y]$ entered by the user.
29        """
30        print(f"\n--- YOUR TURN (Player {self.player_id}) ---")
31
32        while True:
33            # Prevent the asyncio loop from freezing while waiting for human input
34            user_input = await asyncio.to_thread(input, "Enter target coordinate 'x,y' (e.g., 3,4): ")
35
36            try:
37                # Parse the "x,y" string into a list of two integers
38                parts = user_input.strip().split(",")
39                if len(parts) != 2:
40                    raise ValueError
41
42                x, y = int(parts[0]), int(parts[1])
43                target = [x, y]
44
45                if target in valid_actions:
46                    return target
47                else:
48                    print("Invalid coordinate. Either out of bounds or already fired upon. Try again.")
49            except ValueError:
50                print("Invalid input format. Please use 'x,y' with numbers from 0 to 9.")

Prompts the user for a coordinate via the terminal.

Args: my_ships: 10x10 grid with your ship names. my_shots: 10x10 grid with your shot history. valid_actions: List of coordinates $[x, y]$ that haven't been targeted yet.

Returns: The coordinate $[x, y]$ entered by the user.