Public repository: timo / schach

Reader

Ein aufgeräumtes Schach-Programm für GNOME

feat(coach): hover arrows for pawn captures, tag the origin square too

public

feat(coach): hover arrows for pawn captures, tag the origin square too

squares_in now reads 'bxc4' or 'exd5' as a whole move (piece 'P', extra = file),
so hovering anywhere on it works. mover finds the pawn on that file that
attacks the square, even when the square is empty (a parried threat).
The spotlight now tags both squares, like hint and threat.

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

modified schach/diagram.py +2 −0

@@ -324,6 +324,8 @@ class BoardPainter:
324324
                 self._square_tag(cr, hops[i], sq, SPOTLIGHT, hops[i - 1:i] + hops[i + 1:i + 2])
325325
             if len(hops) == 1:
326326
                 self._square_tag(cr, self.spotlight, sq, SPOTLIGHT)
327
+            else:  # beide Felder benannt, wie beim Hinweis: auch das Startfeld bekommt ein Schild
328
+                self._square_tag(cr, hops[0], sq, SPOTLIGHT, hops[1:2])
327329
         for squares, rgb in (((self.hint_from, self.hint_to), HINT), ((self.threat_from, self.threat_to), THREAT)):
328330
             shown = [s for s in squares if s is not None]
329331
             if shown:

modified schach/training.py +15 −5

@@ -80,15 +80,17 @@ def _due_text(course):
8080
 
8181
 PIECE_LETTERS = "KQRBNDTLS"  # deutsch und englisch
8282
 PIECE_TYPES = {"K": chess.KING, "D": chess.QUEEN, "Q": chess.QUEEN, "T": chess.ROOK, "R": chess.ROOK,
83
-               "L": chess.BISHOP, "B": chess.BISHOP, "S": chess.KNIGHT, "N": chess.KNIGHT}
84
-TOKEN_RE = re.compile(r"(?:([KQRBNDTLS])([a-h1-8]?)x?)?([a-h][1-8])(?![0-9A-Za-zäöüß])")
83
+               "L": chess.BISHOP, "B": chess.BISHOP, "S": chess.KNIGHT, "N": chess.KNIGHT,
84
+               "P": chess.PAWN}  # „P“ steht nie im Text: squares_in setzt es für Bauernschläge wie „bxc4“
85
+TOKEN_RE = re.compile(r"(?:([KQRBNDTLS])([a-h1-8]?)x?|([a-h])x)?([a-h][1-8])(?![0-9A-Za-zäöüß])")
8586
 
8687
 
8788
 def squares_in(text):
8889
     """Feldnamen im Text: [(Anfang, Ende, Feld, Figur, Zusatz)].
8990
 
9091
     Mit Figur gehört der ganze Zug dazu: „Lc4“, „Sxe5“, „Sbd2“ (Figur „S“, Zusatz „b“).
91
-    Ohne Figur nur das Feld: „e4“, „f7-Schwäche“, „exd5“ – aber nicht „Ende4“.
92
+    Bauernschlag ebenso: „bxc4“ (Figur „P“, Zusatz „b“). Sonst nur das Feld: „e4“, „f7-Schwäche“ –
93
+    aber nicht „Ende4“.
9294
     """
9395
     found = []
9496
     for match in TOKEN_RE.finditer(text):
@@ -96,7 +98,11 @@ def squares_in(text):
9698
         before = text[start - 1] if start else " "
9799
         if before.isalnum() and not (match.group(1) is None and before == "x"):
98100
             continue
99
-        found.append((start, match.end(), chess.parse_square(match.group(3)), match.group(1), match.group(2) or ""))
101
+        if match.group(3):
102
+            piece, extra = "P", match.group(3)
103
+        else:
104
+            piece, extra = match.group(1), match.group(2) or ""
105
+        found.append((start, match.end(), chess.parse_square(match.group(4)), piece, extra))
100106
     return found
101107
 
102108
 
@@ -109,6 +115,10 @@ def mover(board, piece, extra, square):
109115
     kind = PIECE_TYPES.get(piece)
110116
     if kind is None:
111117
         return None
118
+    if kind == chess.PAWN:  # „bxc4“: der Bauer auf der b-Linie, der c4 angreift (auch wenn c4 leer ist)
119
+        pawns = [s for color in (board.turn, not board.turn) for s in board.attackers(color, square)
120
+                 if board.piece_type_at(s) == chess.PAWN and chess.square_name(s)[0] == extra]
121
+        return pawns[0] if pawns else None
112122
     here = board.piece_at(square)
113123
     if here is not None and here.piece_type == kind:
114124
         return None  # der Zug ist schon gespielt: das Feld leuchtet, die Figur steht ja dort
@@ -143,7 +153,7 @@ def route(board, text, piece, extra, squ
143153
     """
144154
     origin = mover(board, piece, extra, square)
145155
     kind = PIECE_TYPES.get(piece)
146
-    if origin is not None or kind is None or board.piece_type_at(square) == kind:
156
+    if origin is not None or kind in (None, chess.PAWN) or board.piece_type_at(square) == kind:
147157
         return origin, None
148158
     for _start, _end, via, other, other_extra in squares_in(text):
149159
         if via == square or PIECE_TYPES.get(other) != kind:

modified tests/test_opening.py +9 −1

@@ -209,7 +209,7 @@ class SquareNamesTest(unittest.TestCase)
209209
         text = "Lc4 zielt auf f7. Sxe5, Sbd2, exd5 und e4 – aber nicht Ende4 oder a10. Die f7-Schwäche!"
210210
         found = [(text[a:b], chess.square_name(sq), piece, extra) for a, b, sq, piece, extra in squares_in(text)]
211211
         self.assertEqual(found, [("Lc4", "c4", "L", ""), ("f7", "f7", None, ""), ("Sxe5", "e5", "S", ""),
212
-                                 ("Sbd2", "d2", "S", "b"), ("d5", "d5", None, ""), ("e4", "e4", None, ""),
212
+                                 ("Sbd2", "d2", "S", "b"), ("exd5", "d5", "P", "e"), ("e4", "e4", None, ""),
213213
                                  ("f7", "f7", None, "")])
214214
 
215215
     def test_mover_finds_the_piece(self):
@@ -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_mover_finds_capturing_pawn(self):
230
+        from schach.training import mover, route
231
+        # nach 7. Lb3: c4 ist leer, die Drohung bxc4 zeigt trotzdem vom Bauern b5 aus
232
+        board = chess.Board("rn1qkb1r/5ppp/2pp1n2/1p2p3/4P1b1/1B1P1N2/PPP2PPP/RNBQR1K1 b kq - 1 7")
233
+        self.assertEqual(mover(board, "P", "b", chess.C4), chess.B5)
234
+        self.assertEqual(route(board, "Das wehrt die Drohung bxc4 ab.", "P", "b", chess.C4), (chess.B5, None))
235
+        self.assertIsNone(mover(board, "P", "e", chess.C4))  # auf der e-Linie greift kein Bauer c4 an
236
+
229237
     def test_route_bridges_over_earlier_move(self):
230238
         from schach.training import route
231239
         start = chess.Board()