Skip to main content

class Board that provides grid-based 2D rectangular board made by two-layered lists. Perfect for board game creation.

Project description

Board Class

A flexible, grid-based 2D rectangular board implementation using two-layered lists, perfect for board game creation and grid manipulations.

Installation

pip install boards.py
from boards import Board

Board creation:

# Board(width, height, item=None, background=None, joint="")

# Create a 3x3 board with default value 0, joint=" " (space between items)

board = Board(3, 3, item=0, background=0, joint=" ")
board[1, 1] = 'X'
board[0, 0] = 'O'
print(board)
# Output:
# O 0 0
# 0 X 0
# 0 0 0

# Same board but with joint="" (no space between items)
board.joint = ""
print(board)
# Output:
# O00
# 0X0
# 000

# Iterating over cells

board = Board(2, 2, item='.', joint=" ")
board[0,0] = 'A'
board[1,1] = 'B'
for row, col, value in board:
    print(f"({row},{col}): {value}")
# Output:
# (0,0): A
# (0,1): .
# (1,0): .
# (1,1): B

# Board transformations

board = Board(3, 3, item=0, joint=" ")
board[0,0] = 1
board[1,1] = 2
board[2,2] = 3
print("Original:")
print(board)
# Output:
# 1 0 0
# 0 2 0
# 0 0 3

board.rotate()
print("Rotated 90°:")
print(board)
# Output:
# 0 0 1
# 0 2 0
# 3 0 0

board.hflip()
print("Horizontally flipped:")
print(board)
# Output:
# 1 0 0
# 0 2 0
# 0 0 3

# Finding connecting regions

board = Board(5, 5, item='.', background='.', joint=" ")
board[1,1] = board[1,2] = board[2,1] = board[2,2] = 'X'
board[3,3] = board[3,4] = board[4,3] = 'O'
print("Board:")
print(board)
# Output:
# . . . . .
# . X X . .
# . X X . .
# . . . O O
# . . . O .

connected_region = board.connected(1, 1)
print(f"Connected cells to (1,1): {connected_region}")
# Output: Connected cells to (1,1): [(1, 1), (1, 2), (2, 1), (2, 2)]

# Scrolling effects

board = Board(3, 3, item=0, joint=" ")
board.checkerboard(1, 2)
print("Original:")
print(board)
# Output:
# 1 2 1
# 2 1 2
# 1 2 1

print("Scrolled right by 1:")
print(board.rscrolled(1))
# Output:
# 1 1 2
# 2 2 1
# 1 1 2

print("Scrolled down by 1:")
print(board.dscrolled(1))
# Output:
# 2 1 2
# 1 2 1
# 1 2 1

# Board comparison

board1 = Board(3, 3, item='A', joint=" ")
board2 = board1.copy()
board2[1,1] = 'B'
print("Board 1:")
print(board1)
# Output:
# A A A
# A A A
# A A A
print("Board 2:")
print(board2)
# Output:
# A A A
# A B A
# A A A

similarity = board1.similarity(board2)
print(f"Similarity: {similarity:.1f}%")
# Output: Similarity: 88.9%
print(f"Difference: {board1.difference(board2):.1f}%")
# Output: Difference: 11.1%

# Flood fill

board = Board(5, 5, item='.', background='.', joint=" ")
board[1,1] = board[1,2] = board[2,1] = board[2,2] = 'X'
print("Before flood fill:")
print(board)
# Output:
# . . . . .
# . X X . .
# . X X . .
# . . . . .
# . . . . .

board.floodfill(1, 1, 'O')
print("After flood fill at (1,1):")
print(board)
# Output:
# . . . . .
# . O O . .
# . O O . .
# . . . . .
# . . . . .

# Working with different joint strings

# With space separator
board1 = Board(3, 3, item='#', joint=" ")
print(board1)
# Output:
# # # #
# # # #
# # # #

# With no separator
board2 = Board(3, 3, item='#', joint="")
print(board2)
# Output:
# ###
# ###
# ###

# With custom separator
board3 = Board(3, 3, item='#', joint="|")
print(board3)
# Output:
# #|#|#
# #|#|#
# #|#|#

# Board extension

board1 = Board(2, 2, item=1, joint=" ")
board2 = Board(2, 2, item=2, joint=" ")
print("Board 1:")
print(board1)
# Output:
# 1 1
# 1 1
print("Board 2:")
print(board2)
# Output:
# 2 2
# 2 2

board1.extend_to_right(board2)
print("Board 1 extended to the right:")
print(board1)
# Output:
# 1 1 2 2
# 1 1 2 2

# Compression

board = Board(5, 5, item='.', background='.', joint=" ")
board[2,2] = 'X'
print("Original:")
print(board)
# Output:
# . . . . .
# . . . . .
# . . X . .
# . . . . .
# . . . . .

board.compress()
print("Compressed:")
print(board)
# Output:
# . . X . .
# . . . . .
# . . . . .

# Binary marking

board = Board(2, 3, item=0, joint=" ")
print("Empty board:")
print(board)
# Output:
# 0 0 0
# 0 0 0

board.binary_mark(13)  # 13 in binary is 1101, padded to 6 digits: 001101
print("Board filled with binary of 13:")
print(board)
# Output:
# 0 0 1
# 1 0 1

# Checkerboard pattern

board = Board(4, 4, item='', joint=" ")
board.checkerboard('X', 'O')
print(board)
# Output:
# X O X O
# O X O X
# X O X O
# O X O X

# Moving items

board = Board(3, 3, item='.', background='.', joint=" ")
board[0,0] = 'X'
print("Before move:")
print(board)
# Output:
# X . .
# . . .
# . . .

board.move(0, 0, 2, 2)
print("After moving from (0,0) to (2,2):")
print(board)
# Output:
# . . .
# . . .
# . . X

# Checking symmetry

board = Board(3, 3, item=0, joint=" ")
board.checkerboard(1, 2)
print("Board:")
print(board)
# Output:
# 1 2 1
# 2 1 2
# 1 2 1

print(f"Horizontally symmetric? {board.is_symmetric('horizontal')}")
# Output: Horizontally symmetric? True
print(f"Vertically symmetric? {board.is_symmetric('vertical')}")
# Output: Vertically symmetric? True
print(f"Both? {board.is_symmetric('both')}")
# Output: Both? True

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

boards_py-0.1.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

boards_py-0.1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file boards_py-0.1.0.tar.gz.

File metadata

  • Download URL: boards_py-0.1.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for boards_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3736cf398241ac76acfea928a47eeb84e87c7f3ea6c5d25e6f28bb3c79cdb9f3
MD5 dfabdb4ce893e759e71fc89ca6b8e19b
BLAKE2b-256 60e61eb11a319d71b4f1be5d4b9d9716eb997debfa61759552b604df5bd8d738

See more details on using hashes here.

File details

Details for the file boards_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: boards_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for boards_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 faeb2347e0efebf038354fe3b7783709871e4e86c6c11731586261496c25fa31
MD5 6b8243b39d4cf22cbaab7b3bf1d5be3b
BLAKE2b-256 2573c90c177dea8f7a81d08a6535a48130dfac84914ba4d1238aa7efd48f2ed3

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