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>
| Changeset | e149876e2adacd9f1fd67105450b94d109436cf6 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | 45f827567c35 |
| Children | 31305090cbaa |
| Pushlog | by timo via ssh by timo via ssh |
modified schach/diagram.py +2 −0
| @@ -324,6 +324,8 @@ class BoardPainter: | ||
| 324 | 324 | self._square_tag(cr, hops[i], sq, SPOTLIGHT, hops[i - 1:i] + hops[i + 1:i + 2]) |
| 325 | 325 | if len(hops) == 1: |
| 326 | 326 | 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]) | |
| 327 | 329 | for squares, rgb in (((self.hint_from, self.hint_to), HINT), ((self.threat_from, self.threat_to), THREAT)): |
| 328 | 330 | shown = [s for s in squares if s is not None] |
| 329 | 331 | if shown: |
modified schach/training.py +15 −5
| @@ -80,15 +80,17 @@ def _due_text(course): | ||
| 80 | 80 | |
| 81 | 81 | PIECE_LETTERS = "KQRBNDTLS" # deutsch und englisch |
| 82 | 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 | + "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äöüß])") | |
| 85 | 86 | |
| 86 | 87 | |
| 87 | 88 | def squares_in(text): |
| 88 | 89 | """Feldnamen im Text: [(Anfang, Ende, Feld, Figur, Zusatz)]. |
| 89 | 90 | |
| 90 | 91 | 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“. | |
| 92 | 94 | """ |
| 93 | 95 | found = [] |
| 94 | 96 | for match in TOKEN_RE.finditer(text): |
| @@ -96,7 +98,11 @@ def squares_in(text): | ||
| 96 | 98 | before = text[start - 1] if start else " " |
| 97 | 99 | if before.isalnum() and not (match.group(1) is None and before == "x"): |
| 98 | 100 | 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)) | |
| 100 | 106 | return found |
| 101 | 107 | |
| 102 | 108 | |
| @@ -109,6 +115,10 @@ def mover(board, piece, extra, square): | ||
| 109 | 115 | kind = PIECE_TYPES.get(piece) |
| 110 | 116 | if kind is None: |
| 111 | 117 | 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 | |
| 112 | 122 | here = board.piece_at(square) |
| 113 | 123 | if here is not None and here.piece_type == kind: |
| 114 | 124 | 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 | ||
| 143 | 153 | """ |
| 144 | 154 | origin = mover(board, piece, extra, square) |
| 145 | 155 | 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: | |
| 147 | 157 | return origin, None |
| 148 | 158 | for _start, _end, via, other, other_extra in squares_in(text): |
| 149 | 159 | if via == square or PIECE_TYPES.get(other) != kind: |
modified tests/test_opening.py +9 −1
| @@ -209,7 +209,7 @@ class SquareNamesTest(unittest.TestCase) | ||
| 209 | 209 | text = "Lc4 zielt auf f7. Sxe5, Sbd2, exd5 und e4 – aber nicht Ende4 oder a10. Die f7-Schwäche!" |
| 210 | 210 | found = [(text[a:b], chess.square_name(sq), piece, extra) for a, b, sq, piece, extra in squares_in(text)] |
| 211 | 211 | 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, ""), | |
| 213 | 213 | ("f7", "f7", None, "")]) |
| 214 | 214 | |
| 215 | 215 | def test_mover_finds_the_piece(self): |
| @@ -226,6 +226,14 @@ class SquareNamesTest(unittest.TestCase) | ||
| 226 | 226 | self.assertEqual(mover(start, "S", "", chess.D2), chess.B1) # nur einer passt |
| 227 | 227 | self.assertIsNone(mover(start, "T", "", chess.D1)) # zwei Türme könnten: lieber kein Pfeil |
| 228 | 228 | |
| 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 | + | |
| 229 | 237 | def test_route_bridges_over_earlier_move(self): |
| 230 | 238 | from schach.training import route |
| 231 | 239 | start = chess.Board() |