From 08bd3e835c8d858791d3270bafec4afda844e5a6 Mon Sep 17 00:00:00 2001 From: Rudis Muiznieks Date: Tue, 26 Apr 2022 10:09:44 -0500 Subject: [PATCH] fixed stockfish probs --- plugin/chess/draw.py | 4 ++-- plugin/chess/game.py | 42 +++++++++++++++++++++++------------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/plugin/chess/draw.py b/plugin/chess/draw.py index d5aeaf0..682661f 100644 --- a/plugin/chess/draw.py +++ b/plugin/chess/draw.py @@ -1,6 +1,6 @@ from icecream import ic from graphics import Graphics -from chess import Board, Color, WHITE, BLACK +from chess import Board, Color, WHITE class Draw: BOARD_SIZE = 64 @@ -80,10 +80,10 @@ class Draw: self.BOARD_SIZE, 0, self.BOARD_SIZE, self.BOARD_SIZE, 0) def draw_board(self, board: Board, player_color: Color): - ic(board) self._graphics.fill_rect(0, 0, self.BOARD_SIZE, self.BOARD_SIZE, 0) nb = "".join(str(board).split()).replace(" ", "") nb = nb if player_color == WHITE else nb[::-1] + ic(player_color, nb) c = True for row in range(8): for col in range(8): diff --git a/plugin/chess/game.py b/plugin/chess/game.py index 52caa82..1e34122 100644 --- a/plugin/chess/game.py +++ b/plugin/chess/game.py @@ -17,7 +17,7 @@ class GameState(Enum): class ChessGame: SAVE_FILE = path.join("data", "chess_state.fen") - MAX_THINKING_SECONDS = 3 + STOCKFISH_MOVE_TIME = 3 def __init__(self, cinput: ControlInput, graphics: Graphics): self._sf = Stockfish(shutil.which("stockfish") or "stockfish") @@ -26,13 +26,13 @@ class ChessGame: self._state = GameState.MAIN_MENU self._menu_index = 0 - def _init_new_game(self, fen = None): + def _init_new_game(self, fen = None, player_color = None): self._board = Board() if fen == None else Board(fen) - self._last_move = "Begin!" + self._move = "Begin!" self._all_moves = dict() self._src_idx = 0 self._dst_idx = 0 - self._player_color = self._board.turn + self._player_color = player_color if player_color != None else self._board.turn self._sf.set_fen_position(self._board.fen()) self._draw.draw_board(self._board, self._player_color) @@ -48,6 +48,12 @@ class ChessGame: with open(self.SAVE_FILE, "w") as fen: fen.write(self._board.fen()) + def _make_move(self, move: str): + self._move = move + self._board.push(Move.from_uci(move)) + self._sf.make_moves_from_current_position([move]) + self._draw.draw_board(self._board, self._player_color) + def run(self): # either load the save game or start a new one self._load_saved_or_init() @@ -67,11 +73,11 @@ class ChessGame: # computer makes a move elif self._state == GameState.THINKING: - self._draw.draw_thinking(self._last_move) + self._draw.draw_thinking(self._move) # TODO: check game over - self._move = self._sf.get_best_move() - if self._move != None: - self._board.push(Move.from_uci(self._move)) + move = self._sf.get_best_move_time(self.STOCKFISH_MOVE_TIME * 1000) + if move != None: + self._make_move(move) # reset user moves and move state to user's turn self._all_moves = dict() self._src_idx = 0 @@ -79,16 +85,17 @@ class ChessGame: self._state = GameState.CHOOSE_SRC else: self._state = GameState.GAME_OVER - self._draw.draw_board(self._board, self._player_color) # user picks source piece elif self._state == GameState.CHOOSE_SRC: if len(list(self._all_moves)) == 0: - for src, dst in map(lambda m: [Move.uci(m)[0:2], Move.uci(m)[2:4]], self._board.legal_moves): + moves = self._board.generate_legal_moves() + mvarray = map(lambda m: [Move.uci(m)[0:2], Move.uci(m)[2:4]], moves) + for src, dst in list(mvarray): self._all_moves.setdefault(src, []).append(dst) - if len(list(self._all_moves > 0)): + if len(list(self._all_moves)) > 0: src = list(self._all_moves)[self._src_idx] - self._draw.draw_select(self._last_move, src) + self._draw.draw_select(self._move, src) key = self._cinput.get_one_shot() else: self._state = GameState.GAME_OVER @@ -97,7 +104,7 @@ class ChessGame: elif self._state == GameState.CHOOSE_DST: src = list(self._all_moves)[self._src_idx] dst = self._all_moves[src][self._dst_idx] - self._draw.draw_select(self._last_move, src, dst) + self._draw.draw_select(self._move, src, dst) key = self._cinput.get_one_shot() # game has ended @@ -129,12 +136,11 @@ class ChessGame: self._save_game() return elif self._menu_index == 2 or self._menu_index == 3: # new game - self._init_new_game() if self._menu_index == 2: - self._player_color = WHITE + self._init_new_game(player_color=WHITE) self._state = GameState.CHOOSE_SRC else: - self._player_color = BLACK + self._init_new_game(player_color=BLACK) self._state = GameState.THINKING # handle user input when selecting source piece @@ -178,9 +184,7 @@ class ChessGame: # make the move elif key == Button.BTN_B: dst = self._all_moves[src][self._dst_idx] - self._move = src + dst - self._board.append(Move.from_uci(self._move)) - self._draw.draw_board(self._board, self._player_color) + self._make_move(src + dst) # TODO: check game over self._state = GameState.THINKING