Public repository: timo / schach

Reader

Ein aufgeräumtes Schach-Programm für GNOME

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>
Changeseta7ec0edb3ebbba011f7eaa7cdc8447c18d509a96
Author (claimed)Timo Stolz <timo.stolz@nullachtvierzehn.de>,
Parentse41ec0ae0a80
Children7f83afced4ef
Pushlog
by timo via ssh
by timo via ssh

modified README.md +2 −1

@@ -49,7 +49,8 @@ Schlichtes GNOME-Schachprogramm (Python,
4949
   Partie, öffnet sich ein neues. Fremde Partien zeigen die Namen und werden für beide
5050
   Seiten analysiert. Während einer Online-Partie ist Einfügen gesperrt.
5151
 - **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.
5354
 
5455
 ## Starten
5556
 

modified schach/app.py +12 −4

@@ -15,7 +15,7 @@ gi.require_version("Gtk", "4.0")
1515
 gi.require_version("Adw", "1")
1616
 from gi.repository import Adw, Gdk, Gio, GLib, GObject, Gtk  # noqa: E402
1717
 
18
-from . import annotate, clipboard, lichess, savegames  # noqa: E402
18
+from . import annotate, clipboard, lichess, notation, savegames  # noqa: E402
1919
 from .board import BoardView, available_themes  # noqa: E402
2020
 from .movelist import MoveList  # noqa: E402
2121
 from .prefs import Preferences  # noqa: E402
@@ -49,7 +49,7 @@ SETTINGS_PATH = os.path.join(GLib.get_us
4949
 DEFAULTS = {
5050
     "elo": 1200, "color": "white", "theme": "cburnett", "puzzle_difficulty": "normal",
5151
     "temperature": OPPONENT_TEMPERATURE, "scheme": "holz", "hint_helper": DEFAULT_HELPER,
52
-    "threat_source": "opponent",
52
+    "threat_source": "opponent", "notation": notation.DEFAULT,
5353
 }
5454
 
5555
 
@@ -272,6 +272,7 @@ class Window(OnlineMixin, PuzzleMixin, O
272272
         box.append(self._build_coach())
273273
 
274274
         self.movelist = MoveList()
275
+        self.movelist.notation = self._notation()
275276
         self.movelist.on_select = self._browse
276277
         self.movelist.on_copy = self._copy_fen
277278
         self.movelist.on_continue = self._continue_from
@@ -513,7 +514,7 @@ class Window(OnlineMixin, PuzzleMixin, O
513514
             mover = shown.copy()
514515
             last = mover.pop()
515516
             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())}")
517518
         elif self.mode == "puzzle":
518519
             self.title.set_subtitle(self._puzzle_subtitle())
519520
         elif self.mode == "opening":
@@ -1155,7 +1156,7 @@ class Window(OnlineMixin, PuzzleMixin, O
11551156
         board = state.board.copy()
11561157
         last = board.pop()
11571158
         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())}"
11591160
         self.get_application().open_window(state)
11601161
 
11611162
     def _set_symbol(self, ply, move, symbol):
@@ -1170,6 +1171,10 @@ class Window(OnlineMixin, PuzzleMixin, O
11701171
         self.view.flipped = not self.view.flipped
11711172
         self.view.queue_draw()
11721173
 
1174
+    def _notation(self):
1175
+        style = self.settings.get("notation", notation.DEFAULT)
1176
+        return style if style in notation.STYLES else notation.DEFAULT
1177
+
11731178
     def _helper(self):
11741179
         helper = self.settings.get("hint_helper", DEFAULT_HELPER)
11751180
         return helper if helper in HELPERS else DEFAULT_HELPER
@@ -1193,6 +1198,9 @@ class Window(OnlineMixin, PuzzleMixin, O
11931198
             self.view.set_theme(value)
11941199
         elif key == "scheme":
11951200
             self.view.set_scheme(value)
1201
+        elif key == "notation":
1202
+            self.movelist.notation = self._notation()
1203
+            self._refresh()
11961204
 
11971205
     def _preferences_dialog(self, *_):
11981206
         if self.prefs_dialog is not None:

modified schach/movelist.py +4 −2

@@ -6,6 +6,7 @@ import gi
66
 gi.require_version("Gtk", "4.0")
77
 from gi.repository import Gtk  # noqa: E402
88
 
9
+from . import notation  # noqa: E402
910
 from .board import BADGE  # noqa: E402
1011
 
1112
 CSS = b"""
@@ -54,6 +55,7 @@ class MoveList(Gtk.Box):
5455
         self.scroller.set_child(self.listbox)
5556
         self.append(self.scroller)
5657
 
58
+        self.notation = "de"  # Schreibweise der Züge, siehe notation.py
5759
         self._rows = []
5860
         self._selected = None
5961
         self._signature = None
@@ -64,7 +66,7 @@ class MoveList(Gtk.Box):
6466
     def update(self, board, evals, symbols, selected):
6567
         """Baut die Liste neu, wenn sich Züge, Bewertungen oder Zeichen geändert haben."""
6668
         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)
6870
         if signature != self._signature:
6971
             self._signature = signature
7072
             self._rebuild(board, moves, evals, symbols)
@@ -78,7 +80,7 @@ class MoveList(Gtk.Box):
7880
         for ply, move in enumerate(moves):
7981
             number = replay.fullmove_number
8082
             prefix = f"{number}." if replay.turn == chess.WHITE else f"{number}…"
81
-            san = replay.san(move)
83
+            san = notation.move(replay, move, self.notation)
8284
             replay.push(move)
8385
             self._rows.append(self._make_row(ply, prefix, san, symbols.get(ply), evals.get(ply)))
8486
         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
2222
 BOOK_PATH = "/usr/share/pychess/pychess_book.bin"
2323
 ECO_PATH = "/usr/share/pychess/eco.db"
2424
 
25
-GERMAN_PIECES = str.maketrans({"N": "S", "B": "L", "R": "T", "Q": "D"})
26
-
27
-
2825
 def key(board):
2926
     return chess.polyglot.zobrist_hash(board)
3027
 
3128
 
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
-
3729
 @dataclasses.dataclass
3830
 class Option:
3931
     move: chess.Move

modified schach/prefs.py +16 −0

@@ -6,6 +6,7 @@ gi.require_version("Gtk", "4.0")
66
 gi.require_version("Adw", "1")
77
 from gi.repository import Adw, Gtk  # noqa: E402
88
 
9
+from . import notation  # noqa: E402
910
 from .board import DEFAULT_SCHEME, SCHEMES, scheme_colors  # noqa: E402
1011
 
1112
 COLORS = [("white", "Weiß"), ("black", "Schwarz"), ("random", "Zufällig")]
@@ -73,6 +74,21 @@ class Preferences(Adw.PreferencesDialog)
7374
         row.connect("notify::selected", lambda r, _p: self._set("theme", names[r.get_selected()]))
7475
         pieces.add(row)
7576
         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)
7692
         return page
7793
 
7894
     def _scheme_group(self):

modified schach/training.py +8 −14

@@ -15,7 +15,7 @@ gi.require_version("Gtk", "4.0")
1515
 gi.require_version("Adw", "1")
1616
 from gi.repository import Adw, GLib, Gtk  # noqa: E402
1717
 
18
-from . import annotate, opening  # noqa: E402
18
+from . import annotate, notation, opening  # noqa: E402
1919
 from .engines import maia_candidates  # noqa: E402
2020
 
2121
 OPPONENT_DELAY_MS = 650
@@ -42,12 +42,6 @@ def courses():
4242
     return _resources["courses"]
4343
 
4444
 
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
-
5145
 def _escape(text):
5246
     return GLib.markup_escape_text(text or "")
5347
 
@@ -92,7 +86,7 @@ class OpeningMixin:
9286
         key = opening.key(self.board)
9387
         if key not in self._name_cache:
9488
             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
9690
         return f"{name} · Zug {self.board.fullmove_number}"
9791
 
9892
     # --- Ablauf ------------------------------------------------------------
@@ -158,7 +152,7 @@ class OpeningMixin:
158152
         self.save_path = ""
159153
         self.view.flipped = self.human == chess.BLACK
160154
         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())))
162156
         self._changed()
163157
         self._refresh()
164158
         self._opening_next_step()
@@ -199,12 +193,12 @@ class OpeningMixin:
199193
     def _opening_reply(self, gen, option):
200194
         if gen != self.gen or self.mode != "opening" or option.move not in self.board.legal_moves:
201195
             return False
202
-        label = move_label(self.board, option.move)
196
+        label = notation.label(self.board, option.move, self._notation())
203197
         self.view.animate_move(self.board, option.move)
204198
         self.board.push(option.move)
205199
         self.opening_busy = False
206200
         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 ""
208202
         self._coach(f"<b>{_escape(label)}</b>{comment}")
209203
         self._refresh()
210204
         if not self.course.options(self.board):
@@ -214,7 +208,7 @@ class OpeningMixin:
214208
     def _opening_play(self, move):
215209
         before = self.board.copy()
216210
         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()))
218212
         ply = len(self.board.move_stack)
219213
         self.view.animate_move(self.board, move)
220214
         self.board.push(move)
@@ -226,7 +220,7 @@ class OpeningMixin:
226220
                 comment = verdict.option.comment or "Genau so."
227221
             else:
228222
                 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()))}")
230224
             self._refresh()
231225
             self._opening_next_step()
232226
             return
@@ -262,7 +256,7 @@ class OpeningMixin:
262256
         self.stockfish.submit(annotate.fallback_job(before, move), on_result)
263257
 
264258
     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>")
266260
         self._refresh()
267261
         GLib.timeout_add(TAKE_BACK_MS, self._opening_take_back, self.gen, ply)
268262
 

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):
6969
             opening.judge(italian, self.book, self.names, before, chess.Move.from_uci("c2c3")).kind, "course"
7070
         )
7171
 
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
-
8072
 
8173
 @unittest.skipUnless(os.path.exists(opening.ECO_PATH), "eco.db von PyChess fehlt")
8274
 class NamesTest(unittest.TestCase):