Xiangiqgame
AI engine for Xiangqi
Loading...
Searching...
No Matches
game_summary.py
Go to the documentation of this file.
1"""
2GameSummary class and its component classes.
3"""
4
5from dataclasses import dataclass
6from typing import Dict, List
7
8import msgspec
9import pandas as pd
10import xiangqi_bindings as bindings
11
13from xiangqipy.enums import GameState
14from xiangqipy.player_summary import PlayerSummary
15
16
17@dataclass
19 """
20 A data container for holding one PlayerSummary for each player in a Game.
21
22 @param kRed PlayerSummary: summary data for red player
23 @param kBlk PlayerSummary: summary data for black player
24 """
25
26 kRed: PlayerSummary
27 kBlk: PlayerSummary
28
29
30class GameSummary(msgspec.Struct):
31 """
32 Holds summary info of a xiangqipy.game.Game; implements @ref msgspec.Struct for json IO.
33
34 Attributes are all either primitives or from core_data_class_mirrors to
35 minimize need for enc_hook's in msgspec IO.
36
37 @param game_id str: unique ID of the Game, typically based on timestamp when Game is instantiated.
38 @param game_state xiangqipy.enums.GameState: state of the Game.
39 @param whose_turn xianqi_bindings.PieceColor: color of Player to execute next move.
40 @param move_log (List[xiangqipy.core_dataclass_mirrors.ExecutedMove]): log of all executed moves.
41 @param player_summaries (PlayerSummaries): contains a xiangqipy.player_summary.PlayerSummary for
42 each xiangqipy.players.AIPlayer in the Game.
43 """
44
45 game_id: str
46 game_state: GameState
47 whose_turn: bindings.PieceColor
48 move_log: List[cdm.ExecutedMove]
49 player_summaries: PlayerSummaries
50
51 @property
52 def move_counts(self) -> int:
53 """
54 Number of moves executed in the Game.
55 """
56 return len(self.move_log)
57
58 @property
59 def move_numbers(self) -> Dict[bindings.PieceColor, List[int]]:
60 """
61 Dictionary with list of move ID numbers executed by each player;
62 normally Red player's list has odd integers, and Black's has even integers.
63 """
64 return {
65 bindings.PieceColor.kBlk: [
66 val for val in range(1, self.move_counts + 1) if (val % 2) == 0
67 ],
68 bindings.PieceColor.kRed: [
69 val for val in range(self.move_counts + 1) if (val % 2) == 1
70 ],
71 }
72
73 def get_player_summary(self, player: bindings.PieceColor) -> PlayerSummary:
74 """
75 Gets the xiangqipy.player_summary.PlayerSummary for a particular xiangqi_bindings.PieceColor.
76
77 @param player xiangqi_bindings.PieceColor of player that PlayerSummary is being retrieved for.
78 """
79 return self.player_summaries.__dict__[player.name]
80
81 @property
82 def basic_stats(self) -> pd.Series:
83 basic_game_stats = pd.Series(
84 [self.move_counts, self.game_state.name],
85 index=["move_counts", "game_state"],
86 )
87 for player in [bindings.PieceColor.kRed, bindings.PieceColor.kBlk]:
88 player_summary = self.get_player_summary(player=player)
89 if player_summary.has_search_summaries:
90 player_stats = player_summary.selection_stats
91 player_stats.index = [
92 f"{player.name}_{idx}"
93 for idx in player_stats.index
94 ]
95
96 basic_game_stats = pd.concat([basic_game_stats, player_stats])
97
98 return basic_game_stats
Enum indicating state of game: is either unfinished, or a particular player has won.
Definition: enums.py:8
Runs a game between two Players.
Definition: game.py:18
Holds summary info of a xiangqipy.game.Game; implements msgspec.Struct for json IO.
Definition: game_summary.py:30
Dict[bindings.PieceColor, List[int]] move_numbers(self)
Dictionary with list of move ID numbers executed by each player; normally Red player's list has odd i...
Definition: game_summary.py:59
int move_counts(self)
Number of moves executed in the Game.
Definition: game_summary.py:52
PlayerSummary get_player_summary(self, bindings.PieceColor player)
Gets the xiangqipy.player_summary.PlayerSummary for a particular xiangqi_bindings....
Definition: game_summary.py:73
A data container for holding one PlayerSummary for each player in a Game.
Definition: game_summary.py:18
Data container for data from one xiangqipy.game_interfaces.Player in a Game.
Proposed moves selected using an implementation of core MoveEvaluator.
Definition: players.py:122
Contains classes that mirror the structure of some core C++ classes, primarily to facilitate easy IO ...
Enums that are only used on the Python side of the app.
Definition: enums.py:1
Contains PlayerSummary class.