Files
python-battleships/print_helper.py
2024-05-17 16:46:36 +02:00

46 lines
1.2 KiB
Python

from constants import BOARD_WIDTH, BOARD_HEIGHT, ROW_NUMBERS, COLUMN_LETTERS
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
characterPrefixes: list[str] = [bcolors.OKBLUE,
bcolors.BOLD, bcolors.FAIL, bcolors.OKBLUE]
characters: list[str] = ['0', '#', 'X', '-']
def getShipCharacter(ship: int):
return characters[ship]
def printBoard(board: list[int], hideShips: bool = False):
i = 0
print(" ", end="")
for n in range(BOARD_WIDTH):
print(f"{bcolors.BOLD}{COLUMN_LETTERS[n]}{bcolors.ENDC} ", end="")
print()
for x in range(BOARD_WIDTH):
print(f"{bcolors.BOLD}{ROW_NUMBERS[x]}{bcolors.ENDC} ", end="")
for y in range(BOARD_HEIGHT):
character = getShipCharacter(board[i])
prefix = characterPrefixes[board[i]]
if hideShips and board[i] == 1:
character = getShipCharacter(0)
prefix = characterPrefixes[0]
print(f"{prefix}{character}{bcolors.ENDC} ", end="")
i += 1
print()