""" This module offers a small place to start building game clients from. You can use this for: - Learning how the system works. - Debugging a game server. - Debugging a game that behaves weirdly. This module lets you host a game, let online servers participate in it, and then analyze the outcome. You can build several things with this: 1. You can build an AI trainer that trains on existing players. 2. You can build an ELO evaluator that compares the performance of various strategies. """ from __future__ import annotations import json from pyclient import PyClient from pyclient.games.tic_tac_toe import TicTacToe from typing import Any def main() -> int: """ Start a client, then use it to analyze one or more matches. :return: Exit code :rtype: int """ c = PyClient([ "https://bmt001.noordstar.me", "https://bmt001.noordstar.me", ], debug=False) out = c.play_game("tic-tac-toe", TicTacToe) inspect_game(out) return 0 def inspect_game(game : list[tuple[int, dict[str, Any], Any]]) -> None: """ Print a diagnostic of a played game to the terminal. :param game: The results of a played game. :type game: list[typle[int, dict[str, Any], Any]] """ max_width = 40 hbar = max_width * '%' def title(s : str) -> None: """ Show a title nice and clean in the middle :param s: The title to display :type s: str """ w = (max_width - len(s)) // 2 print(hbar) print((w * ' ') + s.upper() + (w * ' ')) print(hbar) # Show all moves made throughout the game title("Turns taken") for player, action, _ in game: print(f"Player {player} : " + json.dumps(action)[:max_width-12]) # Show all remaining variables in the finishing state of the game title("Final state") final_state = game[-1][2] for k, v in final_state.to_dict().items(): print(f"{k} => {json.dumps(v)}") # Some final (usually the most relevant) statistics title("Result") print(f"Total turns taken: {len(game)}") print("Winner: " + ("0 (tie)" if final_state.winner() == 0 else f"player {final_state.winner()}")) if __name__ == "__main__": raise SystemExit(main())