feat(notation): make move notation selectable and consistent
public
feat(notation): make move notation selectable and consistent German (Sf3) or English (Nf3) piece letters in move list, training coach, subtitles and opening names; German moves inside course comments are translated for English. PGN stays international. Clearer coach tip: the hint (H) shows the course move. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | a7ec0edb3ebbba011f7eaa7cdc8447c18d509a96 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | e41ec0ae0a80 |
| Children | 7f83afced4ef |
| Pushlog | by timo via ssh by timo via ssh |
modified README.md +2 −1
| @@ -49,7 +49,8 @@ Schlichtes GNOME-Schachprogramm (Python, | ||
| 49 | 49 | Partie, öffnet sich ein neues. Fremde Partien zeigen die Namen und werden für beide |
| 50 | 50 | Seiten analysiert. Während einer Online-Partie ist Einfügen gesperrt. |
| 51 | 51 | - **Einstellungen** (Strg+,): Standardfarbe, Streuung von Maia, Brettfarben (Holz, |
| 52 | - Schiefer, Grün), Figurensätze aus PyChess (`/usr/share/pychess/pieces`), Lichess-Konto. | |
| 52 | + Schiefer, Grün), Notation (deutsch Sf3 oder englisch Nf3 – gilt für Zugliste und | |
| 53 | + Erklärungen; PGN-Dateien bleiben international), Figurensätze aus PyChess (`/usr/share/pychess/pieces`), Lichess-Konto. | |
| 53 | 54 | |
| 54 | 55 | ## Starten |
| 55 | 56 | |
modified schach/app.py +12 −4
| @@ -15,7 +15,7 @@ gi.require_version("Gtk", "4.0") | ||
| 15 | 15 | gi.require_version("Adw", "1") |
| 16 | 16 | from gi.repository import Adw, Gdk, Gio, GLib, GObject, Gtk # noqa: E402 |
| 17 | 17 | |
| 18 | -from . import annotate, clipboard, lichess, savegames # noqa: E402 | |
| 18 | +from . import annotate, clipboard, lichess, notation, savegames # noqa: E402 | |
| 19 | 19 | from .board import BoardView, available_themes # noqa: E402 |
| 20 | 20 | from .movelist import MoveList # noqa: E402 |
| 21 | 21 | from .prefs import Preferences # noqa: E402 |
| @@ -49,7 +49,7 @@ SETTINGS_PATH = os.path.join(GLib.get_us | ||
| 49 | 49 | DEFAULTS = { |
| 50 | 50 | "elo": 1200, "color": "white", "theme": "cburnett", "puzzle_difficulty": "normal", |
| 51 | 51 | "temperature": OPPONENT_TEMPERATURE, "scheme": "holz", "hint_helper": DEFAULT_HELPER, |
| 52 | - "threat_source": "opponent", | |
| 52 | + "threat_source": "opponent", "notation": notation.DEFAULT, | |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | |
| @@ -272,6 +272,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 272 | 272 | box.append(self._build_coach()) |
| 273 | 273 | |
| 274 | 274 | self.movelist = MoveList() |
| 275 | + self.movelist.notation = self._notation() | |
| 275 | 276 | self.movelist.on_select = self._browse |
| 276 | 277 | self.movelist.on_copy = self._copy_fen |
| 277 | 278 | self.movelist.on_continue = self._continue_from |
| @@ -513,7 +514,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 513 | 514 | mover = shown.copy() |
| 514 | 515 | last = mover.pop() |
| 515 | 516 | number = f"{mover.fullmove_number}." if mover.turn == chess.WHITE else f"{mover.fullmove_number}…" |
| 516 | - self.title.set_subtitle(f"Ansicht: Stellung nach {number} {mover.san(last)}") | |
| 517 | + self.title.set_subtitle(f"Ansicht: Stellung nach {number} {notation.move(mover, last, self._notation())}") | |
| 517 | 518 | elif self.mode == "puzzle": |
| 518 | 519 | self.title.set_subtitle(self._puzzle_subtitle()) |
| 519 | 520 | elif self.mode == "opening": |
| @@ -1155,7 +1156,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1155 | 1156 | board = state.board.copy() |
| 1156 | 1157 | last = board.pop() |
| 1157 | 1158 | number = f"{board.fullmove_number}." if board.turn == chess.WHITE else f"{board.fullmove_number}…" |
| 1158 | - state.name = f"Abzweigung nach {number} {board.san(last)}" | |
| 1159 | + state.name = f"Abzweigung nach {number} {notation.move(board, last, self._notation())}" | |
| 1159 | 1160 | self.get_application().open_window(state) |
| 1160 | 1161 | |
| 1161 | 1162 | def _set_symbol(self, ply, move, symbol): |
| @@ -1170,6 +1171,10 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1170 | 1171 | self.view.flipped = not self.view.flipped |
| 1171 | 1172 | self.view.queue_draw() |
| 1172 | 1173 | |
| 1174 | + def _notation(self): | |
| 1175 | + style = self.settings.get("notation", notation.DEFAULT) | |
| 1176 | + return style if style in notation.STYLES else notation.DEFAULT | |
| 1177 | + | |
| 1173 | 1178 | def _helper(self): |
| 1174 | 1179 | helper = self.settings.get("hint_helper", DEFAULT_HELPER) |
| 1175 | 1180 | return helper if helper in HELPERS else DEFAULT_HELPER |
| @@ -1193,6 +1198,9 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1193 | 1198 | self.view.set_theme(value) |
| 1194 | 1199 | elif key == "scheme": |
| 1195 | 1200 | self.view.set_scheme(value) |
| 1201 | + elif key == "notation": | |
| 1202 | + self.movelist.notation = self._notation() | |
| 1203 | + self._refresh() | |
| 1196 | 1204 | |
| 1197 | 1205 | def _preferences_dialog(self, *_): |
| 1198 | 1206 | if self.prefs_dialog is not None: |
modified schach/movelist.py +4 −2
| @@ -6,6 +6,7 @@ import gi | ||
| 6 | 6 | gi.require_version("Gtk", "4.0") |
| 7 | 7 | from gi.repository import Gtk # noqa: E402 |
| 8 | 8 | |
| 9 | +from . import notation # noqa: E402 | |
| 9 | 10 | from .board import BADGE # noqa: E402 |
| 10 | 11 | |
| 11 | 12 | CSS = b""" |
| @@ -54,6 +55,7 @@ class MoveList(Gtk.Box): | ||
| 54 | 55 | self.scroller.set_child(self.listbox) |
| 55 | 56 | self.append(self.scroller) |
| 56 | 57 | |
| 58 | + self.notation = "de" # Schreibweise der Züge, siehe notation.py | |
| 57 | 59 | self._rows = [] |
| 58 | 60 | self._selected = None |
| 59 | 61 | self._signature = None |
| @@ -64,7 +66,7 @@ class MoveList(Gtk.Box): | ||
| 64 | 66 | def update(self, board, evals, symbols, selected): |
| 65 | 67 | """Baut die Liste neu, wenn sich Züge, Bewertungen oder Zeichen geändert haben.""" |
| 66 | 68 | moves = list(board.move_stack) |
| 67 | - signature = (tuple(moves), tuple(sorted(evals.items())), tuple(sorted(symbols.items()))) | |
| 69 | + signature = (tuple(moves), tuple(sorted(evals.items())), tuple(sorted(symbols.items())), self.notation) | |
| 68 | 70 | if signature != self._signature: |
| 69 | 71 | self._signature = signature |
| 70 | 72 | self._rebuild(board, moves, evals, symbols) |
| @@ -78,7 +80,7 @@ class MoveList(Gtk.Box): | ||
| 78 | 80 | for ply, move in enumerate(moves): |
| 79 | 81 | number = replay.fullmove_number |
| 80 | 82 | prefix = f"{number}." if replay.turn == chess.WHITE else f"{number}…" |
| 81 | - san = replay.san(move) | |
| 83 | + san = notation.move(replay, move, self.notation) | |
| 82 | 84 | replay.push(move) |
| 83 | 85 | self._rows.append(self._make_row(ply, prefix, san, symbols.get(ply), evals.get(ply))) |
| 84 | 86 | for row, _ in self._rows: |
added schach/notation.py +44 −0
| @@ -0,0 +1,44 @@ | ||
| 1 | +"""Schreibweise der Züge: deutsche (Sf3, Lc4, Dd1) oder englische Figurenbuchstaben (Nf3, Bc4, Qd1). | |
| 2 | + | |
| 3 | +Intern und in PGN-Dateien gilt immer die englische Standard-Notation; umgestellt wird | |
| 4 | +nur die Anzeige. Kurskommentare sind auf Deutsch geschrieben – Züge darin werden für | |
| 5 | +die englische Anzeige übersetzt. | |
| 6 | +""" | |
| 7 | + | |
| 8 | +import re | |
| 9 | + | |
| 10 | +STYLES = {"de": "Deutsch (K D T L S)", "en": "Englisch (K Q R B N)"} | |
| 11 | +DEFAULT = "de" | |
| 12 | + | |
| 13 | +_TO_DE = str.maketrans({"N": "S", "B": "L", "R": "T", "Q": "D"}) | |
| 14 | +_TO_EN = str.maketrans({"S": "N", "L": "B", "T": "R", "D": "Q"}) | |
| 15 | +# Ein Zug mit deutscher Figur im Fließtext: Sf3, Lxc6, Sbd2, Te1, De2, e8=D – Wörter haben keine Ziffern | |
| 16 | +_GERMAN_MOVE = re.compile(r"\b([KDTLS])([a-h]?[1-8]?)(x?)([a-h][1-8])\b|\b([a-h]x?[a-h]?[18])=([DTLS])\b") | |
| 17 | + | |
| 18 | + | |
| 19 | +def san(san_text, style=DEFAULT): | |
| 20 | + """Englische SAN (von python-chess) in der gewählten Schreibweise.""" | |
| 21 | + return san_text.translate(_TO_DE) if style == "de" else san_text | |
| 22 | + | |
| 23 | + | |
| 24 | +def move(board, mv, style=DEFAULT): | |
| 25 | + return san(board.san(mv), style) | |
| 26 | + | |
| 27 | + | |
| 28 | +def label(board, mv, style=DEFAULT): | |
| 29 | + """'5. d3' bzw. '5… Sf6'.""" | |
| 30 | + number = f"{board.fullmove_number}." if board.turn else f"{board.fullmove_number}…" | |
| 31 | + return f"{number} {move(board, mv, style)}" | |
| 32 | + | |
| 33 | + | |
| 34 | +def text(german_text, style=DEFAULT): | |
| 35 | + """Fließtext mit deutschen Zügen (Kurskommentare) in der gewählten Schreibweise.""" | |
| 36 | + if style == "de" or not german_text: | |
| 37 | + return german_text | |
| 38 | + | |
| 39 | + def english(m): | |
| 40 | + if m.group(1): | |
| 41 | + return m.group(1).translate(_TO_EN) + m.group(2) + m.group(3) + m.group(4) | |
| 42 | + return f"{m.group(5)}={m.group(6).translate(_TO_EN)}" | |
| 43 | + | |
| 44 | + return _GERMAN_MOVE.sub(english, german_text) | |
modified schach/opening.py +0 −8
| @@ -22,18 +22,10 @@ COURSE_DIR = os.path.join(os.path.dirnam | ||
| 22 | 22 | BOOK_PATH = "/usr/share/pychess/pychess_book.bin" |
| 23 | 23 | ECO_PATH = "/usr/share/pychess/eco.db" |
| 24 | 24 | |
| 25 | -GERMAN_PIECES = str.maketrans({"N": "S", "B": "L", "R": "T", "Q": "D"}) | |
| 26 | - | |
| 27 | - | |
| 28 | 25 | def key(board): |
| 29 | 26 | return chess.polyglot.zobrist_hash(board) |
| 30 | 27 | |
| 31 | 28 | |
| 32 | -def san_de(san): | |
| 33 | - """SAN mit deutschen Figurenbuchstaben: Nf3 → Sf3, Bxc6 → Lxc6, e8=Q → e8=D.""" | |
| 34 | - return san.translate(GERMAN_PIECES) | |
| 35 | - | |
| 36 | - | |
| 37 | 29 | @dataclasses.dataclass |
| 38 | 30 | class Option: |
| 39 | 31 | move: chess.Move |
modified schach/prefs.py +16 −0
| @@ -6,6 +6,7 @@ gi.require_version("Gtk", "4.0") | ||
| 6 | 6 | gi.require_version("Adw", "1") |
| 7 | 7 | from gi.repository import Adw, Gtk # noqa: E402 |
| 8 | 8 | |
| 9 | +from . import notation # noqa: E402 | |
| 9 | 10 | from .board import DEFAULT_SCHEME, SCHEMES, scheme_colors # noqa: E402 |
| 10 | 11 | |
| 11 | 12 | COLORS = [("white", "Weiß"), ("black", "Schwarz"), ("random", "Zufällig")] |
| @@ -73,6 +74,21 @@ class Preferences(Adw.PreferencesDialog) | ||
| 73 | 74 | row.connect("notify::selected", lambda r, _p: self._set("theme", names[r.get_selected()])) |
| 74 | 75 | pieces.add(row) |
| 75 | 76 | page.add(pieces) |
| 77 | + | |
| 78 | + moves = Adw.PreferencesGroup( | |
| 79 | + title="Notation", | |
| 80 | + description="Gilt für Zugliste und Erklärungen. PGN-Dateien verwenden immer die " | |
| 81 | + "internationale (englische) Schreibweise.", | |
| 82 | + ) | |
| 83 | + styles = list(notation.STYLES) | |
| 84 | + style_row = Adw.ComboRow( | |
| 85 | + title="Figurenbuchstaben", model=Gtk.StringList.new([notation.STYLES[k] for k in styles]), | |
| 86 | + ) | |
| 87 | + current_style = self.win.settings.get("notation", notation.DEFAULT) | |
| 88 | + style_row.set_selected(styles.index(current_style) if current_style in styles else 0) | |
| 89 | + style_row.connect("notify::selected", lambda r, _p: self._set("notation", styles[r.get_selected()])) | |
| 90 | + moves.add(style_row) | |
| 91 | + page.add(moves) | |
| 76 | 92 | return page |
| 77 | 93 | |
| 78 | 94 | def _scheme_group(self): |
modified schach/training.py +8 −14
| @@ -15,7 +15,7 @@ gi.require_version("Gtk", "4.0") | ||
| 15 | 15 | gi.require_version("Adw", "1") |
| 16 | 16 | from gi.repository import Adw, GLib, Gtk # noqa: E402 |
| 17 | 17 | |
| 18 | -from . import annotate, opening # noqa: E402 | |
| 18 | +from . import annotate, notation, opening # noqa: E402 | |
| 19 | 19 | from .engines import maia_candidates # noqa: E402 |
| 20 | 20 | |
| 21 | 21 | OPPONENT_DELAY_MS = 650 |
| @@ -42,12 +42,6 @@ def courses(): | ||
| 42 | 42 | return _resources["courses"] |
| 43 | 43 | |
| 44 | 44 | |
| 45 | -def move_label(board, move): | |
| 46 | - """'5. d3' bzw. '5… Sf6' mit deutschen Figurenbuchstaben.""" | |
| 47 | - number = f"{board.fullmove_number}." if board.turn == chess.WHITE else f"{board.fullmove_number}…" | |
| 48 | - return f"{number} {opening.san_de(board.san(move))}" | |
| 49 | - | |
| 50 | - | |
| 51 | 45 | def _escape(text): |
| 52 | 46 | return GLib.markup_escape_text(text or "") |
| 53 | 47 | |
| @@ -92,7 +86,7 @@ class OpeningMixin: | ||
| 92 | 86 | key = opening.key(self.board) |
| 93 | 87 | if key not in self._name_cache: |
| 94 | 88 | self._name_cache[key] = _names().name(self.board) |
| 95 | - name = self._name_cache[key] or self.course.title | |
| 89 | + name = notation.text(self._name_cache[key], self._notation()) or self.course.title | |
| 96 | 90 | return f"{name} · Zug {self.board.fullmove_number}" |
| 97 | 91 | |
| 98 | 92 | # --- Ablauf ------------------------------------------------------------ |
| @@ -158,7 +152,7 @@ class OpeningMixin: | ||
| 158 | 152 | self.save_path = "" |
| 159 | 153 | self.view.flipped = self.human == chess.BLACK |
| 160 | 154 | self.list_btn.set_active(False) |
| 161 | - self._coach(_escape(course.welcome or course.intro)) | |
| 155 | + self._coach(_escape(notation.text(course.welcome or course.intro, self._notation()))) | |
| 162 | 156 | self._changed() |
| 163 | 157 | self._refresh() |
| 164 | 158 | self._opening_next_step() |
| @@ -199,12 +193,12 @@ class OpeningMixin: | ||
| 199 | 193 | def _opening_reply(self, gen, option): |
| 200 | 194 | if gen != self.gen or self.mode != "opening" or option.move not in self.board.legal_moves: |
| 201 | 195 | return False |
| 202 | - label = move_label(self.board, option.move) | |
| 196 | + label = notation.label(self.board, option.move, self._notation()) | |
| 203 | 197 | self.view.animate_move(self.board, option.move) |
| 204 | 198 | self.board.push(option.move) |
| 205 | 199 | self.opening_busy = False |
| 206 | 200 | self._changed() |
| 207 | - comment = f" – {_escape(option.comment)}" if option.comment else "" | |
| 201 | + comment = f" – {_escape(notation.text(option.comment, self._notation()))}" if option.comment else "" | |
| 208 | 202 | self._coach(f"<b>{_escape(label)}</b>{comment}") |
| 209 | 203 | self._refresh() |
| 210 | 204 | if not self.course.options(self.board): |
| @@ -214,7 +208,7 @@ class OpeningMixin: | ||
| 214 | 208 | def _opening_play(self, move): |
| 215 | 209 | before = self.board.copy() |
| 216 | 210 | verdict = opening.judge(self.course, _book(), _names(), before, move) |
| 217 | - label = _escape(move_label(before, move)) | |
| 211 | + label = _escape(notation.label(before, move, self._notation())) | |
| 218 | 212 | ply = len(self.board.move_stack) |
| 219 | 213 | self.view.animate_move(self.board, move) |
| 220 | 214 | self.board.push(move) |
| @@ -226,7 +220,7 @@ class OpeningMixin: | ||
| 226 | 220 | comment = verdict.option.comment or "Genau so." |
| 227 | 221 | else: |
| 228 | 222 | comment = "Zugumstellung – auch so kommst du zu unserem Ziel." |
| 229 | - self._coach(f"✓ <b>{label}</b> – {_escape(comment)}") | |
| 223 | + self._coach(f"✓ <b>{label}</b> – {_escape(notation.text(comment, self._notation()))}") | |
| 230 | 224 | self._refresh() |
| 231 | 225 | self._opening_next_step() |
| 232 | 226 | return |
| @@ -262,7 +256,7 @@ class OpeningMixin: | ||
| 262 | 256 | self.stockfish.submit(annotate.fallback_job(before, move), on_result) |
| 263 | 257 | |
| 264 | 258 | def _opening_off(self, ply, markup): |
| 265 | - self._coach(f"{markup} Versuch es nochmal! <span alpha='70%'>(Kurszug mit H)</span>") | |
| 259 | + self._coach(f"{markup} Versuch es nochmal! <span alpha='70%'>Tipp: Der Hinweis (H) zeigt dir den Kurszug.</span>") | |
| 266 | 260 | self._refresh() |
| 267 | 261 | GLib.timeout_add(TAKE_BACK_MS, self._opening_take_back, self.gen, ply) |
| 268 | 262 | |
added tests/test_notation.py +37 −0
| @@ -0,0 +1,37 @@ | ||
| 1 | +import unittest | |
| 2 | + | |
| 3 | +import chess | |
| 4 | + | |
| 5 | +from schach import notation | |
| 6 | + | |
| 7 | + | |
| 8 | +class NotationTest(unittest.TestCase): | |
| 9 | + def test_san(self): | |
| 10 | + self.assertEqual(notation.san("Nf3", "de"), "Sf3") | |
| 11 | + self.assertEqual(notation.san("Bxc6", "de"), "Lxc6") | |
| 12 | + self.assertEqual(notation.san("e8=Q+", "de"), "e8=D+") | |
| 13 | + self.assertEqual(notation.san("Rfe1", "de"), "Tfe1") | |
| 14 | + self.assertEqual(notation.san("O-O", "de"), "O-O") | |
| 15 | + self.assertEqual(notation.san("Nf3", "en"), "Nf3") | |
| 16 | + | |
| 17 | + def test_label(self): | |
| 18 | + board = chess.Board() | |
| 19 | + self.assertEqual(notation.label(board, chess.Move.from_uci("g1f3"), "de"), "1. Sf3") | |
| 20 | + board.push_san("e4") | |
| 21 | + self.assertEqual(notation.label(board, chess.Move.from_uci("b8c6"), "en"), "1… Nc6") | |
| 22 | + | |
| 23 | + def test_text(self): | |
| 24 | + german = "Sg5 wäre wilder. Nach Sxe4 holt Te1 den Bauern zurück, dann Lxc6 und De2. Plan Sbd2–f1–g3, e8=D." | |
| 25 | + self.assertEqual(notation.text(german, "de"), german) | |
| 26 | + self.assertEqual( | |
| 27 | + notation.text(german, "en"), | |
| 28 | + "Ng5 wäre wilder. Nach Nxe4 holt Re1 den Bauern zurück, dann Bxc6 und Qe2. Plan Nbd2–f1–g3, e8=Q.", | |
| 29 | + ) | |
| 30 | + | |
| 31 | + def test_text_leaves_words_alone(self): | |
| 32 | + sentence = "Schwarz deckt e5. Der Läufer zielt auf f7 – Lehrerin, Dame, Turm, König." | |
| 33 | + self.assertEqual(notation.text(sentence, "en"), sentence) | |
| 34 | + | |
| 35 | + | |
| 36 | +if __name__ == "__main__": | |
| 37 | + unittest.main() | |
modified tests/test_opening.py +0 −8
| @@ -69,14 +69,6 @@ class CourseTest(unittest.TestCase): | ||
| 69 | 69 | opening.judge(italian, self.book, self.names, before, chess.Move.from_uci("c2c3")).kind, "course" |
| 70 | 70 | ) |
| 71 | 71 | |
| 72 | - def test_san_de(self): | |
| 73 | - self.assertEqual(opening.san_de("Nf3"), "Sf3") | |
| 74 | - self.assertEqual(opening.san_de("Bxc6"), "Lxc6") | |
| 75 | - self.assertEqual(opening.san_de("O-O"), "O-O") | |
| 76 | - self.assertEqual(opening.san_de("e8=Q+"), "e8=D+") | |
| 77 | - self.assertEqual(opening.san_de("Qxd8"), "Dxd8") | |
| 78 | - self.assertEqual(opening.san_de("Rfe1"), "Tfe1") | |
| 79 | - | |
| 80 | 72 | |
| 81 | 73 | @unittest.skipUnless(os.path.exists(opening.ECO_PATH), "eco.db von PyChess fehlt") |
| 82 | 74 | class NamesTest(unittest.TestCase): |