toetactic package

Submodules

toetactic.board module

Board model and move parsing for toetactic.

class toetactic.board.Board(dimension: int = 3)[source]

Bases: object

Represent an N x N Tic Tac Toe board.

Variables:
  • dimension (int) – Number of rows/columns on the board.

  • cells (list[list[str]]) – dimension x dimension grid of cell marks, where each cell holds " " (empty), "X", or "O".

available_moves() → list[tuple[int, int]][source]

Return all currently available board coordinates.

Returns:

List of zero-based (row, col) tuples for every empty cell, in row-major order.

has_winner(mark: str) → bool[source]

Return True when a mark completes a row, column, or diagonal.

Parameters:

mark – Single-character mark to check for, e.g. "X" or "O".

Returns:

True if every cell in at least one row, column, or diagonal holds mark, False otherwise.

is_cell_empty(row: int, col: int) → bool[source]

Return True if a board cell is empty.

Parameters:
  • row – Zero-based row index.

  • col – Zero-based column index.

Returns:

True if the cell holds no mark, False otherwise.

is_full() → bool[source]

Return True when no moves are left.

Returns:

True if every cell on the board is occupied, False otherwise.

move_to_label(row: int, col: int) → str[source]

Convert board coordinates to a label like ‘A1’.

Parameters:
  • row – Zero-based row index.

  • col – Zero-based column index.

Returns:

Chess-like move label, e.g. "A1".

parse_move(move: str) → tuple[int, int][source]

Parse a move like ‘A1’ into (row_index, col_index).

Parameters:

move – Chess-like move label, e.g. "A1" or "c3". Leading/trailing whitespace and letter case are ignored.

Returns:

Zero-based (row, col) coordinates for the move.

Raises:

ValueError – If move is not in the <letter><number> format, or if it refers to a cell outside the board.

place_mark(row: int, col: int, mark: str) → None[source]

Place a mark in a cell.

Parameters:
  • row – Zero-based row index.

  • col – Zero-based column index.

  • mark – Single-character mark to place, e.g. "X" or "O".

Raises:

ValueError – If the target cell is not empty.

render() → str[source]

Render the board as a grid with chess-like coordinates.

Returns:

Multi-line string showing column numbers along the top, row letters down the left side, and +/-/| grid lines around each cell.

toetactic.game module

Game session orchestration for toetactic CLI.

toetactic.game.BANNER = '\n _____         _____         _____ _\n|_   _|__   __|_   _|_ _  __|_   _(_) ___\n  | |/ _ \\ / _ \\| |/ _` |/ __|| | | |/ __|\n  | | (_) |  __/| | (_| | (__ | | | | (__\n  |_|\\___/ \\___||_|\\__,_|\\___||_| |_|\\___|\n'

ASCII art title shown when the game is loaded, before setup prompts.

class toetactic.game.Game[source]

Bases: object

Represent a single CLI Tic Tac Toe game session.

Variables:
run() → None[source]

Run the game loop until win or draw.

Shows the BANNER and calls setup() first, then alternates turns between the human and the opponent, redrawing the board after every move, until a player wins or the board fills up.

setup() → None[source]

Prompt for board size, player name, and opponent selection.

Populates board, human, and opponent from the collected input, replacing the defaults set in __init__().

class toetactic.game.HumanPlayer(name: str, mark: str)[source]

Bases: Player

Represent the human player.

choose_move(board: Board, opponent_mark: str) → tuple[int, int][source]

Human move is provided by prompt in Game; this method is not used.

Parameters:
  • board – Current board state. Unused.

  • opponent_mark – Mark used by the opposing player. Unused.

Raises:

NotImplementedError – Always. Human moves are collected via Game._prompt_human_move() instead.

toetactic.game.OPPONENTS: dict[str, type[Player]] = {'dikembe': <class 'toetactic.player.Dikembe'>, 'eleanor': <class 'toetactic.player.Eleanor'>, 'godzilla': <class 'toetactic.player.Godzilla'>, 'noober': <class 'toetactic.player.Noober'>}

Map of opponent selection keywords (as entered at the setup prompt) to the Player subclass that implements them.

toetactic.logger module

toetactic.player module

Players and AI strategies for toetactic.

class toetactic.player.Dikembe(name: str, mark: str)[source]

Bases: Player

Defensive opponent that prioritizes preventing player wins.

choose_move(board: Board, opponent_mark: str) → tuple[int, int][source]

Choose a move by blocking first, then choosing safe central options.

Parameters:
  • board – Current board state.

  • opponent_mark – Mark used by the opposing player.

Returns:

Zero-based (row, col) coordinates of the chosen move: a block of the opponent’s immediate winning line if one exists, otherwise the center, otherwise a corner, otherwise the first available cell.

class toetactic.player.Eleanor(name: str, mark: str)[source]

Bases: Player

Opponent that builds rows and columns, avoiding diagonals if possible.

choose_move(board: Board, opponent_mark: str) → tuple[int, int][source]

Choose a move by favouring row/column lines over diagonals.

Restricts itself to non-diagonal cells whenever any remain: among those, it takes an immediate win, then blocks an immediate opponent win, then advances whichever open row or column line it has already made the most progress on. Diagonal cells are only used once no non-diagonal cell is available.

Parameters:
  • board – Current board state.

  • opponent_mark – Mark used by the opposing player.

Returns:

Zero-based (row, col) coordinates of the chosen move.

class toetactic.player.Godzilla(name: str, mark: str)[source]

Bases: Player

Attacking opponent that seeks immediate wins each turn.

choose_move(board: Board, opponent_mark: str) → tuple[int, int][source]

Choose a move by winning first, then taking strong positional moves.

Parameters:
  • board – Current board state.

  • opponent_mark – Mark used by the opposing player.

Returns:

Zero-based (row, col) coordinates of the chosen move: an immediate winning move if one exists, otherwise the center, otherwise a corner, otherwise the first available cell.

class toetactic.player.Noober(name: str, mark: str)[source]

Bases: Player

Opponent that picks any legal move at random, hoping for luck.

choose_move(board: Board, opponent_mark: str) → tuple[int, int][source]

Choose a uniformly random move among all legal moves.

Parameters:
  • board – Current board state.

  • opponent_mark – Mark used by the opposing player. Unused, since Noober’s choice does not depend on board strategy.

Returns:

Zero-based (row, col) coordinates of a randomly chosen, currently empty cell.

class toetactic.player.Player(name: str, mark: str)[source]

Bases: ABC

Represent a Tic Tac Toe player.

Variables:
  • name (str) – Display name shown in prompts and status messages.

  • mark (str) – Single-character mark the player places on the board, e.g. "X" or "O".

abstract choose_move(board: Board, opponent_mark: str) → tuple[int, int][source]

Return board coordinates for the next move.

Parameters:
  • board – Current board state.

  • opponent_mark – Mark used by the opposing player.

Returns:

Zero-based (row, col) coordinates of the chosen move.

Module contents

CLI entrypoint for toetactic.

toetactic.play_game() → None[source]

Start an interactive Tic Tac Toe game session.

Constructs a new Game and runs it to completion, prompting for setup, taking turns, and reporting the outcome on the terminal.