Skip to main content

TTT-game

codecov Test and Publish

A simple tic tac toe game implementation

Usage

Installation

Testing builds:

python -m pip install -i https://test.pypi.org/simple/ ttt-game

Production builds:

python -m pip install ttt-game

ttt-game module exports main game class Game and Pl and Tr enums to simplify typing.

from ttt_game import Game, Tr, Pl

Game class

class Game:
  """
  Board indexes preview:

  [[0, 1, 2],

   [3, 4, 5],

   [6, 7, 8]]

  Board array itself:

  [Tr.E, Tr.E, Tr.E, Tr.E, Tr.E, Tr.E, Tr.E, Tr.E, Tr.E]
  """
  _board: List[Tr]

  def get_board(self) -> List[Tr]:
    """
    Returns copy of game board. Use it to display board in UI.
    """

  def check_move(self, pos: int) -> bool:
    """
    Checks if board cell empty
    """

  def check_filled(self) -> bool:
    """
    Checks if game board is filled
    """

  def check_win(self, pos: int) -> bool:
    """
    Checks if this move will make player a winner.
    """

  def insert(self, pos: int, who: Tr) -> None:
    """
    Sets game board's cell to specified value. Better use `move` method when possible
    """

  def move(self, pos: int, who: Pl) -> bool:
    """
    Sets game board cell to specified value when possible. It also returns true if player has won.
    """

  def get_free(self) -> Tuple[int]:
    """
    Returns indexes of free game board cells
    """

To use it you should initialize it like below:

game = Game()

To make move, below code is listed. result variable will contain True if Pl.X player won the game.

result = game.move(1, Tr.X)
if result:
  print("Congrats, X player won!")

You also can get game board array with get_board method. To visualize it you can customize code listed below.

board = game.get_board()

for i in range(9):
  print(board[i], end=" ")
  if i % 3 == 2:
    print("\n", end="")

"""
output: Tr.E Tr.X Tr.E
        Tr.E Tr.E Tr.E
        Tr.E Tr.E Tr.E
"""

Pl and Tr Enums

Pl has two members: X and O meaning X and O players.

class Pl(Enum):
  X = 1
  O = 2

Tr enum has all members of Pl plus E entry meaning empty cell.

class Tr(Enum):
  E = 0
  X = Pl.X
  O = Pl.O

Release files for ttt-game 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ttt-game 0.1.0
File Size Uploaded
ttt_game-0.1.0.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ttt-game 0.1.0
File Interpreter ABI Platform
ttt_game-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.8 kB

Release files / ttt_game-0.1.0.tar.gz

Download URL ttt_game-0.1.0.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
227754a5301baac301e9132393a23c32789fcf224a9635e00994a3ebfa9ce065
BLAKE2b-256 checksum
How to use checksums
9823c06db4f140bc24902eee94d9ca11200e05436ff1c517d2b0103d25c891f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

Release files / ttt_game-0.1.0-py3-none-any.whl

Download URL ttt_game-0.1.0-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
424e2487271182a4fb890ba549115399d8a8f5c0d772f66b96ad384aa385d333
BLAKE2b-256 checksum
How to use checksums
15bad8575a3a5cd73c363624252004a703400d614873ba7c1c540d8a189fd19b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page