2022-04-24 10:07:39 -05:00
|
|
|
from icecream import ic
|
|
|
|
from graphics import Graphics
|
|
|
|
|
|
|
|
class Draw:
|
|
|
|
BOARD_SIZE = 64
|
|
|
|
SQUARE_SIZE = 8
|
|
|
|
|
|
|
|
PIECES = {
|
|
|
|
"p": [
|
|
|
|
" . . . . . . ",
|
|
|
|
" . . ■ ■ . . ",
|
|
|
|
" . ■ - - ■ . ",
|
|
|
|
" . ■ - - ■ . ",
|
|
|
|
" . . ■ ■ . . ",
|
|
|
|
" . . . . . . "],
|
|
|
|
"r": [
|
|
|
|
" ■ ■ ■ ■ ■ ■ ",
|
|
|
|
" ■ - - - - ■ ",
|
|
|
|
" . ■ - - ■ . ",
|
|
|
|
" . ■ - - ■ . ",
|
|
|
|
" ■ - - - - ■ ",
|
|
|
|
" ■ ■ ■ ■ ■ ■ "],
|
|
|
|
"n": [
|
|
|
|
" . . ■ ■ ■ . ",
|
|
|
|
" . ■ - - - ■ ",
|
|
|
|
" ■ - - - - ■ ",
|
|
|
|
" . ■ - - - ■ ",
|
|
|
|
" . . ■ - - ■ ",
|
|
|
|
" . . ■ ■ ■ ■ "],
|
|
|
|
"b": [
|
|
|
|
" . . ■ ■ . . ",
|
|
|
|
" . ■ - - ■ . ",
|
|
|
|
" . ■ - - ■ . ",
|
|
|
|
" . ■ - - ■ . ",
|
|
|
|
" ■ - - - - ■ ",
|
|
|
|
" ■ ■ ■ ■ ■ ■ "],
|
|
|
|
"q": [
|
|
|
|
" ■ . . . . ■ ",
|
|
|
|
" ■ ■ . . ■ ■ ",
|
|
|
|
" ■ - ■ ■ - ■ ",
|
|
|
|
" ■ - - - - ■ ",
|
|
|
|
" ■ - - - - ■ ",
|
|
|
|
" ■ ■ ■ ■ ■ ■ "],
|
|
|
|
|
|
|
|
"k": [
|
|
|
|
" . . ■ ■ . . ",
|
|
|
|
" ■ ■ - - ■ ■ ",
|
|
|
|
" ■ - - - - ■ ",
|
|
|
|
" ■ ■ - - ■ ■ ",
|
|
|
|
" . ■ - - ■ . ",
|
|
|
|
" . ■ ■ ■ ■ . "],
|
|
|
|
}
|
|
|
|
|
2022-04-24 13:32:14 -05:00
|
|
|
MENU = [
|
2022-04-24 15:43:34 -05:00
|
|
|
"Play",
|
|
|
|
"Quit",
|
2022-04-24 13:32:14 -05:00
|
|
|
"New (W)",
|
2022-04-24 15:43:34 -05:00
|
|
|
"New (B)"]
|
2022-04-24 13:32:14 -05:00
|
|
|
|
2022-04-24 10:07:39 -05:00
|
|
|
def __init__(self, graphics: Graphics):
|
|
|
|
self._graphics = graphics
|
|
|
|
|
|
|
|
def _draw_piece(self, piece: list[str], white: bool, sqx, sqy, sqc):
|
|
|
|
c = 0 if sqc == 1 else 1
|
|
|
|
filled = (white and sqc == 0) or (not white and sqc == 1)
|
|
|
|
for row in range(len(piece)):
|
|
|
|
pixels = piece[row].replace(" ", "")
|
|
|
|
for col in range(len(pixels)):
|
|
|
|
x = (sqx * self.SQUARE_SIZE) + col + 1
|
|
|
|
y = (sqy * self.SQUARE_SIZE) + row + 1
|
|
|
|
pixel = pixels[col]
|
|
|
|
if pixel == '■':
|
|
|
|
self._graphics.pixel(x, y, c)
|
|
|
|
elif pixel =='-':
|
|
|
|
self._graphics.pixel(x, y, c if filled else sqc)
|
|
|
|
|
2022-04-24 13:32:14 -05:00
|
|
|
def _clear_info(self):
|
|
|
|
self._graphics.fill_rect(
|
|
|
|
self.BOARD_SIZE, 0, self.BOARD_SIZE, self.BOARD_SIZE, 0)
|
2022-04-24 10:07:39 -05:00
|
|
|
|
2022-04-24 13:32:14 -05:00
|
|
|
def draw_board(self, board: str):
|
2022-04-24 15:43:34 -05:00
|
|
|
ic(board)
|
2022-04-24 13:32:14 -05:00
|
|
|
self._graphics.fill_rect(0, 0, self.BOARD_SIZE, self.BOARD_SIZE, 0)
|
2022-04-24 10:07:39 -05:00
|
|
|
nb = "".join(board.split())
|
|
|
|
c = True
|
|
|
|
for row in range(8):
|
|
|
|
for col in range(8):
|
|
|
|
if c:
|
|
|
|
x = col * self.SQUARE_SIZE
|
|
|
|
y = row * self.SQUARE_SIZE
|
|
|
|
self._graphics.fill_rect(x, y, self.SQUARE_SIZE, self.SQUARE_SIZE, 1)
|
|
|
|
p = nb[(row * 8) + col]
|
|
|
|
if p.lower() in self.PIECES:
|
2022-04-24 15:43:34 -05:00
|
|
|
self._draw_piece(
|
2022-04-24 18:18:50 -05:00
|
|
|
self.PIECES[p.lower()], p.isupper(), col, row, c)
|
2022-04-24 10:07:39 -05:00
|
|
|
c = not c
|
2022-04-24 15:43:34 -05:00
|
|
|
c = not c
|
2022-04-24 10:07:39 -05:00
|
|
|
self._graphics.show()
|
2022-04-24 13:32:14 -05:00
|
|
|
|
|
|
|
def draw_menu(self, menu_index: int):
|
|
|
|
self._clear_info()
|
|
|
|
self._graphics.text("Menu:", 12, 0, 1)
|
|
|
|
for i in range(len(self.MENU)):
|
|
|
|
marker = "> " if i == menu_index else " "
|
|
|
|
self._graphics.text(marker + self.MENU[i], 12, i + 2, 1)
|
|
|
|
self._graphics.show()
|
2022-04-24 15:43:34 -05:00
|
|
|
|
2022-04-24 18:18:50 -05:00
|
|
|
def draw_thinking(self):
|
2022-04-24 15:43:34 -05:00
|
|
|
self._clear_info()
|
2022-04-24 18:18:50 -05:00
|
|
|
self._graphics.text("Shh, I'm", 12, 0, 1)
|
|
|
|
self._graphics.text("thinking!", 12, 1, 1)
|
|
|
|
self._graphics.show()
|
|
|
|
|
|
|
|
def draw_checkmate(self, move: str, player_won: bool):
|
|
|
|
self._clear_info()
|
|
|
|
self._graphics.text(move, 12, 0, 1)
|
|
|
|
self._graphics.text("Checkmate", 12, 1, 1)
|
|
|
|
self._graphics.text(
|
|
|
|
"you win!" if player_won else "you lose!", 12, 2, 1)
|
|
|
|
self._graphics.show()
|
|
|
|
|
|
|
|
def draw_rowsel(self, move: str):
|
|
|
|
self._clear_info()
|
|
|
|
self._graphics.text(move, 12, 0, 1)
|
2022-04-24 15:43:34 -05:00
|
|
|
self._graphics.show()
|