Xiangiqgame
AI engine for Xiangqi
Loading...
Searching...
No Matches
app.py
Go to the documentation of this file.
1"""
2Contains a single function, run(), that does everything needed to play a Game.
3"""
4from pathlib import Path
5from typing import Dict
6import colorama
7import xiangqi_bindings as bindings
8from xiangqipy.command_input import (
9 RunKwargsInterpreter,
10 XiangqiGameCommandLine,
11)
12from xiangqipy.game import Game
13from xiangqipy.game_output_generator import GameOutputGenerator
14from xiangqipy.game_summary import GameSummary
15from xiangqipy.handlers.signals import set_signal_handlers
16from xiangqipy.player_builder import RedAndBlackPlayersBuilder
17
18
19def run(custom_output_root: Path = None, **kwargs: Dict) -> GameSummary:
20 """
21 Collects command line args, instantiates everything needed for a Game, runs the Game,
22 and optionally saves GameSummary info.
23
24 @param custom_output_root: optional Path of output parent dir; if not specified,
25 defaults to ./data/game_summaries/<timestamp-based-dirname>
26 @param **kwargs: optional key value pairs that can supplement or override
27 command line args.
28 """
29 set_signal_handlers()
30 colorama.init()
31
32 command_line_kwargs = XiangqiGameCommandLine().get_args()
33 run_kwargs = {**command_line_kwargs, **kwargs}
34
35 run_kwargs_interpreter = RunKwargsInterpreter(run_kwargs=run_kwargs)
36 xiangqi_command = run_kwargs_interpreter.interpret_command()
37
38 game_board = bindings.GameBoard()
39 # game_board_new = bindings.GameBoardFactory().create()
41 xiangqi_command=xiangqi_command, game_board=game_board
42 ).build()
43 my_game = Game(players=players, game_board=game_board)
44
45 game_summary = my_game.play()
46
47 # Optionally saves GameSummary and plots under /data/<game-ID>
48 if xiangqi_command.save_summary:
49 output_generator = GameOutputGenerator(
50 game_summary=game_summary,
51 output_dir_suffix=xiangqi_command.output_dir_suffix,
52 custom_output_root=custom_output_root,
53 # game_collection_id=game_collection_id,
54 )
55 output_generator.generate_output()
56
57 return game_summary
58
59
60if __name__ == "__main__":
61 run()
Converts dictionary output by XiangqiGameCommandLine into a XiangqiGameCommand.
Collects info from command line args out outputs as a dictionary.
Runs a game between two Players.
Definition: game.py:18
Outputs GameSummary to .json file, and saves plots to a .png file.
Builds two Player objects for a GameBoard based on XiangqiGameCommand.
GameSummary run(Path custom_output_root=None, **Dict kwargs)
Collects command line args, instantiates everything needed for a Game, runs the Game,...
Definition: app.py:19
Contains classes for collecting command line input, and converting to a form that will be convenient ...
Definition: command_input.py:1
Contains the GameOutputGenerator class.
GameSummary class and its component classes.
Definition: game_summary.py:1
Contains Game class.
Definition: game.py:1
Handlers for signals received when Game is running in terminal UI.
Definition: signals.py:1
Classes for building Player objects.