pacai.pacman.bin

The main executable for running a game of Pac-Man.

 1"""
 2The main executable for running a game of Pac-Man.
 3"""
 4
 5import argparse
 6import typing
 7
 8import pacai.core.agentinfo
 9import pacai.core.board
10import pacai.pacman.game
11import pacai.pacman.gamestate
12import pacai.util.bin
13import pacai.util.alias
14
15DEFAULT_BOARD: str = 'classic-medium'
16DEFAULT_SPRITE_SHEET: str = 'pacman'
17
18def set_cli_args(parser: argparse.ArgumentParser, **kwargs: typing.Any) -> argparse.ArgumentParser:
19    """
20    Set Pac-Man-specific CLI arguments.
21    This is a sibling to init_from_args(), as the arguments set here can be interpreted there.
22    """
23
24    parser.add_argument('--pacman', dest = 'pacman', metavar = 'AGENT_TYPE',
25            action = 'store', type = str, default = pacai.util.alias.AGENT_USER_INPUT.short,
26            help = ('Select the agent type that PacMan will use (default: %(default)s).'
27                    + f' Builtin agents: {pacai.util.alias.AGENT_SHORT_NAMES}.'))
28
29    parser.add_argument('--ghosts', dest = 'ghosts', metavar = 'AGENT_TYPE',
30            action = 'store', type = str, default = pacai.util.alias.AGENT_RANDOM.short,
31            help = ('Select the agent type that all ghosts will use (default: %(default)s).'
32                    + f' Builtin agents: {pacai.util.alias.AGENT_SHORT_NAMES}.'))
33
34    parser.add_argument('--num-ghosts', dest = 'num_ghosts',
35            action = 'store', type = int, default = -1,
36            help = ('The maximum number of ghosts on the board (default: %(default)s).'
37                    + ' Ghosts with the highest agent index will be removed first.'
38                    + ' Board positions that normally spawn the removed agents/ghosts will now be empty.'))
39
40    return parser
41
42def init_from_args(args: argparse.Namespace) -> tuple[dict[int, pacai.core.agentinfo.AgentInfo], list[int], dict[str, typing.Any]]:
43    """
44    Setup agents based on Pac-Man rules.
45    """
46
47    base_agent_infos: dict[int, pacai.core.agentinfo.AgentInfo] = {}
48
49    # Create base arguments for all possible agents.
50    for i in range(pacai.core.board.MAX_AGENTS):
51        if (i == 0):
52            base_agent_infos[i] = pacai.core.agentinfo.AgentInfo(name = args.pacman)
53        else:
54            base_agent_infos[i] = pacai.core.agentinfo.AgentInfo(name = args.ghosts)
55
56    remove_agent_indexes = []
57
58    if (args.num_ghosts >= 0):
59        for i in range(1 + args.num_ghosts, pacai.core.board.MAX_AGENTS):
60            remove_agent_indexes.append(i)
61
62    return base_agent_infos, remove_agent_indexes, {}
63
64def get_additional_ui_options(args: argparse.Namespace) -> dict[str, typing.Any]:
65    """ Get additional options for the UI. """
66
67    return {
68        'sprite_sheet_path': DEFAULT_SPRITE_SHEET,
69    }
70
71def main(argv: list[str] | None = None,
72        ) -> tuple[list[pacai.core.game.GameResult], list[pacai.core.game.GameResult]]:
73    """
74    Invoke a game of Pac-Man.
75
76    Will return the results of any training games followed by the results of any non-training games.
77    """
78
79    return pacai.util.bin.run_main(
80        description = "Play a game of Pac-Man.",
81        default_board = DEFAULT_BOARD,
82        game_class = pacai.pacman.game.Game,
83        get_additional_ui_options = get_additional_ui_options,
84        custom_set_cli_args = set_cli_args,
85        custom_init_from_args = init_from_args,
86        winning_agent_indexes = {pacai.pacman.gamestate.PACMAN_AGENT_INDEX},
87        argv = argv,
88    )
89
90if (__name__ == '__main__'):
91    main()
DEFAULT_BOARD: str = 'classic-medium'
DEFAULT_SPRITE_SHEET: str = 'pacman'
def set_cli_args( parser: argparse.ArgumentParser, **kwargs: Any) -> argparse.ArgumentParser:
19def set_cli_args(parser: argparse.ArgumentParser, **kwargs: typing.Any) -> argparse.ArgumentParser:
20    """
21    Set Pac-Man-specific CLI arguments.
22    This is a sibling to init_from_args(), as the arguments set here can be interpreted there.
23    """
24
25    parser.add_argument('--pacman', dest = 'pacman', metavar = 'AGENT_TYPE',
26            action = 'store', type = str, default = pacai.util.alias.AGENT_USER_INPUT.short,
27            help = ('Select the agent type that PacMan will use (default: %(default)s).'
28                    + f' Builtin agents: {pacai.util.alias.AGENT_SHORT_NAMES}.'))
29
30    parser.add_argument('--ghosts', dest = 'ghosts', metavar = 'AGENT_TYPE',
31            action = 'store', type = str, default = pacai.util.alias.AGENT_RANDOM.short,
32            help = ('Select the agent type that all ghosts will use (default: %(default)s).'
33                    + f' Builtin agents: {pacai.util.alias.AGENT_SHORT_NAMES}.'))
34
35    parser.add_argument('--num-ghosts', dest = 'num_ghosts',
36            action = 'store', type = int, default = -1,
37            help = ('The maximum number of ghosts on the board (default: %(default)s).'
38                    + ' Ghosts with the highest agent index will be removed first.'
39                    + ' Board positions that normally spawn the removed agents/ghosts will now be empty.'))
40
41    return parser

Set Pac-Man-specific CLI arguments. This is a sibling to init_from_args(), as the arguments set here can be interpreted there.

def init_from_args( args: argparse.Namespace) -> tuple[dict[int, pacai.core.agentinfo.AgentInfo], list[int], dict[str, typing.Any]]:
43def init_from_args(args: argparse.Namespace) -> tuple[dict[int, pacai.core.agentinfo.AgentInfo], list[int], dict[str, typing.Any]]:
44    """
45    Setup agents based on Pac-Man rules.
46    """
47
48    base_agent_infos: dict[int, pacai.core.agentinfo.AgentInfo] = {}
49
50    # Create base arguments for all possible agents.
51    for i in range(pacai.core.board.MAX_AGENTS):
52        if (i == 0):
53            base_agent_infos[i] = pacai.core.agentinfo.AgentInfo(name = args.pacman)
54        else:
55            base_agent_infos[i] = pacai.core.agentinfo.AgentInfo(name = args.ghosts)
56
57    remove_agent_indexes = []
58
59    if (args.num_ghosts >= 0):
60        for i in range(1 + args.num_ghosts, pacai.core.board.MAX_AGENTS):
61            remove_agent_indexes.append(i)
62
63    return base_agent_infos, remove_agent_indexes, {}

Setup agents based on Pac-Man rules.

def get_additional_ui_options(args: argparse.Namespace) -> dict[str, typing.Any]:
65def get_additional_ui_options(args: argparse.Namespace) -> dict[str, typing.Any]:
66    """ Get additional options for the UI. """
67
68    return {
69        'sprite_sheet_path': DEFAULT_SPRITE_SHEET,
70    }

Get additional options for the UI.

def main( argv: list[str] | None = None) -> tuple[list[pacai.core.game.GameResult], list[pacai.core.game.GameResult]]:
72def main(argv: list[str] | None = None,
73        ) -> tuple[list[pacai.core.game.GameResult], list[pacai.core.game.GameResult]]:
74    """
75    Invoke a game of Pac-Man.
76
77    Will return the results of any training games followed by the results of any non-training games.
78    """
79
80    return pacai.util.bin.run_main(
81        description = "Play a game of Pac-Man.",
82        default_board = DEFAULT_BOARD,
83        game_class = pacai.pacman.game.Game,
84        get_additional_ui_options = get_additional_ui_options,
85        custom_set_cli_args = set_cli_args,
86        custom_init_from_args = init_from_args,
87        winning_agent_indexes = {pacai.pacman.gamestate.PACMAN_AGENT_INDEX},
88        argv = argv,
89    )

Invoke a game of Pac-Man.

Will return the results of any training games followed by the results of any non-training games.