Skip to content

Commit

Permalink
online game premove
Browse files Browse the repository at this point in the history
  • Loading branch information
LKL1235 committed May 21, 2024
1 parent 011ab50 commit ee7f693
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/cli_chess/core/game/offline_game/offline_game_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def make_move(self, move: str):
move = move.strip()
if not move:
raise Warning("No move specified")

# clean premove
self.board_model.set_premove(None)
self.board_model.make_move(move)

Expand All @@ -64,7 +64,7 @@ def make_move(self, move: str):
raise Warning("Game has already ended")

def make_premove(self, move: str):
"""Sends the move to the board model for it to be made"""
"""Verify move and Set board model premove"""
if self.game_in_progress:
try:
if self.board_model.board.is_game_over():
Expand All @@ -77,14 +77,16 @@ def make_premove(self, move: str):
move = move.strip()
if not move:
raise Warning("No move specified")
# use a temporary board skip one turn to verify the move
fen = self.board_model.board.fen()
tmp_board = Board(fen)
tmp_board.turn = not tmp_board.turn
try:
move = tmp_board.push_san(move).uci()
except ValueError as e:
raise Warning("Invalid move")
except ValueError:
raise Warning("Invalid premove")
self.board_model.set_premove(move)
# to update highlight for premove
self.board_model._notify_board_model_updated(successfulMoveMade=True)
except Exception:
raise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def make_move(self, move: str) -> None:
self.model.make_move(move)
self.make_engine_move()
else:
# if not my turn, set premove
self.model.make_premove(move)
except Exception as e:
self.view.alert.show_alert(str(e))
Expand All @@ -75,6 +76,7 @@ def make_engine_move(self) -> None:
move = engine_move.move.uci()
log.debug(f"Received move ({move}) from engine.")
self.board_presenter.make_move(move)
# After engine move, if premove is set, make it
if self.model.board_model.get_premove():
self.make_move(self.model.board_model.get_premove())
except Exception as e:
Expand Down
40 changes: 37 additions & 3 deletions src/cli_chess/core/game/online_game/online_game_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from cli_chess.core.game.game_options import GameOption
from cli_chess.core.api import GameStateDispatcher
from cli_chess.utils import log, threaded, RequestSuccessfullySent
from chess import COLOR_NAMES, WHITE
from chess import COLOR_NAMES, WHITE, Board
from typing import Optional


Expand Down Expand Up @@ -122,6 +122,9 @@ def handle_game_state_dispatcher_event(self, **kwargs) -> None:
# and our local board are in sync (eg. takebacks, moves played on website, etc)
self.board_model.reset(notify=False)
self.board_model.make_moves_from_list(event.get('moves', []).split())
# if board_model.premove is set, make the move
if self.is_my_turn() and self.board_model.get_premove() is not None:
self.make_move(self.board_model.get_premove())

if kwargs['gameOver']:
self._report_game_over(status=event.get('status'), winner=event.get('winner', ""))
Expand Down Expand Up @@ -154,6 +157,8 @@ def make_move(self, move: str):

move = self.board_model.verify_move(move)
self.game_state_dispatcher.make_move(move)
# clean premove
self.board_model.set_premove(None)
except Exception:
raise
else:
Expand All @@ -164,9 +169,38 @@ def make_move(self, move: str):
raise Warning("Game has already ended")

def make_premove(self, move: str):
# TODO: Implement premove for online games
pass
""" Verify move and Set board model premove """
if self.game_in_progress:
try:
if self.board_model.get_premove() is not None:
raise Warning("You already have a premove set")

move = move.strip()
if not move:
raise Warning("No move specified")

if move == "0000":
raise Warning("Null moves are not supported in online games")

# use a temporary board skip one turn to verify the move
fen = self.board_model.board.fen()
tmp_board = Board(fen)
if tmp_board.turn != self.my_color:
tmp_board.turn = not tmp_board.turn
try:
move = tmp_board.push_san(move).uci()
except ValueError as e:
raise Warning("Invalid premove")
self.board_model.set_premove(move)
except Exception:
raise
else:
log.warning("Attempted to make a move in a game that's not in progress")
if self.searching:
raise Warning("Still searching for opponent")
else:
raise Warning("Game has already ended")

def propose_takeback(self) -> None:
"""Notifies the game state dispatcher to propose a takeback"""
if self.game_in_progress:
Expand Down
1 change: 1 addition & 0 deletions src/cli_chess/modules/board/board_presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def get_square_display_color(self, square: chess.Square) -> str:
if self.model.is_square_in_check(square):
square_color = "in-check"

# premove highlight
if self.model.get_premove() is not None:
try:
move = chess.Move.from_uci(self.model.get_premove())
Expand Down

0 comments on commit ee7f693

Please sign in to comment.