Public repository: timo / schach

Reader

Ein aufgeräumtes Schach-Programm für GNOME

feat(coach): bridge hover arrows via earlier move in text

public

feat(coach): bridge hover arrows via earlier move in text

Se5 unreachable from start: if the text names Sf3 and a knight
on f3 reaches e5, draw g1→f3→e5 with tags on both squares.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Changeset72385b734fb50b363f1c2d5648c9572bdff1a70f
Author (claimed)Timo Stolz <timo.stolz@nullachtvierzehn.de>,
Parents4e35d08bc0a1
Children0c383647c00c
Pushlog
by timo via ssh
by timo via ssh

modified schach/diagram.py +19 −17

@@ -153,6 +153,7 @@ class BoardPainter:
153153
         self.turn_marker = None  # Farbe, deren Name fett steht (wer am Zug ist, bei zwei Spielern)
154154
         self.spotlight = None  # Feld, das gerade im Text gezeigt wird (Maus über „e4“)
155155
         self.spotlight_from = None  # bei „Ld3“: der Läufer, der dorthin ziehen kann (Pfeil)
156
+        self.spotlight_via = None  # bei „Se5“ nach „Sf3“ im Text: Zwischenfeld, zwei Pfeile
156157
         self.shapes = ()  # Pfeile und Felder aus Kurskommentaren: ((von, nach, Farbe), …)
157158
         self.preview = None  # Zug, der halb durchsichtig gezeigt wird (Explorer: Maus über einer Zeile)
158159
         self.editing = False  # Stellung aufbauen: keine geschlagenen Figuren anzeigen
@@ -295,9 +296,13 @@ class BoardPainter:
295296
         if self.threat_from is not None and self.threat_to is not None:
296297
             self._draw_arrow(cr, self.threat_from, self.threat_to, sq, THREAT)
297298
         if self.spotlight is not None:
298
-            if self.spotlight_from is not None:
299
-                self._draw_arrow(cr, self.spotlight_from, self.spotlight, sq, SPOTLIGHT, outline=True)
300
-            self._square_tag(cr, self.spotlight, sq, SPOTLIGHT, self.spotlight_from)
299
+            hops = [s for s in (self.spotlight_from, self.spotlight_via) if s is not None] + [self.spotlight]
300
+            for a, b in zip(hops, hops[1:]):
301
+                self._draw_arrow(cr, a, b, sq, SPOTLIGHT, outline=True)
302
+            for i in range(1, len(hops)):  # Ziel und Zwischenfeld bekommen ein Schild, jedes neben seinen Pfeilen
303
+                self._square_tag(cr, hops[i], sq, SPOTLIGHT, hops[i - 1:i] + hops[i + 1:i + 2])
304
+            if len(hops) == 1:
305
+                self._square_tag(cr, self.spotlight, sq, SPOTLIGHT)
301306
         for tail, head, color in self.shapes:
302307
             rgb = SHAPE_COLORS.get(color, SHAPE_COLORS["green"])
303308
             if tail == head:
@@ -345,11 +350,12 @@ class BoardPainter:
345350
         cr.rectangle(x + inset, y + inset, sq - 2 * inset, sq - 2 * inset)
346351
         cr.stroke()
347352
 
348
-    def _square_tag(self, cr, square, sq, rgb, came_from=None):
353
+    def _square_tag(self, cr, square, sq, rgb, arrows=()):
349354
         """Zielfeld mit Namensschild („c4“): die Figur bleibt frei, der Name steht da.
350355
 
351356
         Am liebsten oben rechts – bündig mit den Ziffern oben und den Buchstaben rechts am Brettrand.
352
-        Schneidet der Pfeil dort, dann unten rechts, oben links, zuletzt unten links.
357
+        Schneidet ein Pfeil dort, dann unten rechts, oben links, zuletzt unten links.
358
+        ``arrows``: Felder, zu denen oder von denen ein Pfeil an diesem Feld hängt.
353359
         """
354360
         x, y = self._square_origin(square)
355361
         text = chess.square_name(square)
@@ -362,18 +368,14 @@ class BoardPainter:
362368
         inset = sq * 0.04
363369
         corners = [(x + sq - w - inset, y + inset), (x + sq - w - inset, y + sq - h - inset),
364370
                    (x + inset, y + inset), (x + inset, y + sq - h - inset)]
365
-        left, top = corners[0]
366
-        if came_from is not None:
367
-            fx, fy = self._square_origin(came_from)
368
-            shaft = [(x + sq / 2 + (fx - x) * t / 20, y + sq / 2 + (fy - y) * t / 20) for t in range(21)]
369
-            margin = sq * 0.06  # halber Schaft mit Rand
370
-            for cx, cy in corners:
371
-                if not any(cx - margin < px < cx + w + margin and cy - margin < py < cy + h + margin
372
-                           for px, py in shaft):
373
-                    left, top = cx, cy
374
-                    break
375
-            else:
376
-                left, top = corners[-1]
371
+        shaft = []
372
+        for other in arrows:
373
+            fx, fy = self._square_origin(other)
374
+            shaft += [(x + sq / 2 + (fx - x) * t / 20, y + sq / 2 + (fy - y) * t / 20) for t in range(21)]
375
+        margin = sq * 0.06  # halber Schaft mit Rand
376
+        left, top = next(((cx, cy) for cx, cy in corners
377
+                          if not any(cx - margin < px < cx + w + margin and cy - margin < py < cy + h + margin
378
+                                     for px, py in shaft)), corners[-1])
377379
         r = h * 0.3
378380
         cr.new_path()
379381
         cr.arc(left + w - r, top + r, r, -math.pi / 2, 0)

modified schach/training.py +30 −6

@@ -135,6 +135,29 @@ def mover(board, piece, extra, square):
135135
     return None
136136
 
137137
 
138
+def route(board, text, piece, extra, square):
139
+    """Pfeilweg zu „Se5“: (Start, Zwischenfeld) – Zwischenfeld None, wenn es direkt geht.
140
+
141
+    Geht es nicht direkt, dient ein anderer Zug derselben Figurenart im Text als Brücke:
142
+    „Sf3 … Se5“ zeigt g1→f3→e5. Ohne passende Brücke (None, None).
143
+    """
144
+    origin = mover(board, piece, extra, square)
145
+    kind = PIECE_TYPES.get(piece)
146
+    if origin is not None or kind is None or board.piece_type_at(square) == kind:
147
+        return origin, None
148
+    for _start, _end, via, other, other_extra in squares_in(text):
149
+        if via == square or PIECE_TYPES.get(other) != kind:
150
+            continue
151
+        start = mover(board, other, other_extra, via)
152
+        if start is None:
153
+            continue
154
+        hop = chess.BaseBoard(None)
155
+        hop.set_piece_at(via, chess.Piece(kind, board.color_at(start)))
156
+        if hop.is_attacked_by(board.color_at(start), square):
157
+            return start, via
158
+    return None, None
159
+
160
+
138161
 TAG_RE = re.compile(r"(<[^>]*>)")
139162
 
140163
 
@@ -230,14 +253,15 @@ class OpeningMixin:
230253
         return self.coach
231254
 
232255
     def _coach_spotlight(self, hit):
233
-        """``hit`` aus square_at: Feld leuchtet; bei „Ld3“ zusätzlich ein Pfeil vom Läufer, der dorthin kann."""
234
-        square = origin = None
256
+        """``hit`` aus square_at: Feld leuchtet; bei „Ld3“ ein Pfeil vom Läufer, bei „Se5“ evtl. über „Sf3“."""
257
+        square = origin = via = None
235258
         if hit is not None:
236259
             square, piece, extra = hit
237
-            origin = mover(self.view.board, piece, extra, square)
238
-        if (self.view.spotlight, self.view.spotlight_from) != (square, origin):
239
-            self.view.spotlight, self.view.spotlight_from = square, origin
240
-            self.view.queue_draw()
260
+            origin, via = route(self.view.board, self.coach_label.get_layout().get_text(), piece, extra, square)
261
+        v = self.view
262
+        if (v.spotlight, v.spotlight_from, v.spotlight_via) != (square, origin, via):
263
+            v.spotlight, v.spotlight_from, v.spotlight_via = square, origin, via
264
+            v.queue_draw()
241265
 
242266
     def _coach(self, markup, ply=None, replace=False, shapes=(), demos=()):
243267
         """Neue Rückmeldung zur Stellung nach Halbzug ``ply`` (Standard: aktuelle Stellung).

modified tests/test_opening.py +8 −0

@@ -226,6 +226,14 @@ class SquareNamesTest(unittest.TestCase)
226226
         self.assertEqual(mover(start, "S", "", chess.D2), chess.B1)  # nur einer passt
227227
         self.assertIsNone(mover(start, "T", "", chess.D1))  # zwei Türme könnten: lieber kein Pfeil
228228
 
229
+    def test_route_bridges_over_earlier_move(self):
230
+        from schach.training import route
231
+        start = chess.Board()
232
+        text = "Aufbau d4–Lf4–e3–Sf3–c3–Ld3–Sbd2, dann e4 oder Se5."
233
+        self.assertEqual(route(start, text, "S", "", chess.E5), (chess.G1, chess.F3))  # g1→f3→e5
234
+        self.assertEqual(route(start, text, "L", "", chess.F4), (chess.C1, None))  # geht direkt
235
+        self.assertEqual(route(start, "Plan: Se5", "S", "", chess.E5), (None, None))  # keine Brücke im Text
236
+
229237
     def test_mark_squares_leaves_tags_alone(self):
230238
         from schach.training import mark_squares
231239
         out = mark_squares("<span alpha='70%'>Warum?</span>\n<b>3. Lc4</b> – auf f7")