Skip to content

Instantly share code, notes, and snippets.

@jonatasleon
Created May 6, 2022 18:59
Show Gist options
  • Save jonatasleon/1afdfb4217baae4b3ba561154e6d4a6d to your computer and use it in GitHub Desktop.
Save jonatasleon/1afdfb4217baae4b3ba561154e6d4a6d to your computer and use it in GitHub Desktop.
Board sheet demonstration
from itertools import product
from typing import Iterable, Literal
class Board:
def __init__(self):
self.board = {}
def add_piece(self, x, y, piece, axis: Literal["x", "y"] = "x"):
"""Add a piece to the board.
:param x: the x coordinate
:param y: the y coordinate
:param piece: the piece to add
:param axis: the axis to add the piece to (default: "x")
x: add to the x axis
y: add to the y axis
"""
if not isinstance(piece, Iterable):
piece = f"{piece}"
target = x
if axis == "y":
target = y
for target, c in enumerate(piece, start=target):
if axis == "x":
self.board[(target, y)] = c
else:
self.board[(x, target)] = c
def get(self, key, default=None):
return self.board.get(key, default)
@property
def x(self):
return (x for x, _ in self.board)
@property
def y(self):
return (y for _, y in self.board)
def min_max(nums):
return min(nums), max(nums)
def print_board(board: Board):
minx, maxx = min_max(list(board.x))
miny, maxy = min_max(list(board.y))
for x, y in product(range(minx, maxx + 1), range(miny, maxy + 2)):
if y == miny:
print("{x:<{width}}".format(x=x, width=len(f"{maxx + 1}")), "| ", end="")
print(board.get((x, y), "."), end=" ")
if y == maxy + 1:
print()
board = Board()
board.add_piece(0, 1, "Jonatas Leon")
board.add_piece(0, 0, "Paty Patyka", axis="x")
print_board(board)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment