feat(coach): hovering a piece move points from its piece
public
feat(coach): hovering a piece move points from its piece "Ld3" draws a gold arrow from the bishop that can go there; if it already stands there, only the square lights up. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | 9c1385fb03b50c33e0f79fe7e2c36766c77e9fe8 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | 4e53dc7c4e10 |
| Children | 3dfaecc53bd0 |
| Pushlog | by timo via ssh by timo via ssh |
modified schach/diagram.py +7 −1
| @@ -152,6 +152,7 @@ class BoardPainter: | ||
| 152 | 152 | self.clock_running = None # Farbe, deren Uhr gerade läuft |
| 153 | 153 | self.turn_marker = None # Farbe, deren Name fett steht (wer am Zug ist, bei zwei Spielern) |
| 154 | 154 | self.spotlight = None # Feld, das gerade im Text gezeigt wird (Maus über „e4“) |
| 155 | + self.spotlight_from = None # bei „Ld3“: der Läufer, der dorthin ziehen kann (Pfeil) | |
| 155 | 156 | self.shapes = () # Pfeile und Felder aus Kurskommentaren: ((von, nach, Farbe), …) |
| 156 | 157 | self.preview = None # Zug, der halb durchsichtig gezeigt wird (Explorer: Maus über einer Zeile) |
| 157 | 158 | self.editing = False # Stellung aufbauen: keine geschlagenen Figuren anzeigen |
| @@ -244,7 +245,10 @@ class BoardPainter: | ||
| 244 | 245 | for s in (self.threat_from, self.threat_to): |
| 245 | 246 | if s is not None: |
| 246 | 247 | self._mark(cr, s, sq, THREAT) |
| 247 | - if self.spotlight is not None: | |
| 248 | + if self.spotlight is not None and self.spotlight_from is not None: | |
| 249 | + self._glow(cr, self.spotlight_from, sq, SPOTLIGHT, 0.8) | |
| 250 | + self._mark(cr, self.spotlight, sq, SPOTLIGHT) | |
| 251 | + elif self.spotlight is not None: | |
| 248 | 252 | # Spalte und Zeile blass, das Feld im Schnittpunkt kräftig: so liest man Koordinaten |
| 249 | 253 | file, rank = chess.square_file(self.spotlight), chess.square_rank(self.spotlight) |
| 250 | 254 | cr.set_source_rgba(*SPOTLIGHT, 0.22) |
| @@ -292,6 +296,8 @@ class BoardPainter: | ||
| 292 | 296 | |
| 293 | 297 | if self.threat_from is not None and self.threat_to is not None: |
| 294 | 298 | self._draw_arrow(cr, self.threat_from, self.threat_to, sq, THREAT) |
| 299 | + if self.spotlight is not None and self.spotlight_from is not None: | |
| 300 | + self._draw_arrow(cr, self.spotlight_from, self.spotlight, sq, SPOTLIGHT) | |
| 295 | 301 | for tail, head, color in self.shapes: |
| 296 | 302 | rgb = SHAPE_COLORS.get(color, SHAPE_COLORS["green"]) |
| 297 | 303 | if tail == head: |
modified schach/training.py +47 −15
| @@ -79,22 +79,49 @@ def _due_text(course): | ||
| 79 | 79 | |
| 80 | 80 | |
| 81 | 81 | PIECE_LETTERS = "KQRBNDTLS" # deutsch und englisch |
| 82 | -SQUARE_RE = re.compile(r"[a-h][1-8](?![0-9A-Za-zäöüß])") | |
| 82 | +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 | 85 | |
| 84 | 86 | |
| 85 | 87 | def squares_in(text): |
| 86 | - """Feldnamen im Text: [(Anfang, Ende, Feld)]. „e4“, „Lc4“, „Sxe5“, „Sbd2“ – aber nicht „Ende4“.""" | |
| 88 | + """Feldnamen im Text: [(Anfang, Ende, Feld, Figur, Zusatz)]. | |
| 89 | + | |
| 90 | + 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 | + """ | |
| 87 | 93 | found = [] |
| 88 | - for match in SQUARE_RE.finditer(text): | |
| 94 | + for match in TOKEN_RE.finditer(text): | |
| 89 | 95 | start = match.start() |
| 90 | 96 | before = text[start - 1] if start else " " |
| 91 | - if before in "abcdefgh": # nur als Zusatz wie in „Sbd2“: davor muss eine Figur stehen | |
| 92 | - if start < 2 or text[start - 2] not in PIECE_LETTERS: | |
| 97 | + if before.isalnum() and not (match.group(1) is None and before == "x"): | |
| 98 | + continue | |
| 99 | + found.append((start, match.end(), chess.parse_square(match.group(3)), match.group(1), match.group(2) or "")) | |
| 100 | + return found | |
| 101 | + | |
| 102 | + | |
| 103 | +def mover(board, piece, extra, square): | |
| 104 | + """Von wo zieht die Figur aus „Ld3“ nach ``square``? None, wenn sie schon dort steht oder keine kann. | |
| 105 | + | |
| 106 | + Zuerst die Seite am Zug, dann die andere (Kommentare sprechen über beide). | |
| 107 | + """ | |
| 108 | + kind = PIECE_TYPES.get(piece) | |
| 109 | + if kind is None: | |
| 110 | + return None | |
| 111 | + here = board.piece_at(square) | |
| 112 | + if here is not None and here.piece_type == kind: | |
| 113 | + return None # der Zug ist schon gespielt: das Feld leuchtet, die Figur steht ja dort | |
| 114 | + for color in (board.turn, not board.turn): | |
| 115 | + probe = board.copy(stack=False) | |
| 116 | + probe.turn = color | |
| 117 | + for move in probe.pseudo_legal_moves: | |
| 118 | + if move.to_square != square or probe.piece_type_at(move.from_square) != kind: | |
| 93 | 119 | continue |
| 94 | - elif before.isalnum() and before not in PIECE_LETTERS + "x": | |
| 95 | - continue | |
| 96 | - found.append((match.start(), match.end(), chess.parse_square(match.group()))) | |
| 97 | - return found | |
| 120 | + name = chess.square_name(move.from_square) | |
| 121 | + if extra and extra not in name: | |
| 122 | + continue | |
| 123 | + return move.from_square | |
| 124 | + return None | |
| 98 | 125 | |
| 99 | 126 | |
| 100 | 127 | TAG_RE = re.compile(r"(<[^>]*>)") |
| @@ -105,7 +132,7 @@ def mark_squares(markup): | ||
| 105 | 132 | parts = TAG_RE.split(markup) |
| 106 | 133 | for i in range(0, len(parts), 2): # gerade Stellen sind Text, ungerade Tags |
| 107 | 134 | text = parts[i] |
| 108 | - for start, end, _sq in reversed(squares_in(text)): | |
| 135 | + for start, end, *_rest in reversed(squares_in(text)): | |
| 109 | 136 | text = (f"{text[:start]}<i>{text[start:end]}</i>" |
| 110 | 137 | f"{text[end:]}") |
| 111 | 138 | parts[i] = text |
| @@ -113,7 +140,7 @@ def mark_squares(markup): | ||
| 113 | 140 | |
| 114 | 141 | |
| 115 | 142 | def square_at(label, x, y): |
| 116 | - """Feld, dessen Name im Label an der Stelle (x, y) steht – oder None.""" | |
| 143 | + """Feldname im Label an der Stelle (x, y): (Feld, Figur, Zusatz) – oder None.""" | |
| 117 | 144 | layout = label.get_layout() |
| 118 | 145 | ox, oy = label.get_layout_offsets() |
| 119 | 146 | inside, index, _trailing = layout.xy_to_index(int((x - ox) * Pango.SCALE), int((y - oy) * Pango.SCALE)) |
| @@ -121,7 +148,7 @@ def square_at(label, x, y): | ||
| 121 | 148 | return None |
| 122 | 149 | text = layout.get_text() |
| 123 | 150 | char = len(text.encode()[:index].decode(errors="ignore")) |
| 124 | - return next((square for start, end, square in squares_in(text) if start <= char < end), None) | |
| 151 | + return next((tuple(hit[2:]) for hit in squares_in(text) if hit[0] <= char < hit[1]), None) | |
| 125 | 152 | |
| 126 | 153 | |
| 127 | 154 | class OpeningMixin: |
| @@ -191,9 +218,14 @@ class OpeningMixin: | ||
| 191 | 218 | self.coach = Gtk.Revealer(child=card, transition_type=Gtk.RevealerTransitionType.SLIDE_UP) |
| 192 | 219 | return self.coach |
| 193 | 220 | |
| 194 | - def _coach_spotlight(self, square): | |
| 195 | - if self.view.spotlight != square: | |
| 196 | - self.view.spotlight = square | |
| 221 | + def _coach_spotlight(self, hit): | |
| 222 | + """``hit`` aus square_at: Feld leuchtet; bei „Ld3“ zusätzlich ein Pfeil vom Läufer, der dorthin kann.""" | |
| 223 | + square = origin = None | |
| 224 | + if hit is not None: | |
| 225 | + square, piece, extra = hit | |
| 226 | + origin = mover(self.view.board, piece, extra, square) | |
| 227 | + if (self.view.spotlight, self.view.spotlight_from) != (square, origin): | |
| 228 | + self.view.spotlight, self.view.spotlight_from = square, origin | |
| 197 | 229 | self.view.queue_draw() |
| 198 | 230 | |
| 199 | 231 | def _coach(self, markup, ply=None, replace=False, shapes=(), demos=()): |
modified tests/test_opening.py +16 −5
| @@ -206,14 +206,25 @@ class BooksTest(unittest.TestCase): | ||
| 206 | 206 | class SquareNamesTest(unittest.TestCase): |
| 207 | 207 | def test_squares_in_comment(self): |
| 208 | 208 | from schach.training import squares_in |
| 209 | - text = "Lc4 zielt auf f7. Sxe5, Sbd2 und e4 – aber nicht Ende4 oder a10. Die f7-Schwäche!" | |
| 210 | - found = [(text[a:b], chess.square_name(sq)) for a, b, sq in squares_in(text)] | |
| 211 | - self.assertEqual(found, [("c4", "c4"), ("f7", "f7"), ("e5", "e5"), ("d2", "d2"), ("e4", "e4"), | |
| 212 | - ("f7", "f7")]) | |
| 209 | + text = "Lc4 zielt auf f7. Sxe5, Sbd2, exd5 und e4 – aber nicht Ende4 oder a10. Die f7-Schwäche!" | |
| 210 | + found = [(text[a:b], chess.square_name(sq), piece, extra) for a, b, sq, piece, extra in squares_in(text)] | |
| 211 | + self.assertEqual(found, [("Lc4", "c4", "L", ""), ("f7", "f7", None, ""), ("Sxe5", "e5", "S", ""), | |
| 212 | + ("Sbd2", "d2", "S", "b"), ("d5", "d5", None, ""), ("e4", "e4", None, ""), | |
| 213 | + ("f7", "f7", None, "")]) | |
| 214 | + | |
| 215 | + def test_mover_finds_the_piece(self): | |
| 216 | + from schach.training import mover | |
| 217 | + board = chess.Board() | |
| 218 | + for san in ("e4", "e5", "Nf3", "Nc6"): | |
| 219 | + board.push_san(san) | |
| 220 | + self.assertEqual(mover(board, "L", "", chess.C4), chess.F1) # Lc4 kommt noch: Pfeil von f1 | |
| 221 | + self.assertEqual(mover(board, "S", "", chess.F6), chess.G8) # auch für die andere Seite | |
| 222 | + self.assertIsNone(mover(board, "S", "", chess.F3)) # steht schon dort | |
| 223 | + self.assertIsNone(mover(board, None, "", chess.E4)) # nur ein Feld | |
| 213 | 224 | |
| 214 | 225 | def test_mark_squares_leaves_tags_alone(self): |
| 215 | 226 | from schach.training import mark_squares |
| 216 | 227 | out = mark_squares("<span alpha='70%'>Warum?</span>\n<b>3. Lc4</b> – auf f7") |
| 217 | 228 | self.assertIn("<span alpha='70%'>Warum?</span>", out) |
| 218 | 229 | self.assertEqual(out.count("<i>"), 2) |
| 219 | - self.assertIn("<i>c4</i></b>", out) | |
| 230 | + self.assertIn("<i>Lc4</i></b>", out) | |