from icecream import ic from graphics import Graphics from .sunfish import Position class Draw: BOARD_SIZE = 64 SQUARE_SIZE = 8 PIECES = { "p": [ " . . . . . . ", " . . ■ ■ . . ", " . ■ - - ■ . ", " . ■ - - ■ . ", " . . ■ ■ . . ", " . . . . . . "], "r": [ " ■ ■ ■ ■ ■ ■ ", " ■ - - - - ■ ", " . ■ - - ■ . ", " . ■ - - ■ . ", " ■ - - - - ■ ", " ■ ■ ■ ■ ■ ■ "], "n": [ " . . ■ ■ ■ . ", " . ■ - - - ■ ", " ■ - - - - ■ ", " . ■ - - - ■ ", " . . ■ - - ■ ", " . . ■ ■ ■ ■ "], "b": [ " . . ■ ■ . . ", " . ■ - - ■ . ", " . ■ - - ■ . ", " . ■ - - ■ . ", " ■ - - - - ■ ", " ■ ■ ■ ■ ■ ■ "], "q": [ " ■ . . . . ■ ", " ■ ■ . . ■ ■ ", " ■ - ■ ■ - ■ ", " ■ - - - - ■ ", " ■ - - - - ■ ", " ■ ■ ■ ■ ■ ■ "], "k": [ " . . ■ ■ . . ", " ■ ■ - - ■ ■ ", " ■ - - - - ■ ", " ■ ■ - - ■ ■ ", " . ■ - - ■ . ", " . ■ ■ ■ ■ . "], } MENU = [ "Play", "Quit", "New (W)", "New (B)"] 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) def _clear_info(self): self._graphics.fill_rect( self.BOARD_SIZE, 0, self.BOARD_SIZE, self.BOARD_SIZE, 0) def draw_board(self, pos: Position, player_color: str): self._graphics.fill_rect(0, 0, self.BOARD_SIZE, self.BOARD_SIZE, 0) nb = "".join(pos.board.split()) if player_color == "w" else "".join(pos.rotate().board.split())[::-1] 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: self._draw_piece( self.PIECES[p.lower()], p.isupper(), col, row, c) c = not c c = not c self._graphics.show() 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() def draw_thinking(self, last_move: str): self._clear_info() self._graphics.text(last_move, 12, 0, 1) self._graphics.text("Shh, I'm", 12, 2, 1) self._graphics.text("thinking!", 12, 3, 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_select(self, last_move: str, src: str, dst = None): self._clear_info() self._graphics.text(last_move, 12, 0, 1) self._graphics.text("You move:", 12, 2, 1) if dst == None: self._graphics.text("<" + src + "> - __", 12, 3, 1) else: self._graphics.text(src + " - <" + dst + ">", 12, 3, 1) self._graphics.show()