feat(duo): two players on one computer
public
feat(duo): two players on one computer Hint budget per colour (hint and threat cost one each), visible move glyphs per colour (none/own/all, only while that colour is to move, default none), optional clock with increment, optional board flip. Player to move in bold, undo without asking, draw and resign via the flag button. Afterwards both sides are analysed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | 2194dab24eb5212261407028357efd8fe6dc1ea5 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | 8990a946f8e2 |
| Children | 697ad135f48e |
| Pushlog | by timo via ssh by timo via ssh |
modified README.md +6 −0
| @@ -46,6 +46,12 @@ Schlichtes GNOME-Schachprogramm (Python, | ||
| 46 | 46 | oder Fehler. Zugumstellungen zählen (Vergleich nach Stellung, nicht nach Zugfolge). |
| 47 | 47 | Hinweis zeigt den Kurszug. Am Ende: „Nächste Linie“ oder „Gegen Maia weiterspielen“. |
| 48 | 48 | Eröffnungsbuch und -namen stammen von PyChess (`pychess_book.bin`, `eco.db`). |
| 49 | +- **Zwei Spieler** (Pfeil neben `+`): abwechselnd am selben Rechner. Handicap pro Farbe: | |
| 50 | + Anzahl Hinweise (Hinweis und Drohung kosten je einen) und welche Zugzeichen man sieht | |
| 51 | + (keine / eigene Züge / alle – nur solange diese Farbe am Zug ist; Standard: keine). | |
| 52 | + Optional Bedenkzeit (5, 10, 10+5, 15+10, 30 Minuten) und „Brett nach jedem Zug drehen“. | |
| 53 | + Wer am Zug ist, steht fett. Strg+Z nimmt ohne Rückfrage einen Halbzug zurück; Remis und | |
| 54 | + Aufgeben über die Fahne. Danach wird für beide Seiten analysiert. | |
| 49 | 55 | - **Zwischenablage:** Strg+C kopiert die Partie als PGN, Strg+Umschalt+C die gezeigte |
| 50 | 56 | Stellung als FEN. Strg+V stellt auf, was in der Zwischenablage steht: PGN, FEN (auch nur |
| 51 | 57 | die Figurenstellung) oder ein Lichess-Link (die Partie wird geholt, `/black` dreht das |
modified ROADMAP.md +5 −0
| @@ -383,6 +383,11 @@ Erklärung: | ||
| 383 | 383 | |
| 384 | 384 | ## 7. Zwei Spieler an einem Rechner |
| 385 | 385 | |
| 386 | +**Status:** umgesetzt (20.11.2026), einschließlich Schachuhr (Minuten | Sekunden, | |
| 387 | +zweistellig, ohne Zehntel). Nicht automatisch gespeichert: Eine Zwei-Spieler-Partie | |
| 388 | +überlebt kein Schließen des Fensters (Strg+S speichert sie als Analyse). Zugzeichen | |
| 389 | +entstehen beim Mitdenken und damit nur im aktiven Fenster. | |
| 390 | + | |
| 386 | 391 | Neu ▾ → „Zwei Spieler an diesem Rechner …“. Das braucht keine Engine als Gegner |
| 387 | 392 | und funktioniert offline. |
| 388 | 393 | |
modified schach/app.py +57 −15
| @@ -24,6 +24,7 @@ from .threat import null_board, threat_j | ||
| 24 | 24 | from .online import OnlineMixin # noqa: E402 |
| 25 | 25 | from .puzzles import PuzzleMixin # noqa: E402 |
| 26 | 26 | from .training import OpeningMixin # noqa: E402 |
| 27 | +from .duo import DuoMixin # noqa: E402 | |
| 27 | 28 | |
| 28 | 29 | APP_ID = "app.schach.Schach" |
| 29 | 30 | # Wer gibt den Hinweis? Maia schlägt menschliche Züge vor, Stockfish prüft sie (≤ 3 % schlechter); |
| @@ -86,9 +87,10 @@ def outcome_text(board, human, opponent= | ||
| 86 | 87 | }.get(outcome.termination, "Remis.") |
| 87 | 88 | |
| 88 | 89 | |
| 89 | -class Window(OnlineMixin, PuzzleMixin, OpeningMixin, Adw.ApplicationWindow): | |
| 90 | +class Window(OnlineMixin, PuzzleMixin, OpeningMixin, DuoMixin, Adw.ApplicationWindow): | |
| 90 | 91 | """Ein Brett. ``mode``: "maia" (Partie gegen Maia), "online" (Lichess-Partie, ohne Engine-Hilfe), |
| 91 | - "puzzle" (Lichess-Rätsel), "opening" (Eröffnungstraining), "review" (fertige Partie ansehen und analysieren).""" | |
| 92 | + "puzzle" (Lichess-Rätsel), "opening" (Eröffnungstraining), "duo" (zwei Spieler an diesem Rechner), | |
| 93 | + "review" (fertige Partie ansehen und analysieren).""" | |
| 92 | 94 | |
| 93 | 95 | def __init__(self, app, state=None, blank=False): |
| 94 | 96 | super().__init__(application=app, title="Schach", default_width=900, default_height=720) |
| @@ -130,6 +132,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 130 | 132 | self.review_requested = set() |
| 131 | 133 | self._online_init() |
| 132 | 134 | self._opening_init() |
| 135 | + self._duo_init() | |
| 133 | 136 | |
| 134 | 137 | # Engines gehören der App und werden von allen Fenstern geteilt |
| 135 | 138 | self.maia = app.maia |
| @@ -164,6 +167,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 164 | 167 | games.append("_Rätsel", "win.puzzle") |
| 165 | 168 | games.append_submenu("Rätsel-_Schwierigkeit", difficulty) |
| 166 | 169 | games.append("_Eröffnung üben …", "win.opening") |
| 170 | + games.append("_Zwei Spieler an diesem Rechner …", "win.duo") | |
| 167 | 171 | games.append("Aus der _Zwischenablage", "win.paste") |
| 168 | 172 | online = Gio.Menu() |
| 169 | 173 | online.append("Freund auf _Lichess herausfordern …", "win.challenge") |
| @@ -232,6 +236,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 232 | 236 | self.threat_btn = Gtk.Button(icon_name="dialog-warning-symbolic") |
| 233 | 237 | self.threat_btn.set_action_name("win.threat") |
| 234 | 238 | self.help_box.append(self.threat_btn) |
| 239 | + self.help_box.append(self._build_duo_button()) | |
| 235 | 240 | self.mode_stack.add_named(self.help_box, "help") |
| 236 | 241 | |
| 237 | 242 | online_menu = Gio.Menu() |
| @@ -340,6 +345,9 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 340 | 345 | ("shortcuts", self._shortcuts_dialog), |
| 341 | 346 | ("preferences", self._preferences_dialog), |
| 342 | 347 | ("opening", self._opening_dialog), |
| 348 | + ("duo", self._duo_dialog), | |
| 349 | + ("duo-draw", self._duo_draw), | |
| 350 | + ("duo-resign", self._duo_resign), | |
| 343 | 351 | ("opening-next", self._opening_again), |
| 344 | 352 | ("opening-continue", self._opening_play_on), |
| 345 | 353 | ("copy-pgn", self._copy_pgn), |
| @@ -411,7 +419,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 411 | 419 | return self.final_text |
| 412 | 420 | if self.mode in ("online", "puzzle", "opening"): |
| 413 | 421 | return None # das Ende meldet Lichess, die Rätsel-Lösung bzw. der Kurs |
| 414 | - if self.names: | |
| 422 | + if self.names or self.mode == "duo": | |
| 415 | 423 | return outcome_text(self.board, None) |
| 416 | 424 | name = "Maia" if self.mode == "maia" else (self.opponent.split(" (")[0] or "Gegner") |
| 417 | 425 | return outcome_text(self.board, self.human, name) |
| @@ -434,6 +442,8 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 434 | 442 | """Läuft hier eine Partie, die man nicht einfach ersetzen sollte?""" |
| 435 | 443 | if self.mode == "online": |
| 436 | 444 | return self.final_text is None |
| 445 | + if self.mode == "duo": | |
| 446 | + return bool(self.board.move_stack) and not self._over() | |
| 437 | 447 | return self.mode == "maia" and bool(self.board.move_stack) and not self._over() |
| 438 | 448 | |
| 439 | 449 | def _leave_special(self): |
| @@ -441,6 +451,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 441 | 451 | self._online_leave() |
| 442 | 452 | self._puzzle_leave() |
| 443 | 453 | self._opening_leave() |
| 454 | + self._duo_leave() | |
| 444 | 455 | self.mode = "maia" |
| 445 | 456 | self.opponent = "" |
| 446 | 457 | self.names = () |
| @@ -497,7 +508,10 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 497 | 508 | v.threat_from = threat.from_square if threat and self.threat_level >= 2 else None |
| 498 | 509 | |
| 499 | 510 | v.badge = None |
| 500 | - if self.badge_ply is not None and self.badge_ply in self.symbols: | |
| 511 | + v.turn_marker = self.board.turn if self.mode == "duo" and not self._over() else None | |
| 512 | + if self.mode == "duo": | |
| 513 | + v.badge = self._duo_badge() | |
| 514 | + elif self.badge_ply is not None and self.badge_ply in self.symbols: | |
| 501 | 515 | if self.badge_ply < len(self.board.move_stack): |
| 502 | 516 | mv = self.board.move_stack[self.badge_ply] |
| 503 | 517 | piece = self.board.piece_at(mv.to_square) |
| @@ -523,6 +537,8 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 523 | 537 | self.title.set_subtitle(self._puzzle_subtitle()) |
| 524 | 538 | elif self.mode == "opening": |
| 525 | 539 | self.title.set_subtitle(self._opening_subtitle()) |
| 540 | + elif self.mode == "duo": | |
| 541 | + self.title.set_subtitle(self._duo_subtitle()) | |
| 526 | 542 | elif self.mode == "online": |
| 527 | 543 | if self.online_full is None: |
| 528 | 544 | self.title.set_subtitle("Verbinde mit Lichess …") |
| @@ -543,16 +559,21 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 543 | 559 | self.title.set_subtitle("Maia wird geladen …") |
| 544 | 560 | else: |
| 545 | 561 | self.title.set_subtitle(f"Maia {self.elo} · du spielst {side}") |
| 546 | - default_title = {"puzzle": "Rätsel", "online": "Lichess", "opening": self._opening_title()}.get( | |
| 562 | + default_title = {"puzzle": "Rätsel", "online": "Lichess", "opening": self._opening_title(), | |
| 563 | + "duo": "Zwei Spieler"}.get( | |
| 547 | 564 | self.mode, "Schach") |
| 548 | 565 | self.title.set_title(self.save_name or default_title) |
| 549 | 566 | self._refresh_players() |
| 550 | 567 | |
| 551 | - hints = self.mode in ("maia", "puzzle", "opening") | |
| 568 | + hints = self.mode in ("maia", "puzzle", "opening", "duo") | |
| 569 | + budget = self.mode != "duo" or self._duo_can_help() or self.hint_level == 1 | |
| 552 | 570 | can_hint = ( |
| 553 | - hints and self._human_to_move() and self.hint_level < 2 and not self.hint_pending | |
| 571 | + hints and budget and self._human_to_move() and self.hint_level < 2 and not self.hint_pending | |
| 554 | 572 | and not self._browsing() |
| 555 | 573 | ) |
| 574 | + self.duo_btn.set_visible(self.mode == "duo") | |
| 575 | + self._action("duo-draw").set_enabled(self.mode == "duo" and not self._over()) | |
| 576 | + self._action("duo-resign").set_enabled(self.mode == "duo" and not self._over()) | |
| 556 | 577 | self._action("hint").set_enabled(can_hint) |
| 557 | 578 | self.hint_content.set_label("Zug zeigen" if self.hint_level == 1 else "Hinweis") |
| 558 | 579 | self.hint_btn.set_tooltip_text( |
| @@ -560,10 +581,11 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 560 | 581 | else "Kurszug zeigen (H)" if self.mode == "opening" |
| 561 | 582 | else f"Hinweis von {HELPERS[self._helper()][0]} (H)" |
| 562 | 583 | ) |
| 563 | - self._action("hint-helper").set_enabled(self.mode == "maia") # Rätsel: Hinweis = Lösung | |
| 584 | + self._action("hint-helper").set_enabled(self.mode in ("maia", "duo")) # Rätsel: Hinweis = Lösung | |
| 564 | 585 | puzzle_open = self.mode != "puzzle" or (self.puzzle is not None and not self.puzzle["done"]) |
| 586 | + threat_budget = self.mode != "duo" or self._duo_can_help() or self.threat_level == 1 | |
| 565 | 587 | can_threat = ( |
| 566 | - hints and puzzle_open and self._human_to_move() and self.threat_level < 2 | |
| 588 | + hints and puzzle_open and threat_budget and self._human_to_move() and self.threat_level < 2 | |
| 567 | 589 | and not self.threat_pending and not self._browsing() |
| 568 | 590 | ) |
| 569 | 591 | self._action("threat").set_enabled(can_threat) |
| @@ -589,10 +611,13 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 589 | 611 | and self._human_has_moved() |
| 590 | 612 | ) |
| 591 | 613 | self.undo_btn.set_tooltip_text("Rücknahme anfragen (Strg+Z)") |
| 614 | + elif self.mode == "duo": | |
| 615 | + self._action("undo").set_enabled(bool(self.board.move_stack) and not self._over()) | |
| 616 | + self.undo_btn.set_tooltip_text("Halbzug zurücknehmen (Strg+Z)") | |
| 592 | 617 | else: |
| 593 | 618 | self._action("undo").set_enabled(self.mode in ("maia", "opening") and self._human_has_moved()) |
| 594 | 619 | self.undo_btn.set_tooltip_text("Zug zurücknehmen (Strg+Z)") |
| 595 | - self.undo_btn.set_visible(self.mode in ("maia", "opening", "online")) | |
| 620 | + self.undo_btn.set_visible(self.mode in ("maia", "opening", "online", "duo")) | |
| 596 | 621 | self._action("offer-draw").set_enabled(self._online_running() and len(self.board.move_stack) >= 2) |
| 597 | 622 | self._action("resign").set_enabled(self._online_running() and self.online_full is not None) |
| 598 | 623 | self._action("end-browse").set_enabled(self.browse_ply is not None) |
| @@ -643,6 +668,9 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 643 | 668 | if self.mode == "opening" and self.course is not None: |
| 644 | 669 | self.view.names = {self.human: "Du", not self.human: f"Kurs: {self.course.title}"} |
| 645 | 670 | return |
| 671 | + if self.mode == "duo" and self.duo is not None: | |
| 672 | + self.view.names = self._duo_names() | |
| 673 | + return | |
| 646 | 674 | if self.mode == "maia": |
| 647 | 675 | other = f"Maia-3 · {self.elo}" |
| 648 | 676 | elif self.mode == "review" and self.opponent: |
| @@ -723,6 +751,9 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 723 | 751 | if self.mode == "opening": |
| 724 | 752 | self._opening_play(move) |
| 725 | 753 | return |
| 754 | + if self.mode == "duo": | |
| 755 | + self._duo_play(move) | |
| 756 | + return | |
| 726 | 757 | ply = len(self.board.move_stack) |
| 727 | 758 | before = self.board.copy() |
| 728 | 759 | key = annotate.key(before) |
| @@ -787,6 +818,9 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 787 | 818 | if self.mode == "online": |
| 788 | 819 | self._takeback_request() |
| 789 | 820 | return |
| 821 | + if self.mode == "duo": | |
| 822 | + self._duo_undo() | |
| 823 | + return | |
| 790 | 824 | if self.mode not in ("maia", "opening") or not self._human_has_moved(): |
| 791 | 825 | return |
| 792 | 826 | while self.board.move_stack: |
| @@ -819,8 +853,12 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 819 | 853 | self._opening_hint() |
| 820 | 854 | self._reveal_hint() |
| 821 | 855 | return |
| 822 | - if self.mode != "maia": | |
| 856 | + if self.mode not in ("maia", "duo"): | |
| 823 | 857 | return |
| 858 | + if self.mode == "duo": | |
| 859 | + if not self._duo_can_help(): | |
| 860 | + return | |
| 861 | + self._duo_spend_help() # ein Hinweis kostet einen, egal welche Stufe | |
| 824 | 862 | if self.hint_move is not None or self.hint_error is not None: |
| 825 | 863 | self._reveal_hint() # im Hintergrund schon fertig berechnet |
| 826 | 864 | else: |
| @@ -843,10 +881,14 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 843 | 881 | |
| 844 | 882 | def threat(self, *_): |
| 845 | 883 | """Erster Druck: bedrohtes Feld, zweiter: auch die drohende Figur.""" |
| 846 | - if self.mode not in ("maia", "puzzle", "opening") or not self._human_to_move() or self._browsing(): | |
| 884 | + if self.mode not in ("maia", "puzzle", "opening", "duo") or not self._human_to_move() or self._browsing(): | |
| 847 | 885 | return |
| 848 | 886 | if self.mode == "puzzle" and (self.puzzle is None or self.puzzle["done"]): |
| 849 | 887 | return |
| 888 | + if self.mode == "duo" and self.threat_level == 0: | |
| 889 | + if not self._duo_can_help(): | |
| 890 | + return | |
| 891 | + self._duo_spend_help() | |
| 850 | 892 | if self.threat_level == 1: |
| 851 | 893 | self.threat_level = 2 |
| 852 | 894 | self._refresh() |
| @@ -905,7 +947,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 905 | 947 | |
| 906 | 948 | def _start_thinking(self): |
| 907 | 949 | """Sobald der Mensch am Zug ist: Maias Kandidaten holen, dann denkt Stockfish mit.""" |
| 908 | - if self.mode != "maia" or not self._human_to_move() or self.hint_prefetch_gen == self.gen: | |
| 950 | + if self.mode not in ("maia", "duo") or not self._human_to_move() or self.hint_prefetch_gen == self.gen: | |
| 909 | 951 | return # Online-Partien: Fair Play, kein Mitdenken; Rätsel: Lösung steht fest |
| 910 | 952 | if not self.is_active(): |
| 911 | 953 | return # nur das aktive Fenster lässt Stockfish mitdenken |
| @@ -965,7 +1007,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 965 | 1007 | return False |
| 966 | 1008 | |
| 967 | 1009 | def _annotate(self, ply, move, before): |
| 968 | - if self.mode != "maia": | |
| 1010 | + if self.mode not in ("maia", "duo", "review"): | |
| 969 | 1011 | return |
| 970 | 1012 | entry = self.analyses.get(annotate.key(before)) |
| 971 | 1013 | symbol = annotate.classify_with(entry, before, move) |
| @@ -1365,7 +1407,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1365 | 1407 | mode="maia" if self.mode == "maia" else "review", |
| 1366 | 1408 | opponent=self.opponent, |
| 1367 | 1409 | final_text=self.final_text or "", |
| 1368 | - names=self.names, | |
| 1410 | + names=("Weiß", "Schwarz") if self.mode == "duo" else self.names, | |
| 1369 | 1411 | ) |
| 1370 | 1412 | |
| 1371 | 1413 | def load_state(self, state): |
modified schach/board.py +4 −1
| @@ -111,6 +111,7 @@ class BoardView(Gtk.DrawingArea): | ||
| 111 | 111 | self.names = None # {Farbe: "Name (1500)"} – Online-Partien |
| 112 | 112 | self.clocks = None # {Farbe: Millisekunden} |
| 113 | 113 | self.clock_running = None # Farbe, deren Uhr gerade läuft |
| 114 | + self.turn_marker = None # Farbe, deren Name fett steht (wer am Zug ist, bei zwei Spielern) | |
| 114 | 115 | |
| 115 | 116 | self._drag_sq = None |
| 116 | 117 | self._drag_pos = None |
| @@ -493,7 +494,9 @@ class BoardView(Gtk.DrawingArea): | ||
| 493 | 494 | if self.clocks and color in self.clocks: |
| 494 | 495 | right = self._draw_clock(cr, right, y, h, self.clocks[color], self.clock_running == color, fg) |
| 495 | 496 | if self.names and color in self.names: |
| 496 | - cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) | |
| 497 | + bold = self.turn_marker == color | |
| 498 | + cr.select_font_face( | |
| 499 | + "Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD if bold else cairo.FONT_WEIGHT_NORMAL) | |
| 497 | 500 | cr.set_font_size(h * 0.45) |
| 498 | 501 | text = self.names[color] |
| 499 | 502 | ext = cr.text_extents(text) |
added schach/duo.py +336 −0
| @@ -0,0 +1,336 @@ | ||
| 1 | +"""Zwei Spieler an einem Rechner – als Mixin für das Hauptfenster. | |
| 2 | + | |
| 3 | +Wer am Zug ist, spielt. Handicap pro Farbe: eine Anzahl Hinweise (Hinweis und | |
| 4 | +Drohung kosten je einen, egal welche Stufe) und welche Zugzeichen man sehen darf | |
| 5 | +(keine / eigene Züge / alle Züge – gezeigt nur, solange diese Farbe am Zug ist). | |
| 6 | +Optional eine Schachuhr. Nach der Partie wird für beide Seiten analysiert. | |
| 7 | +""" | |
| 8 | + | |
| 9 | +import time | |
| 10 | + | |
| 11 | +import chess | |
| 12 | +import gi | |
| 13 | + | |
| 14 | +gi.require_version("Gtk", "4.0") | |
| 15 | +gi.require_version("Adw", "1") | |
| 16 | +from gi.repository import Adw, Gio, GLib, Gtk # noqa: E402 | |
| 17 | + | |
| 18 | +from . import annotate # noqa: E402 | |
| 19 | + | |
| 20 | +COLOR_NAME = {chess.WHITE: "Weiß", chess.BLACK: "Schwarz"} | |
| 21 | +GLYPHS = [("none", "keine"), ("own", "eigene Züge"), ("all", "alle Züge")] | |
| 22 | +# (Bezeichnung, Minuten, Zuschlag in Sekunden); None = ohne Uhr | |
| 23 | +TIME_CONTROLS = [ | |
| 24 | + ("Ohne Uhr", None, 0), ("5 Minuten", 5, 0), ("10 Minuten", 10, 0), ("10 + 5", 10, 5), | |
| 25 | + ("15 + 10", 15, 10), ("30 Minuten", 30, 0), | |
| 26 | +] | |
| 27 | + | |
| 28 | + | |
| 29 | +def badge_ply(stack_len, viewer, first_turn, policy, symbols): | |
| 30 | + """Welches Zugzeichen sieht ``viewer`` (der gerade am Zug ist)? Halbzug oder None. | |
| 31 | + | |
| 32 | + ``first_turn``: wer den ersten Halbzug der Partie gemacht hat (bei FEN-Starts auch Schwarz). | |
| 33 | + """ | |
| 34 | + if policy == "none" or stack_len == 0: | |
| 35 | + return None | |
| 36 | + last = stack_len - 1 # Zug des Gegners | |
| 37 | + own = stack_len - 2 # eigener letzter Zug | |
| 38 | + | |
| 39 | + def mover(ply): | |
| 40 | + return first_turn if ply % 2 == 0 else not first_turn | |
| 41 | + | |
| 42 | + if policy == "all" and last in symbols and mover(last) != viewer: | |
| 43 | + return last | |
| 44 | + if own >= 0 and own in symbols and mover(own) == viewer: | |
| 45 | + return own | |
| 46 | + return None | |
| 47 | + | |
| 48 | + | |
| 49 | +class DuoMixin: | |
| 50 | + def _duo_init(self): | |
| 51 | + self.duo = None # {"hints", "used", "glyphs", "flip", "clock"} | |
| 52 | + self.duo_tick = 0 | |
| 53 | + | |
| 54 | + # --- Oberfläche -------------------------------------------------------- | |
| 55 | + | |
| 56 | + def _build_duo_button(self): | |
| 57 | + menu = Gio.Menu() | |
| 58 | + menu.append("_Remis vereinbaren", "win.duo-draw") | |
| 59 | + menu.append("_Aufgeben …", "win.duo-resign") | |
| 60 | + self.duo_btn = Gtk.MenuButton( | |
| 61 | + icon_name="emoji-flags-symbolic", menu_model=menu, visible=False, | |
| 62 | + tooltip_text="Remis oder Aufgeben", | |
| 63 | + ) | |
| 64 | + return self.duo_btn | |
| 65 | + | |
| 66 | + def _duo_dialog(self, *_): | |
| 67 | + dialog = Adw.AlertDialog( | |
| 68 | + heading="Zwei Spieler", | |
| 69 | + body="Ihr spielt abwechselnd an diesem Rechner. Wer möchte, gibt dem anderen einen Vorsprung.", | |
| 70 | + ) | |
| 71 | + dialog.add_response("cancel", "Abbrechen") | |
| 72 | + dialog.add_response("start", "Spielen") | |
| 73 | + dialog.set_response_appearance("start", Adw.ResponseAppearance.SUGGESTED) | |
| 74 | + dialog.set_default_response("start") | |
| 75 | + dialog.set_close_response("cancel") | |
| 76 | + | |
| 77 | + box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=12, width_request=340) | |
| 78 | + rows = {} | |
| 79 | + for color in (chess.WHITE, chess.BLACK): | |
| 80 | + group = Adw.PreferencesGroup(title=COLOR_NAME[color]) | |
| 81 | + hints = Adw.SpinRow( | |
| 82 | + title="Hinweise", subtitle="Hinweis und Drohung kosten je einen", | |
| 83 | + adjustment=Gtk.Adjustment(lower=0, upper=20, step_increment=1, page_increment=5), | |
| 84 | + ) | |
| 85 | + glyphs = Adw.ComboRow( | |
| 86 | + title="Zugzeichen", subtitle="!, ?, ?? – nur sichtbar, solange diese Farbe am Zug ist", | |
| 87 | + model=Gtk.StringList.new([label for _, label in GLYPHS]), | |
| 88 | + ) | |
| 89 | + group.add(hints) | |
| 90 | + group.add(glyphs) | |
| 91 | + box.append(group) | |
| 92 | + rows[color] = (hints, glyphs) | |
| 93 | + | |
| 94 | + game = Adw.PreferencesGroup(title="Partie") | |
| 95 | + clock = Adw.ComboRow(title="Bedenkzeit", model=Gtk.StringList.new([t[0] for t in TIME_CONTROLS])) | |
| 96 | + flip = Adw.SwitchRow(title="Brett nach jedem Zug drehen", subtitle="Wenn ihr euch gegenübersitzt") | |
| 97 | + game.add(clock) | |
| 98 | + game.add(flip) | |
| 99 | + box.append(game) | |
| 100 | + dialog.set_extra_child(box) | |
| 101 | + | |
| 102 | + def on_response(_d, response): | |
| 103 | + if response != "start": | |
| 104 | + return | |
| 105 | + config = { | |
| 106 | + "hints": {c: int(rows[c][0].get_value()) for c in rows}, | |
| 107 | + "glyphs": {c: GLYPHS[rows[c][1].get_selected()][0] for c in rows}, | |
| 108 | + "flip": flip.get_active(), | |
| 109 | + "time": TIME_CONTROLS[clock.get_selected()], | |
| 110 | + } | |
| 111 | + if self._busy(): | |
| 112 | + self.get_application().open_window(None, blank=True).start_duo(config) | |
| 113 | + else: | |
| 114 | + self.start_duo(config) | |
| 115 | + | |
| 116 | + dialog.connect("response", on_response) | |
| 117 | + dialog.present(self) | |
| 118 | + | |
| 119 | + # --- Ablauf ------------------------------------------------------------ | |
| 120 | + | |
| 121 | + def start_duo(self, config, board=None): | |
| 122 | + self._leave_special() | |
| 123 | + self.mode = "duo" | |
| 124 | + _label, minutes, increment = config["time"] | |
| 125 | + self.duo = { | |
| 126 | + "time": _label, | |
| 127 | + "hints": dict(config["hints"]), | |
| 128 | + "used": {chess.WHITE: 0, chess.BLACK: 0}, | |
| 129 | + "glyphs": dict(config["glyphs"]), | |
| 130 | + "flip": config["flip"], | |
| 131 | + "clock": None if minutes is None else { | |
| 132 | + "left": {chess.WHITE: minutes * 60000, chess.BLACK: minutes * 60000}, | |
| 133 | + "inc": increment * 1000, "running": None, "at": 0.0, | |
| 134 | + }, | |
| 135 | + } | |
| 136 | + self.board = board or chess.Board() | |
| 137 | + self.duo["first_turn"] = self.board.turn | |
| 138 | + self.human = self.board.turn | |
| 139 | + self._reset_game_tables() | |
| 140 | + self.save_name = "" | |
| 141 | + self.save_path = "" | |
| 142 | + self.view.flipped = False | |
| 143 | + self.list_btn.set_active(False) | |
| 144 | + self._duo_show_clock() | |
| 145 | + self._changed() | |
| 146 | + self._refresh() | |
| 147 | + | |
| 148 | + def _duo_leave(self): | |
| 149 | + if self.duo_tick: | |
| 150 | + GLib.source_remove(self.duo_tick) | |
| 151 | + self.duo_tick = 0 | |
| 152 | + if self.duo is not None: | |
| 153 | + self.view.clocks = self.view.clock_running = None | |
| 154 | + self.duo = None | |
| 155 | + self.view.turn_marker = None | |
| 156 | + | |
| 157 | + def _duo_play(self, move): | |
| 158 | + mover = self.board.turn | |
| 159 | + ply = len(self.board.move_stack) | |
| 160 | + before = self.board.copy() | |
| 161 | + key = annotate.key(before) | |
| 162 | + self.view.animate_move(self.board, move) | |
| 163 | + self.board.push(move) | |
| 164 | + self.browse_ply = None | |
| 165 | + self._duo_clock_after_move(mover) | |
| 166 | + self.human = self.board.turn | |
| 167 | + if self.duo["flip"]: | |
| 168 | + self.view.flipped = self.board.turn == chess.BLACK | |
| 169 | + self._changed() | |
| 170 | + self._refresh() | |
| 171 | + entry = self.analyses.get(key) | |
| 172 | + if entry and entry["depth"] >= annotate.MIN_DEPTH: | |
| 173 | + self._annotate(ply, move, before) | |
| 174 | + else: | |
| 175 | + self.pending_symbols[key] = (ply, move, before) | |
| 176 | + if self._over(): | |
| 177 | + self._duo_finish() | |
| 178 | + | |
| 179 | + def _duo_can_help(self): | |
| 180 | + """Hat der Spieler am Zug noch Hinweise übrig?""" | |
| 181 | + if self.duo is None: | |
| 182 | + return False | |
| 183 | + color = self.board.turn | |
| 184 | + return self.duo["used"][color] < self.duo["hints"][color] | |
| 185 | + | |
| 186 | + def _duo_spend_help(self): | |
| 187 | + self.duo["used"][self.board.turn] += 1 | |
| 188 | + | |
| 189 | + def _duo_badge(self): | |
| 190 | + """Zugzeichen für den Spieler am Zug, je nach seinem Handicap.""" | |
| 191 | + if self.duo is None: | |
| 192 | + return None | |
| 193 | + viewer = self.board.turn | |
| 194 | + ply = badge_ply(len(self.board.move_stack), viewer, self.duo["first_turn"], | |
| 195 | + self.duo["glyphs"][viewer], self.symbols) | |
| 196 | + if ply is None: | |
| 197 | + return None | |
| 198 | + return self.board.move_stack[ply].to_square, self.symbols[ply] | |
| 199 | + | |
| 200 | + def _duo_names(self): | |
| 201 | + names = {} | |
| 202 | + for color in (chess.WHITE, chess.BLACK): | |
| 203 | + text = COLOR_NAME[color] | |
| 204 | + budget = self.duo["hints"][color] | |
| 205 | + if budget: | |
| 206 | + left = budget - self.duo["used"][color] | |
| 207 | + text += f" · noch {left} Hinweis" + ("" if left == 1 else "e") | |
| 208 | + names[color] = text | |
| 209 | + return names | |
| 210 | + | |
| 211 | + def _duo_subtitle(self): | |
| 212 | + label = self.duo["time"] if self.duo else "Ohne Uhr" | |
| 213 | + parts = [] if self._over() else [f"{COLOR_NAME[self.board.turn]} am Zug"] | |
| 214 | + if label != "Ohne Uhr": | |
| 215 | + parts.append(label) | |
| 216 | + return " · ".join(parts) or "Ohne Uhr" | |
| 217 | + | |
| 218 | + def _duo_undo(self): | |
| 219 | + """Ohne Rückfrage einen Halbzug zurück – man sitzt ja zusammen.""" | |
| 220 | + if not self.board.move_stack: | |
| 221 | + return | |
| 222 | + self.board.pop() | |
| 223 | + self._truncate_extras(len(self.board.move_stack)) | |
| 224 | + self.badge_ply = None | |
| 225 | + self.human = self.board.turn | |
| 226 | + if self.duo["flip"]: | |
| 227 | + self.view.flipped = self.board.turn == chess.BLACK | |
| 228 | + clock = self.duo["clock"] | |
| 229 | + if clock is not None and clock["running"] is not None: | |
| 230 | + self._duo_clock_settle() | |
| 231 | + clock["running"] = self.board.turn if self.board.move_stack else None | |
| 232 | + self._changed() | |
| 233 | + self._refresh() | |
| 234 | + | |
| 235 | + def _duo_draw(self, *_): | |
| 236 | + if self.mode != "duo" or self._over(): | |
| 237 | + return | |
| 238 | + self.final_text = "Remis vereinbart." | |
| 239 | + self._duo_finish() | |
| 240 | + | |
| 241 | + def _duo_resign(self, *_): | |
| 242 | + if self.mode != "duo" or self._over(): | |
| 243 | + return | |
| 244 | + loser = self.board.turn | |
| 245 | + dialog = Adw.AlertDialog( | |
| 246 | + heading=f"{COLOR_NAME[loser]} gibt auf?", | |
| 247 | + body=f"Dann gewinnt {COLOR_NAME[not loser]}.", | |
| 248 | + ) | |
| 249 | + dialog.add_response("cancel", "Weiterspielen") | |
| 250 | + dialog.add_response("resign", "Aufgeben") | |
| 251 | + dialog.set_response_appearance("resign", Adw.ResponseAppearance.DESTRUCTIVE) | |
| 252 | + dialog.set_close_response("cancel") | |
| 253 | + | |
| 254 | + def on_response(_d, response): | |
| 255 | + if response == "resign" and self.mode == "duo" and not self._over(): | |
| 256 | + self.final_text = f"{COLOR_NAME[loser]} gibt auf – {COLOR_NAME[not loser]} gewinnt." | |
| 257 | + self._duo_finish() | |
| 258 | + | |
| 259 | + dialog.connect("response", on_response) | |
| 260 | + dialog.present(self) | |
| 261 | + | |
| 262 | + def _duo_finish(self): | |
| 263 | + """Partie vorbei: Uhr anhalten, als Analyse beider Seiten weiter.""" | |
| 264 | + clock = self.duo["clock"] if self.duo else None | |
| 265 | + if clock is not None and clock["running"] is not None: | |
| 266 | + self._duo_clock_settle() | |
| 267 | + clock["running"] = None | |
| 268 | + self.view.clock_running = None | |
| 269 | + if self.duo_tick: | |
| 270 | + GLib.source_remove(self.duo_tick) | |
| 271 | + self.duo_tick = 0 | |
| 272 | + self.mode = "review" | |
| 273 | + self.names = ("Weiß", "Schwarz") | |
| 274 | + self.view.turn_marker = None | |
| 275 | + self.human = chess.WHITE | |
| 276 | + self.end_shown = False | |
| 277 | + self._refresh() | |
| 278 | + self._analyze_review() | |
| 279 | + | |
| 280 | + # --- Uhr --------------------------------------------------------------- | |
| 281 | + | |
| 282 | + def _duo_show_clock(self): | |
| 283 | + clock = self.duo["clock"] | |
| 284 | + if clock is None: | |
| 285 | + self.view.clocks = self.view.clock_running = None | |
| 286 | + return | |
| 287 | + self.view.clocks = dict(clock["left"]) | |
| 288 | + self.view.clock_running = None | |
| 289 | + | |
| 290 | + def _duo_clock_settle(self): | |
| 291 | + """Verbrauchte Zeit der laufenden Uhr abbuchen.""" | |
| 292 | + clock = self.duo["clock"] | |
| 293 | + if clock["running"] is None: | |
| 294 | + return | |
| 295 | + now = time.monotonic() | |
| 296 | + clock["left"][clock["running"]] -= (now - clock["at"]) * 1000 | |
| 297 | + clock["at"] = now | |
| 298 | + | |
| 299 | + def _duo_clock_after_move(self, mover): | |
| 300 | + clock = self.duo["clock"] | |
| 301 | + if clock is None: | |
| 302 | + return | |
| 303 | + if clock["running"] is not None: | |
| 304 | + self._duo_clock_settle() | |
| 305 | + clock["left"][mover] += clock["inc"] | |
| 306 | + # die Uhr startet mit dem ersten Zug: ab dann läuft sie für den, der am Zug ist | |
| 307 | + clock["running"] = not mover | |
| 308 | + clock["at"] = time.monotonic() | |
| 309 | + if not self.duo_tick: | |
| 310 | + self.duo_tick = GLib.timeout_add(100, self._duo_clock_tick) | |
| 311 | + self._duo_clock_tick() | |
| 312 | + | |
| 313 | + def _duo_clock_tick(self): | |
| 314 | + clock = self.duo["clock"] if self.duo else None | |
| 315 | + if clock is None or clock["running"] is None or self.mode != "duo": | |
| 316 | + self.duo_tick = 0 | |
| 317 | + return False | |
| 318 | + running = clock["running"] | |
| 319 | + left = dict(clock["left"]) | |
| 320 | + left[running] -= (time.monotonic() - clock["at"]) * 1000 | |
| 321 | + self.view.clocks = left | |
| 322 | + self.view.clock_running = running | |
| 323 | + self.view.queue_draw() | |
| 324 | + if left[running] <= 0: | |
| 325 | + self._duo_clock_settle() | |
| 326 | + clock["left"][running] = 0 | |
| 327 | + self.view.clocks = dict(clock["left"]) | |
| 328 | + winner = not running | |
| 329 | + if self.board.has_insufficient_material(winner): | |
| 330 | + self.final_text = f"Zeit abgelaufen – Remis, {COLOR_NAME[winner]} kann nicht mehr mattsetzen." | |
| 331 | + else: | |
| 332 | + self.final_text = f"Zeit von {COLOR_NAME[running]} abgelaufen – {COLOR_NAME[winner]} gewinnt." | |
| 333 | + self.duo_tick = 0 | |
| 334 | + self._duo_finish() | |
| 335 | + return False | |
| 336 | + return True | |
added tests/test_duo.py +37 −0
| @@ -0,0 +1,37 @@ | ||
| 1 | +import unittest | |
| 2 | + | |
| 3 | +import chess | |
| 4 | + | |
| 5 | +from schach.duo import badge_ply | |
| 6 | + | |
| 7 | +W, B = chess.WHITE, chess.BLACK | |
| 8 | + | |
| 9 | + | |
| 10 | +class BadgeTest(unittest.TestCase): | |
| 11 | + # Halbzüge 0 (Weiß), 1 (Schwarz), 2 (Weiß), 3 (Schwarz); Weiß ist am Zug (4 Halbzüge gespielt) | |
| 12 | + symbols = {2: "?!", 3: "??"} | |
| 13 | + | |
| 14 | + def test_none_sees_nothing(self): | |
| 15 | + self.assertIsNone(badge_ply(4, W, W, "none", self.symbols)) | |
| 16 | + | |
| 17 | + def test_all_prefers_opponents_last_move(self): | |
| 18 | + self.assertEqual(badge_ply(4, W, W, "all", self.symbols), 3) | |
| 19 | + | |
| 20 | + def test_own_shows_own_last_move_only(self): | |
| 21 | + self.assertEqual(badge_ply(4, W, W, "own", self.symbols), 2) | |
| 22 | + self.assertIsNone(badge_ply(4, W, W, "own", {3: "??"})) | |
| 23 | + | |
| 24 | + def test_all_falls_back_to_own(self): | |
| 25 | + self.assertEqual(badge_ply(4, W, W, "all", {2: "?"}), 2) | |
| 26 | + | |
| 27 | + def test_black_starts_from_fen(self): | |
| 28 | + # erster Halbzug von Schwarz: Halbzug 0 = Schwarz, 1 = Weiß; Schwarz ist am Zug | |
| 29 | + self.assertEqual(badge_ply(2, B, B, "all", {1: "??"}), 1) | |
| 30 | + self.assertEqual(badge_ply(2, B, B, "own", {0: "!"}), 0) | |
| 31 | + | |
| 32 | + def test_empty_game(self): | |
| 33 | + self.assertIsNone(badge_ply(0, W, W, "all", {})) | |
| 34 | + | |
| 35 | + | |
| 36 | +if __name__ == "__main__": | |
| 37 | + unittest.main() | |