ttt.models package

Submodules

ttt.models.action module

class ttt.models.action.Action[source]

Bases: enum.Enum

Models all possible actions.

bottom_center = 7
bottom_left = 6
bottom_right = 8
middle_center = 4
middle_left = 3
middle_right = 5
top_center = 1
top_left = 0
top_right = 2

ttt.models.agent module

class ttt.models.agent.Agent[source]

Bases: object

Class modeling a player

grid

Representation of the board as an array of integers.

Type

numpy.ndarray

current_state

Current game state

Type

ttt.models.State

update_grid(grid: numpy.ndarray) → None[source]

Updates grid and current_state with the given value.

Parameters

grid (numpy.ndarray) – Representation of the board as an array of integers.

class ttt.models.agent.CPUAgent[source]

Bases: ttt.models.agent.Agent

Class modeling a CPU player.

states

List of states. Representation of the game as a Markov Decision Process.

Type

list

add_state(state: ttt.models.state.State) → None[source]

Adds the given state to the MDP.

Parameters

state (ttt.models.State) – State to be added.

deserialize(json_data: dict) → None[source]

Dumps specified json_data to the agent.

Parameters

json_data (dict) – Dictionary with the following format: {mdp: dict}

get_best_move() → ttt.models.action.Action[source]

Generates a new move using the previous knowledge.

Returns

move

Return type

ttt.models.Action

get_random_move() → ttt.models.action.Action[source]

Generates a random move.

Returns

move

Return type

ttt.models.Action

get_state(grid: numpy.ndarray) → ttt.models.state.State[source]

Gets the state with the given grid.

Parameters

grid (numpy.ndarray) – Representation of the board.

Returns

state – State containing the given grid.

Return type

ttt.models.State

has_state(state: ttt.models.state.State) → bool[source]

Checks if MDP has registered the given state :param state: State to be checked :type state: ttt.models.State

Returns

is_registered

Return type

bool

load(path: str) → None[source]

Loads a JSON file containing agent’s params.

Parameters

path (str) – Path of the JSON file.

save(path: str) → None[source]

Saves model’s params as a JSON file.

Parameters

path (str) – Where to save agent params.

serialize() → dict[source]

Serializes this Agent into a dict.

Returns

serializable_dict

Return type

dict

update_grid(grid: numpy.ndarray) → None[source]

Updates grid, current_state and mdp with the given value.

Parameters

grid (numpy.ndarray) – Representation of the board as an array of integers.

update_state(state: ttt.models.state.State) → None[source]

Updates the MDPs state with the given one.

Parameters

state (ttt.models.State) – State to update MDPs with.

class ttt.models.agent.HumanAgent[source]

Bases: ttt.models.agent.Agent

Class modeling a human player.

grid

Representation of the board as list of integers.

Type

list

get_next_move() → ttt.models.action.Action[source]

Gets the user move.

Returns

move

Return type

int

ttt.models.game module

class ttt.models.game.Game(player_1: ttt.models.agent.Agent, player_2: ttt.models.agent.Agent)[source]

Bases: object

Class modeling.

grid
Type

numpy.ndarray

player_1
Type

ttt.models.Agent

player_2
Type

ttt.models.Agent

result
Type

int

grid_is_full
Type

bool

game_sequence
Type

list

Parameters
  • player_1 (ttt.models.Agent) –

  • player_2 (ttt.models.Agent) –

apply_move(move: ttt.models.action.Action, player_id: int) → None[source]

Commits the move over the board.

Parameters
  • move (ttt.models.Action) – Move to be performed.

  • player_id (int) – Value to apply.

check_diagonal_win() → None[source]

Checks if there is 3 consecutives marks of the same player diagonally

check_horizontal_win() → None[source]

Checks if there is 3 consecutives marks of the same player horizontally.

check_if_grid_is_full() → None[source]

Checks if there is no more possible move.

check_vertical_win() → None[source]

Checks if there is 3 consecutives marks of the same player vertically.

check_victory() → None[source]

Checks if there is 3 consecutives marks of the same player.

print_board() → dict[source]

Displays the current grid.

ttt.models.reward module

class ttt.models.reward.Reward[source]

Bases: object

Models all possible rewards.

static defeat(i, lr)[source]

Computes the reward for a given state when the result was a defeat.

Parameters
  • i (int) – Position in which the state was reached

  • lr (float) – Learning rate

Returns

reward

Return type

float

static tie(i, lr)[source]

Computes the reward for a given state when the result was a tie.

Parameters
  • i (int) – Position in which the state was reached

  • lr (float) – Learning rate

Returns

reward

Return type

float

static win(i, lr)[source]

Computes the reward for a given state when the result was a win.

Parameters
  • i (int) – Position in which the state was reached

  • lr (float) – Learning rate

Returns

reward

Return type

float

ttt.models.state module

class ttt.models.state.State(grid: numpy.ndarray = None)[source]

Bases: object

Class modelling the states of the game

Parameters

grid (numpy.ndarray) – Representation of the board.

grid

Representation of the board.

Type

numpy.ndarray

next_states_values

The weight of the transition to a state.

Type

numpy.ndarray

next_states_transitions

The next move to move to the next state.

Type

numpy.ndarray

deserialize(json_data: dict) → None[source]

Dumps specified json_data to the state.

Parameters

json_data (dict) – Dictionary with the following format: {grid: list, next_state_values: list, next_state_transitions: list}

get_best_move() → ttt.models.action.Action[source]

Decides on the best available move based on the transition weights.

serialize() → dict[source]

Serializes this State into a dict.

Returns

serializable_dict

Return type

dict

update_transition_weight(transition: ttt.models.action.Action, increase: float) → None[source]

Updates the transition weights.

Parameters
  • transition (int) – Transition move.

  • increase (float) – Value to be added