finished implementing chess game
This commit is contained in:
parent
762b1a2dbb
commit
c2c401a75b
22
__main__.py
22
__main__.py
|
@ -12,10 +12,14 @@ menu_config = [
|
||||||
MenuItem("Apps",
|
MenuItem("Apps",
|
||||||
MenuType.SUB_MENU,
|
MenuType.SUB_MENU,
|
||||||
{"sub_menu": [
|
{"sub_menu": [
|
||||||
MenuItem("Chess",
|
MenuItem("Chess Game",
|
||||||
MenuType.PLUGIN,
|
MenuType.PLUGIN,
|
||||||
{"plugin": "chess",
|
{"plugin": "chess",
|
||||||
"arg": None}),
|
"arg": "game"}),
|
||||||
|
MenuItem("Chess Puzzles",
|
||||||
|
MenuType.PLUGIN,
|
||||||
|
{"plugin": "chess",
|
||||||
|
"arg": "puzzles"}),
|
||||||
MenuItem("Cube Timer",
|
MenuItem("Cube Timer",
|
||||||
MenuType.PLUGIN,
|
MenuType.PLUGIN,
|
||||||
{"plugin": "cube",
|
{"plugin": "cube",
|
||||||
|
@ -25,6 +29,18 @@ menu_config = [
|
||||||
MenuType.PLUGIN,
|
MenuType.PLUGIN,
|
||||||
{"plugin": "info",
|
{"plugin": "info",
|
||||||
"arg": None}),
|
"arg": None}),
|
||||||
|
MenuItem("Config",
|
||||||
|
MenuType.SUB_MENU,
|
||||||
|
{"sub_menu": [
|
||||||
|
MenuItem("Brightness",
|
||||||
|
MenuType.PLUGIN,
|
||||||
|
{"plugin": "config",
|
||||||
|
"arg": "brightness"}),
|
||||||
|
MenuItem("Wifi",
|
||||||
|
MenuType.PLUGIN,
|
||||||
|
{"plugin": "config",
|
||||||
|
"arg": "wifi"}),
|
||||||
|
]}),
|
||||||
MenuItem("Reboot",
|
MenuItem("Reboot",
|
||||||
MenuType.EXIT_CMD,
|
MenuType.EXIT_CMD,
|
||||||
{"command": "sudo reboot"}),
|
{"command": "sudo reboot"}),
|
||||||
|
@ -47,12 +63,12 @@ signal.signal(signal.SIGTERM, program_exit)
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
item = menu.get_selection()
|
item = menu.get_selection()
|
||||||
ic(item)
|
|
||||||
if item.menu_type == MenuType.PLUGIN:
|
if item.menu_type == MenuType.PLUGIN:
|
||||||
try:
|
try:
|
||||||
graphics.clear()
|
graphics.clear()
|
||||||
graphics.text("Loading...", 0, 0, 1)
|
graphics.text("Loading...", 0, 0, 1)
|
||||||
graphics.show()
|
graphics.show()
|
||||||
|
ic(item)
|
||||||
plugin = import_module("plugin." + item.data["plugin"])
|
plugin = import_module("plugin." + item.data["plugin"])
|
||||||
ic(plugin)
|
ic(plugin)
|
||||||
plugin.execute(cinput, graphics, item.data["arg"])
|
plugin.execute(cinput, graphics, item.data["arg"])
|
||||||
|
|
|
@ -2,5 +2,6 @@ from cinput import ControlInput
|
||||||
from graphics import Graphics
|
from graphics import Graphics
|
||||||
from .game import ChessGame
|
from .game import ChessGame
|
||||||
|
|
||||||
def execute(cinput: ControlInput, graphics: Graphics, _):
|
def execute(cinput: ControlInput, graphics: Graphics, arg: str):
|
||||||
ChessGame(cinput, graphics).run()
|
if arg == "game":
|
||||||
|
ChessGame(cinput, graphics).run()
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from icecream import ic
|
from icecream import ic
|
||||||
from graphics import Graphics
|
from graphics import Graphics
|
||||||
import chess
|
from chess import Board, Outcome, Color, WHITE
|
||||||
|
|
||||||
class Draw:
|
class Draw:
|
||||||
BOARD_SIZE = 64
|
BOARD_SIZE = 64
|
||||||
|
@ -90,37 +90,41 @@ class Draw:
|
||||||
c = 0 if c != 0 else 1
|
c = 0 if c != 0 else 1
|
||||||
self._graphics.pixel(x, y, c)
|
self._graphics.pixel(x, y, c)
|
||||||
|
|
||||||
def _cursor_clear(self, except_for: list[str], player_color: chess.Color):
|
def _cursor_clear(self, except_for: list[str], player_color: Color):
|
||||||
c = 0
|
c = 1
|
||||||
for row in range(8):
|
for row in range(8):
|
||||||
for col in range(8):
|
for col in range(8): # 0,0 w = a8 b = h1 ... 7,7 w = h1 b = a8
|
||||||
sqx = col if player_color == chess.WHITE else 7 - col
|
sqx = col if player_color == WHITE else 7 - col
|
||||||
sqy = row if player_color == chess.BLACK else 7 - row
|
sqy = 7 - row if player_color == WHITE else row
|
||||||
square = chr(sqx + 97) + str(sqy + 1)
|
square = chr(sqx + 97) + str(sqy + 1)
|
||||||
if not (square in except_for):
|
if not (square in except_for):
|
||||||
# see if square is right color
|
# see if square is right color
|
||||||
sqc = self._graphics.pixel(sqx * 8, sqy * 8)
|
sqc = self._graphics.pixel(col * 8, row * 8)
|
||||||
if sqc != c:
|
if sqc != c:
|
||||||
ic(square)
|
|
||||||
self._cursor(square, player_color);
|
self._cursor(square, player_color);
|
||||||
c = 0 if c == 1 else 1
|
c = 0 if c == 1 else 1
|
||||||
c = 0 if c == 1 else 1
|
c = 0 if c == 1 else 1
|
||||||
|
|
||||||
def _cursor(self, square: str, player_color: chess.Color):
|
def _cursor(self, square: str, player_color: Color):
|
||||||
sqx = ord(square[0]) - 97
|
sqx = ord(square[0]) - 97
|
||||||
sqy = int(square[1]) - 1
|
sqy = int(square[1]) - 1
|
||||||
if player_color == chess.WHITE:
|
if player_color == WHITE:
|
||||||
sqy = 7 - sqy
|
sqy = 7 - sqy
|
||||||
else:
|
else:
|
||||||
sqx = 7 - sqx
|
sqx = 7 - sqx
|
||||||
for i in range(7):
|
for i in range(8):
|
||||||
self._invert_pixel(sqx * 8 + i, sqy * 8) #0,0 - 7,0
|
# top and bottom row
|
||||||
self._invert_pixel(sqx * 8 + i, sqy * 8 + 7) # 0,7 - 7,7
|
self._invert_pixel(sqx * 8 + i, sqy * 8)
|
||||||
|
self._invert_pixel(sqx * 8 + i, sqy * 8 + 7)
|
||||||
|
if i > 0 and i < 7:
|
||||||
|
# sides (skipping top and bottom row)
|
||||||
|
self._invert_pixel(sqx * 8, sqy * 8 + i)
|
||||||
|
self._invert_pixel(sqx * 8 + 7, sqy * 8 + i)
|
||||||
|
|
||||||
def draw_board(self, board: chess.Board, player_color: chess.Color):
|
def draw_board(self, board: Board, player_color: Color):
|
||||||
self._graphics.fill_rect(0, 0, self.BOARD_SIZE, self.BOARD_SIZE, 0)
|
self._graphics.fill_rect(0, 0, self.BOARD_SIZE, self.BOARD_SIZE, 0)
|
||||||
nb = "".join(str(board).split()).replace(" ", "")
|
nb = "".join(str(board).split()).replace(" ", "")
|
||||||
nb = nb if player_color == chess.WHITE else nb[::-1]
|
nb = nb if player_color == WHITE else nb[::-1]
|
||||||
c = True
|
c = True
|
||||||
for row in range(8):
|
for row in range(8):
|
||||||
for col in range(8):
|
for col in range(8):
|
||||||
|
@ -159,28 +163,25 @@ class Draw:
|
||||||
"you win!" if player_won else "you lose!", 12, 2, 1)
|
"you win!" if player_won else "you lose!", 12, 2, 1)
|
||||||
self._graphics.show()
|
self._graphics.show()
|
||||||
|
|
||||||
def draw_select(self, board: chess.Board, last_move: str, player_color: chess.Color, src: Optional[str], dst: Optional[str] = None):
|
def draw_select(self, last_move: str, player_color: Color, src: Optional[str], dst: Optional[str] = None):
|
||||||
self._clear_info()
|
self._clear_info()
|
||||||
self._graphics.text(self._format_move(last_move), 12, 0, 1)
|
self._graphics.text(self._format_move(last_move), 12, 0, 1)
|
||||||
self._graphics.text("You move:", 12, 2, 1)
|
self._graphics.text("You move:", 12, 2, 1)
|
||||||
if src != None and dst == None:
|
if src != None and dst == None:
|
||||||
ic('skipping src only')
|
|
||||||
self._graphics.text("<" + src.upper() + "> - __", 12, 3, 1)
|
self._graphics.text("<" + src.upper() + "> - __", 12, 3, 1)
|
||||||
self._cursor_clear([src], player_color)
|
self._cursor_clear([src], player_color)
|
||||||
self._cursor(src, player_color)
|
self._cursor(src, player_color)
|
||||||
elif src != None and dst != None:
|
elif src != None and dst != None:
|
||||||
ic('skipping src and dest')
|
|
||||||
self._graphics.text(src.upper() + " - <" + dst.upper() + ">", 12, 3, 1)
|
self._graphics.text(src.upper() + " - <" + dst.upper() + ">", 12, 3, 1)
|
||||||
self._cursor_clear([src, dst], player_color)
|
self._cursor_clear([src, dst], player_color)
|
||||||
self._cursor(src, player_color)
|
self._cursor(src, player_color)
|
||||||
self._cursor(dst, player_color)
|
self._cursor(dst, player_color)
|
||||||
else:
|
else:
|
||||||
ic('clearing all cursors')
|
|
||||||
self._cursor_clear(list(), player_color)
|
self._cursor_clear(list(), player_color)
|
||||||
|
|
||||||
self._graphics.show()
|
self._graphics.show()
|
||||||
|
|
||||||
def draw_game_over(self, outcome: Optional[chess.Outcome]):
|
def draw_game_over(self, outcome: Optional[Outcome]):
|
||||||
self._clear_info()
|
self._clear_info()
|
||||||
self._graphics.text("Game over", 12, 0, 1)
|
self._graphics.text("Game over", 12, 0, 1)
|
||||||
if outcome != None:
|
if outcome != None:
|
||||||
|
@ -188,6 +189,6 @@ class Draw:
|
||||||
self._graphics.text("Draw!", 12, 2, 1)
|
self._graphics.text("Draw!", 12, 2, 1)
|
||||||
else:
|
else:
|
||||||
self._graphics.text("Winner:", 12, 2, 1)
|
self._graphics.text("Winner:", 12, 2, 1)
|
||||||
self._graphics.text("White!" if outcome.winner == chess.WHITE else "Black!",
|
self._graphics.text("White!" if outcome.winner == WHITE else "Black!",
|
||||||
12, 3, 1)
|
12, 3, 1)
|
||||||
self._graphics.show()
|
self._graphics.show()
|
||||||
|
|
|
@ -120,7 +120,7 @@ class ChessGame:
|
||||||
elif self._state == GameState.CHOOSE_SRC:
|
elif self._state == GameState.CHOOSE_SRC:
|
||||||
if len(self._get_sources()) > 0:
|
if len(self._get_sources()) > 0:
|
||||||
src = self._get_sources()[self._src_idx]
|
src = self._get_sources()[self._src_idx]
|
||||||
self._draw.draw_select(self._board, self._move, self._player_color, src)
|
self._draw.draw_select(self._move, self._player_color, src)
|
||||||
key = self._cinput.get_one_shot(0.1)
|
key = self._cinput.get_one_shot(0.1)
|
||||||
else:
|
else:
|
||||||
self._state = GameState.GAME_OVER
|
self._state = GameState.GAME_OVER
|
||||||
|
@ -129,7 +129,7 @@ class ChessGame:
|
||||||
elif self._state == GameState.CHOOSE_DST:
|
elif self._state == GameState.CHOOSE_DST:
|
||||||
src = self._get_sources()[self._src_idx]
|
src = self._get_sources()[self._src_idx]
|
||||||
dst = self._get_dests()[self._dst_idx]
|
dst = self._get_dests()[self._dst_idx]
|
||||||
self._draw.draw_select(self._board, self._move, self._player_color, src, dst)
|
self._draw.draw_select(self._move, self._player_color, src, dst)
|
||||||
key = self._cinput.get_one_shot(0.1)
|
key = self._cinput.get_one_shot(0.1)
|
||||||
|
|
||||||
# game has ended
|
# game has ended
|
||||||
|
|
Reference in New Issue