Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 55 additions & 11 deletions chess_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __init__(self, theme_name="dark", interactive=True, has_menu=False):
self.fens = [self.board.fen()]
self.view_ply = None
self.human_color = chess.WHITE
self.mode = "ai" # "ai" (vs model) | "two" (pass & play)
self.mode = "ai" # "ai" | "two" | "ai_ai"
saved_settings = settings_mod.load()
self.difficulty = saved_settings["difficulty"]
self.difficulty_value = saved_settings["difficulty_value"]
Expand Down Expand Up @@ -334,14 +334,19 @@ def new_game(self, color, mode="ai"):
self.bv.set_position(self.board, animate=False)
self.move_list.set_moves([])
self.model_panel.set_idle()
self.diff_slider.setVisible(mode == "ai")
self.style_combo.setVisible(mode == "ai")
self.adaptive_btn.setVisible(mode == "ai")
ai_mode = mode in ("ai", "ai_ai")
self.diff_slider.setVisible(ai_mode)
self.style_combo.setVisible(ai_mode)
self.adaptive_btn.setVisible(ai_mode)
self.diff_slider.set_mood(self.difficulty_value, self.ai_style)
self._refresh_cards()
self._set_controls()
if mode == "two":
self.set_status("White to move — pass & play")
elif mode == "ai_ai":
self.bv.set_view_only(True)
self.set_status("ChessNet White is thinking…")
self._trigger_ai()
elif color == chess.BLACK:
self._trigger_ai()
else:
Expand Down Expand Up @@ -405,10 +410,15 @@ def _on_coach_error(self, err, game_id):

def _trigger_ai(self):
self.ai_busy = True
self.bv.set_view_only(False)
if self.mode != "ai_ai":
self.bv.set_view_only(False)
self.card_top.set_thinking(True)
self.model_panel.set_thinking()
self.set_status("ChessNet is thinking…")
side_name = "White" if self.board.turn == chess.WHITE else "Black"
self.set_status(
f"ChessNet {side_name} is thinking…"
if self.mode == "ai_ai" else "ChessNet is thinking…"
)
self._set_controls()

game_id = self._game_id
Expand Down Expand Up @@ -455,6 +465,13 @@ def _on_ai_move(self, move, top, animate=True, game_id=None):
self._set_controls()
if self.board.is_game_over():
self._finish_game()
elif self.mode == "ai_ai":
next_side = "White" if self.board.turn == chess.WHITE else "Black"
self.set_status(
f"ChessNet {next_side} is thinking… · "
f"last move {elapsed:.2f}s · confidence {top_prob * 100:.0f}%"
)
QTimer.singleShot(120, self._trigger_ai)
elif self.board.is_check():
self.set_status("Your move — check!", error=True)
else:
Expand Down Expand Up @@ -573,6 +590,9 @@ def save_pgn(self):
if self.mode == "two":
game.headers["White"] = "Player 1"
game.headers["Black"] = "Player 2"
elif self.mode == "ai_ai":
game.headers["White"] = "ChessNet White"
game.headers["Black"] = "ChessNet Black"
else:
game.headers["White"] = ("You" if self.human_color == chess.WHITE
else "ChessNet")
Expand Down Expand Up @@ -622,8 +642,15 @@ def go_live(self):
side = "White" if self.board.turn == chess.WHITE else "Black"
self.set_status(f"{side} to move")
else:
self.set_status("ChessNet is thinking…" if self.ai_busy
else "Your move")
if self.mode == "ai_ai":
side = "White" if self.board.turn == chess.WHITE else "Black"
self.set_status(
f"ChessNet {side} is thinking…" if self.ai_busy
else f"ChessNet {side} to move"
)
else:
self.set_status("ChessNet is thinking…" if self.ai_busy
else "Your move")

def _choose_promotion(self, color, to_square):
pos = self.bv.map_square_to_global(to_square)
Expand All @@ -632,6 +659,21 @@ def _choose_promotion(self, color, to_square):

def _refresh_cards(self):
over = self.board.is_game_over()
if self.mode == "ai_ai":
self.card_bottom.set_identity("ChessNet White", "White · AI", "K")
self.card_top.set_identity("ChessNet Black", "Black · AI", "k")
white_caps, white_val = captured_by(self.board, chess.WHITE)
black_caps, black_val = captured_by(self.board, chess.BLACK)
self.card_bottom.set_captured(white_caps, white_val - black_val)
self.card_top.set_captured(black_caps, black_val - white_val)
self.card_bottom.set_turn(
self.board.turn == chess.WHITE and not over)
self.card_top.set_turn(
self.board.turn == chess.BLACK and not over)
self.card_top.set_thinking(
self.ai_busy and self.board.turn == chess.BLACK)
return

if self.mode == "two":
self.card_bottom.set_identity("White", "human · pass & play", "K")
self.card_top.set_identity("Black", "human · pass & play", "k")
Expand Down Expand Up @@ -680,7 +722,7 @@ def _finish_game(self):
elif winner_name == "You":
title = "Checkmate — you win!"
else:
title = "Checkmate — ChessNet wins"
title = f"Checkmate — {winner_name} wins"
sub = "Rematch?" if winner_name != "You" else "The CNN is mated."
else:
title = "Draw"
Expand All @@ -691,7 +733,7 @@ def _finish_game(self):
chess.Termination.FIVEFOLD_REPETITION: "Fivefold repetition",
}.get(outcome.termination, outcome.termination.name.capitalize())
sub = f"{reason}."
if self.mode == "ai":
if self.mode in ("ai", "ai_ai"):
tier = chess_coach.LABELS[self.difficulty]
style = chess_coach.STYLES.get(self.ai_style, "Balanced")
value = max(0, min(100, int(self.difficulty_value)))
Expand Down Expand Up @@ -720,7 +762,9 @@ def _finish_game(self):
self.sounds.play("end")

def _set_controls(self):
self.undo_btn.setEnabled(not self.ai_busy and bool(self.history))
self.undo_btn.setEnabled(
not self.ai_busy and bool(self.history) and self.mode != "ai_ai"
)

def set_status(self, text, error=False):
self.status_lbl.setText(text)
Expand Down
10 changes: 10 additions & 0 deletions ui/panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,11 @@ def __init__(self, theme, parent=None, first_run=False):
two_btn.clicked.connect(self._choose_two)
root.addWidget(two_btn, 0, Qt.AlignLeft)

ai_ai_btn = QPushButton("🤖 AI vs AI — watch ChessNet play itself")
ai_ai_btn.setToolTip("Let two ChessNet players play an automatic game.")
ai_ai_btn.clicked.connect(self._choose_ai_ai)
root.addWidget(ai_ai_btn, 0, Qt.AlignLeft)

if not first_run:
cancel = QPushButton("Cancel")
cancel.clicked.connect(self.reject)
Expand All @@ -520,6 +525,11 @@ def _choose_two(self):
self.chosen_color = chess.WHITE
self.accept()

def _choose_ai_ai(self):
self.chosen_mode = "ai_ai"
self.chosen_color = chess.WHITE
self.accept()

@staticmethod
def ask(theme, parent=None, first_run=False):
"""Return (mode, color); color is None when cancelled."""
Expand Down
Loading