Skip to main content

A python library for the game of Ultimate Tic-Tac-Toe

Project description

UTTT Engine

A python library for the game of Ultimate Tic-Tac-Toe

Documentation

Functions:

  • print_board
  • print_block
  • has_won
  • is_tie
  • find_won
  • claimed_spaces
  • is_game_over
  • get_move
  • change_turn
  • generate_moves
  • make_move

Definitions and important info

Game board refers to the entire game board with 9 groups and 9 spaces inside
Group refers to one group inside a game board, can also be referred to as a board
Old_move is set right after setting move, and is used to determine the group in which the next player can play in
The following examples assume from utt-engine import *

print_board(grid)

This function prints a given game board

Example usage:

grid = [["-" for j in range(9)] for i in range(9)]

print_board(grid)

print_block(row_start, col_start, grid)

This function prints one of the 9 groups. The only real use case is for the print_board function.

Example usage:

grid = [["-" for j in range(9)] for i in range(9)]

for i in range(0, 3 * 3, 3):
  for j in range(0, 3 * 3, 3):
    print_block(i, j, grid)
    print("-" * (3 * 6 + 3))

has_won(board)

This function returns True if either player has won (not tied) a given group, or false if not.

Example usage:

if has_won(grid[board]):
  print(f"{turn} has claimed group {board + 1}")
  for x in range(9):
    grid[board][x] = turn

is_tie(board)

This function returns True if either player has tied a given group, or false if not.

Example usage:

if is_tie(claimed_spaces(grid)):
  print("Game is a tie")

find_won(board)

This function returns the winner of a group. X is returned if X has won the given group, O if O has won, Tie if the group was tied, or - if none of the above are true.

Example usage:

if find_won(grid[board]) == "Tie":
  print("Group has tied")

claimed_spaces(board)

This function returns a list of all the groups that have been claimed given a game board.

Example usage:

if has_won(claimed_spaces(grid)):
  print(f"{turn} has won")
  game = False

is_game_over

This function returns True if the game is over (Tied or won), and False if not given a game board.

Example usage:

if is_game_over(grid):
  print("Game is over")

get_move(board, old_move)

This function gets a move from a human player via input given the game board and the previous move. If this move is invalid, "Invalid move" is printed, and nothing is returned until a valid move is played. If the move is valid, that move is returned.

Example usage:

move = get_move(grid, old_move)
print(f"{move} has been played")

change_turn(turn)

This function returns X if given O, and O if given X.

Example usage:

turn = "X"
print(f"The current turn is {turn}")
turn = change_turn()
print(f"The current turn is now {turn}")

generate_moves(board, old_move)

This function returns a list of every valid move given a game board and the previous move.

Example usage:

grid = make_move(grid, random.choice(generate_moves(grid, old_move)), turn)

make_move(board, move, player)

This function returns a game board after playing a move.

Example usage:

print_board(grid)
make_move(grid, "11", "X")
print_board(grid)

Example Game

This is an example of how to make a game of Ultimate Tic-Tac-Toe using UTT Engine. This is the code that is executed when the library is ran.

while game:
    move = get_move(grid, old_move)
    old_move = move
    grid = make_move(grid, move, turn)
    claimed_spaces_var = claimed_spaces(grid)
    board = int(move[1])-1
    if has_won(grid[board]):
        print(f"{turn} has claimed group {board + 1}")
        for x in range(9):
            grid[board][x] = turn
    if has_won(claimed_spaces_var):
        print(f"{turn} has won")
        game = False
    if is_tie(claimed_spaces_var):
        print("Game is a tie")
        game = False
    print_board(grid)
    first_move = False
    turn = change_turn(turn)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uttt_engine-0.0.8.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uttt_engine-0.0.8-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file uttt_engine-0.0.8.tar.gz.

File metadata

  • Download URL: uttt_engine-0.0.8.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.11

File hashes

Hashes for uttt_engine-0.0.8.tar.gz
Algorithm Hash digest
SHA256 dfcb90b8103dc4dcbc51ad764563833b4e6ca15ad9a421860898065b2523fc38
MD5 a610d9a8d3e1b88e7f3a086387a44e9b
BLAKE2b-256 a76cd20d3d6140fdbf23af10b4acac174c5255b9f33c86848e7ded246b5f89dd

See more details on using hashes here.

File details

Details for the file uttt_engine-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: uttt_engine-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.11

File hashes

Hashes for uttt_engine-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 c016f0ca33bb2900692c61c4f5d96cc9a31ff24f58f793fbdc2bbb21e9dd976f
MD5 a6ac7f8584673266f7556ff49e4794e7
BLAKE2b-256 d458c406d3dbd645c94e76d9c5642a096ce780bcbb90c9f7d996a76df922af14

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page