TDT4109/Exercise 10/chess/board.py

132 lines
4.0 KiB
Python

from os import system
from piece import Piece
class Board:
def __init__(self):
self.boardArray = [
# [],
[Piece('p', 'black') for _ in range(8)],
[Piece('p', 'black') for _ in range(8)],
*[[None for _ in range(8)] for _ in range(4)],
[Piece('p', 'white') for _ in range(8)],
[Piece('p', 'white') for _ in range(8)],
# [],
]
def draw(
self,
config={
'highlightedContent': [],
'highlightEscapeCodes': ('\033[32;5;7m', '\033[0m'),
'highlightedBoxes': [],
'center': False
}
) -> str:
"""Returns a string representing the board
config options:
highlightedContent: [(x,y)] - Pieces to color
highlightEscapeCodes: (str, str) - Terminal escape codes to color highlightedContent with
highlightedBoxes: [(x,y)] - Boxes to make bold
center: Bool - Whether or not to indent the board into the center
"""
def fillConfigDefaultValue(key, defaultValue):
if key not in config:
config[key] = defaultValue
fillConfigDefaultValue('highlightedContent', [])
fillConfigDefaultValue('highlightedBoxes', [])
fillConfigDefaultValue('highlightEscapeCodes', ('\033[32;5;7m', '\033[0m'))
fillConfigDefaultValue('center', False)
# Draw general outline
stringArray = [list('' + '───┼' * 8)] + [[None] for _ in range(8 * 2)]
for y, row in enumerate(self.boardArray):
for x, column in enumerate(row):
stringArray[2 * y + 1][4 * x] = ''
stringArray[2 * y + 2][4 * x] = ''
stringArray[2 * y + 1] += list(
' {}'.format(str(self.boardArray[y][x]) if self.boardArray[y][x] != None else ' '))
stringArray[2 * y + 2] += list('───┼')
# Overwrite corners
stringArray[0][0] = ''
stringArray[0][-1] = ''
stringArray[-1][0] = ''
stringArray[-1][-1] = ''
# Overwrite T-junctions
for i in range(int(len(stringArray[0]) / 4) - 1):
stringArray[0][i * 4 + 4] = ''
stringArray[-1][i * 4 + 4] = ''
for i in range(int(len(stringArray) / 2) - 1):
stringArray[i * 2 + 2][0] = ''
stringArray[i * 2 + 2][-1] = ''
def highlightContent(x, y):
"""highlight contents of a piece with xterm-256colors modifiers"""
stringArray[y * 2 +
1][x * 4 +
1] = config['highlightEscapeCodes'][0] + stringArray[y * 2 + 1][x * 4 + 1]
stringArray[y * 2 + 1][x * 4 + 3] += config['highlightEscapeCodes'][1]
def highlightBox(x, y):
"""Make box around a piece bold"""
characterMap = {
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
'': '',
}
pointsToChange = \
[(x * 4 + 0, y * 2 + i) for i in range(3)] + \
[(x * 4 + 4, y * 2 + i) for i in range(3)] + \
[(x * 4 + i, y * 2 + 0) for i in range(1,4)] + \
[(x * 4 + i, y * 2 + 2) for i in range(1,4)]
for x, y in pointsToChange:
stringArray[y][x] = characterMap[
stringArray[y][x]] if stringArray[y][x] in characterMap else stringArray[y][x]
for x, y in config['highlightedBoxes']:
highlightBox(x, y)
for x, y in config['highlightedContent']:
highlightContent(x, y)
# TODO: Center board
return '\n'.join([''.join(line) for line in stringArray])
def selectPiece(self, player) -> tuple:
"""Lets the user select a piece from a graphic board"""
x, y = 0, 0
while True:
system('clear')
print(player.name, '\n')
print(self.draw({'highlightedBoxes': [(x, y)]}), '\n')
key = input(f" W E\n A S D <- Enter : ")[0]
if key in ['s', 'j'] and y != 7: y += 1
elif key in ['w', 'k'] and y != 0: y -= 1
elif key in ['d', 'l'] and x != 7: x += 1
elif key in ['a', 'h'] and x != 0: x -= 1
elif key == 'e': return (x, y)
def getPieceAt(self, x, y) -> Piece:
return self.boardArray[y][x]