ttt package

Submodules

ttt.play module

ttt.play.play_game(game: ttt.models.game.Game, player_1_move: Callable, player_2_move: Callable, start: int, display_board: bool = True) → ttt.models.game.Game[source]

Runs 1 round of a game.

Parameters
  • game (ttt.models.Game) – Game to be played.

  • player_1_move (method) – Player’s method that computes the next move.

  • player_2_move (method) – Player’s method that computes the next move.

  • start (int) – Which player will start.

  • display_board (bool) – Whether to display the board after each move or not.

Returns

played_game

Return type

ttt.models.Game

ttt.play.play_game_cpu_vs_cpu(player_1: ttt.models.agent.CPUAgent, player_2: ttt.models.agent.CPUAgent, player_1_mode: str, player_2_mode: str, num_rounds: int, display_board: bool = True, display_text: bool = True) → ttt.models.game.Game[source]

Plays a game between two cpu players.

Parameters
  • player_1 (ttt.models.CPUAgent) –

  • player_2 (ttt.models.CPUAgent) –

  • player_1_mode (str) – Whether best or random.

  • player_2_mode (str) – Whether best or random.

  • num_rounds (int) – Number or rounds to be played.

  • display_board (bool) – Whether to display the board after each move.

  • display_text (bool) – Whether to display game information

Returns

played_game

Return type

ttt.models.Game

Raises

AssertionError – if num_rounds < 1:

ttt.play.play_game_player_vs_comp(cpu_player: ttt.models.agent.CPUAgent, cpu_player_mode: str) → ttt.models.game.Game[source]

Plays a game between a human player and a CPU agent.

Parameters
  • cpu_player (ttt.models.CPUAgent) –

  • cpu_player_mode (str) – Whether best or random.

Returns

played_game

Return type

ttt.models.Game

ttt.play.play_game_player_vs_player() → None[source]

Plays a game between 2 human players

ttt.rewarding module

ttt.rewarding.get_move(state_1: ttt.models.state.State, state_2: ttt.models.state.State) → ttt.models.action.Action[source]

Returns the move applied to go from state_1 to state_2.

Parameters
  • state_1 (ttt.models.State) – Grid representation.

  • state_2 (ttt.models.State) – Grid representation.

Returns

move – Move applied to go from state_1 to state_2.

Return type

ttt.models.Action

ttt.rewarding.rewarding(game: ttt.models.game.Game, player_number: int, lr: float) → ttt.models.agent.CPUAgent[source]

Reads the sequence of states the completed game has.

Parameters
  • game (ttt.models.Game) – Game that was played.

  • player_number (int) – Player to be rewarded.

  • lr (float) – Learning rate.

Returns

cpu_agent – Agent with its weights updated with games’ results.

Return type

ttt.models.CPUAgent

ttt.training module

ttt.training.train(output_path: str, lr: float = 0.1, exploration_iterations: int = 2500, exploitation_iterations: int = 1500, exploration_exploitation_iterations: int = 1000) → ttt.models.agent.CPUAgent[source]

Trains a new agent.

Parameters
  • output_path (str) – Where to save agent’s weights. E.g., weights.json.

  • lr (float) – Learning rate.

  • exploration_iterations (int) – Number of iterations during exploration phase.

  • exploitation_iterations (int) – Number of iterations during exploitation phase.

  • exploration_exploitation_iterations (int) – Number of iterations during exploration-exploitation phase.

Returns

cpu_agent

Return type

ttt.models.CPUAgent