feat(annotate): flag lost mates, missed short mates and needless material loss
public
feat(annotate): flag lost mates, missed short mates and needless material loss Win percentage barely moves in decided positions, so obvious slips got no symbol. Three extra rules: - lost forced mate, as on Lichess, up to mate in depth/3 (at most 5) so long mates that Stockfish only sometimes sees do not flicker - missed mate in 1 or 2 while the mate just takes longer: ?! - giving away a piece when another top move is as successful and keeps it: ?! "Warum nicht?" says "Damit setzt du sofort matt." and "Damit behältst du …". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | 00427af512e9951784d1fb73b45c55cb7d0efd71 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | 2457cf10d667 |
| Children | 3c25b2ae3473 |
| Pushlog | by timo via ssh by timo via ssh |
modified README.md +6 −1
| @@ -19,6 +19,10 @@ Schlichtes GNOME-Schachprogramm (Python, | ||
| 19 | 19 | - **Zugbewertung:** Stockfish denkt mit, während du überlegst (Top-3-Züge, live |
| 20 | 20 | immer tiefer); andere Züge werden nur bei Bedarf einzeln nachgerechnet. |
| 21 | 21 | Nur auffällige Züge bekommen ein Zeichen an der Figur: `!!` `!` `?!` `?` `??`. |
| 22 | + Maßstab ist die verlorene Gewinnchance (5/10/15 Prozentpunkte, wie bei Lichess). Weil | |
| 23 | + die sich in entschiedenen Stellungen kaum bewegt, gibt es dort zusätzlich: verlorenes | |
| 24 | + erzwungenes Matt (wie Lichess), übersehenes Matt in 1 oder 2 (`?!`) und eine Figur | |
| 25 | + ohne Not hergegeben, obwohl ein genauso guter Zug sie behalten hätte (`?!`). | |
| 22 | 26 | Wie lange, steht unter Einstellungen → Analyse: Threads, maximale Tiefe und Rechenzeit, je |
| 23 | 27 | für „Normal“ (halb so viele Threads wie der Rechner hat, höchstens 8; Tiefe 30 oder 30 s) und |
| 24 | 28 | „Energiesparen“ (2 Threads, Tiefe 18 oder 10 s). Automatisch gespart wird auf Akku oder wenn |
| @@ -159,7 +163,8 @@ Ins App-Menü aufnehmen: | ||
| 159 | 163 | |
| 160 | 164 | - `schach/app.py`: `HELPERS` (Hinweis-Helfer), `MIN_REPLY_SECONDS` |
| 161 | 165 | - `schach/threat.py`: `THREAT_GAIN` (ab wann eine Drohung zählt), `THREAT_LIMIT` |
| 162 | -- `schach/annotate.py`: Schwellen für `?!`/`?`/`??` und für „einziger Zug“ (`!`) | |
| 166 | +- `schach/annotate.py`: Schwellen für `?!`/`?`/`??`, für „einziger Zug“ (`!`), `SHORT_MATE` | |
| 167 | + (übersehenes kurzes Matt) und `NEEDLESS_LOSS` (unnötiger Materialverlust) | |
| 163 | 168 | - Brettfarben: `SCHEMES` in `schach/diagram.py` |
| 164 | 169 | - Eröffnungskurse: `schach/kurse/*.pgn` – normale PGN-Dateien. Hauptvariante = Plan, |
| 165 | 170 | Varianten = weitere Antworten, Kommentare = Erklärung. Kopf: `[Event "Name"]`, |
modified schach/annotate.py +124 −17
| @@ -25,6 +25,18 @@ NAG = {"!": 1, "?": 2, "!!": 3, "??": 4, | ||
| 25 | 25 | BLUNDER, MISTAKE, INACCURACY = 15.0, 10.0, 5.0 |
| 26 | 26 | # Ein "einziger Zug": alle Alternativen sind mindestens so viel schlechter |
| 27 | 27 | ONLY_MOVE_GAP = 12.0 |
| 28 | +# In klar entschiedenen Stellungen ändert sich die Gewinnchance kaum. Dafür drei Zusatzregeln: | |
| 29 | +# verlorenes Matt (wie Lichess), übersehenes kurzes Matt und unnötig hergegebenes Material. | |
| 30 | +LOST_MATE_INACCURACY, LOST_MATE_MISTAKE = 999, 700 # Centipawns danach, Schwellen von Lichess | |
| 31 | +# Verlorenes Matt zählt nur bis Matt in LOST_MATE_MAX – und nur so weit, wie die Rechentiefe ein Matt | |
| 32 | +# sicher findet (Tiefe / MATE_DEPTH_PER_MOVE). Längere Matts sieht Stockfish mal und mal nicht. | |
| 33 | +LOST_MATE_MAX = 5 | |
| 34 | +MATE_DEPTH_PER_MOVE = 3 | |
| 35 | +SHORT_MATE = 2 # Matt in so vielen Zügen übersehen (es geht nur langsamer weiter) gibt ?! | |
| 36 | +NEEDLESS_LOSS = 3 # so viele Bauerneinheiten (eine Figur) ohne Not hergegeben gibt ?! | |
| 37 | +SAME_SUCCESS = 1.0 # Prozentpunkte: so knapp unter dem gespielten Zug gilt eine Alternative als genauso gut | |
| 38 | +SETTLE_PLIES = 10 # höchstens so weit in die Hauptlinie, bis der Tausch fertig ist | |
| 39 | +SEVERITY = {None: 0, "?!": 1, "?": 2, "??": 3} | |
| 28 | 40 | # Hinweis von Stockfish, auf Vorrat: vorläufig ab HINT_MIN_DEPTH (bei einem eigenen Matt sofort), dann |
| 29 | 41 | # mit jeder Tiefe nachgeschärft bis HINT_DEPTH. Wer früh H drückt, bekommt den aktuellen Stand. |
| 30 | 42 | HINT_MIN_DEPTH = 8 |
| @@ -41,6 +53,11 @@ def win_pct(score, color): | ||
| 41 | 53 | return score.pov(color).wdl(model="lichess").expectation() * 100.0 |
| 42 | 54 | |
| 43 | 55 | |
| 56 | +def _wp(score): | |
| 57 | + """Gewinnchance zu einer Bewertung, die schon aus Sicht des Ziehenden ist.""" | |
| 58 | + return score.wdl(model="lichess").expectation() * 100.0 | |
| 59 | + | |
| 60 | + | |
| 44 | 61 | def is_sacrifice(before, move): |
| 45 | 62 | """Grobe Abschätzung: lässt der Zug mindestens zwei Bauern Material stehen?""" |
| 46 | 63 | mover = before.turn |
| @@ -71,16 +88,99 @@ def is_sacrifice(before, move): | ||
| 71 | 88 | return net <= -2 |
| 72 | 89 | |
| 73 | 90 | |
| 74 | -def classify(before, move, best_wp, second_wp, played_wp): | |
| 75 | - """Reine Logik, ohne Engine – gibt ein Symbol oder None zurück.""" | |
| 76 | - loss = best_wp - played_wp | |
| 77 | - if loss >= BLUNDER: | |
| 78 | - return "??" | |
| 79 | - if loss >= MISTAKE: | |
| 80 | - return "?" | |
| 81 | - if loss >= INACCURACY: | |
| 91 | +def material(board, color): | |
| 92 | + return sum(VALUE[p.piece_type] * (1 if p.color == color else -1) for p in board.piece_map().values()) | |
| 93 | + | |
| 94 | + | |
| 95 | +def settle(before, pv): | |
| 96 | + """Stellung entlang der Hauptlinie, sobald der Tausch fertig ist. | |
| 97 | + | |
| 98 | + Zug und Antwort zählen immer; danach geht es weiter, solange geschlagen wird oder Schach steht. | |
| 99 | + """ | |
| 100 | + probe = before.copy(stack=False) | |
| 101 | + for i, move in enumerate(pv[:SETTLE_PLIES]): | |
| 102 | + if move not in probe.legal_moves: | |
| 103 | + break | |
| 104 | + if i >= 2 and not (probe.is_check() or probe.is_capture(move)): | |
| 105 | + break | |
| 106 | + probe.push(move) | |
| 107 | + if probe.is_game_over(): | |
| 108 | + break | |
| 109 | + return probe | |
| 110 | + | |
| 111 | + | |
| 112 | +def material_change(before, pv): | |
| 113 | + """Materialänderung für den Ziehenden, bis der Tausch fertig ist (siehe settle).""" | |
| 114 | + return material(settle(before, pv), before.turn) - material(before, before.turn) | |
| 115 | + | |
| 116 | + | |
| 117 | +def _mate(score): | |
| 118 | + """Eigenes erzwungenes Matt in n Zügen, sonst None.""" | |
| 119 | + n = score.mate() | |
| 120 | + return n if n is not None and n > 0 else None | |
| 121 | + | |
| 122 | + | |
| 123 | +def mate_horizon(depth): | |
| 124 | + """Bis zu welchem Matt in n Zügen ein verlorenes Matt zählt, bei dieser Rechentiefe.""" | |
| 125 | + return min(LOST_MATE_MAX, depth // MATE_DEPTH_PER_MOVE) | |
| 126 | + | |
| 127 | + | |
| 128 | +def _lost_mate(best, played, depth): | |
| 129 | + """Lichess „Lost forced checkmate sequence“: das Matt war da und ist weg – sofern es kurz genug war, | |
| 130 | + dass Stockfish es bei ``depth`` verlässlich sieht.""" | |
| 131 | + if _mate(best) is None or _mate(best) > mate_horizon(depth) or _mate(played) is not None: | |
| 132 | + return None | |
| 133 | + cp = played.score() or 0 # selbst mattgesetzt zählt wie 0 | |
| 134 | + if cp > LOST_MATE_INACCURACY: | |
| 82 | 135 | return "?!" |
| 136 | + return "?" if cp > LOST_MATE_MISTAKE else "??" | |
| 83 | 137 | |
| 138 | + | |
| 139 | +def _as_good(other, played): | |
| 140 | + """Bringt ``other`` mindestens so viel wie der gespielte Zug? Matt nur, wenn es nicht länger dauert.""" | |
| 141 | + if _mate(played) is not None: | |
| 142 | + return _mate(other) is not None and _mate(other) <= _mate(played) | |
| 143 | + return _mate(other) is not None or _wp(other) >= _wp(played) - SAME_SUCCESS | |
| 144 | + | |
| 145 | + | |
| 146 | +def _needless_loss(before, move, lines): | |
| 147 | + """?! wenn der Zug Material hergibt, das ein genauso erfolgreicher anderer Zug behält.""" | |
| 148 | + score, pv = lines[move] | |
| 149 | + change = material_change(before, pv) | |
| 150 | + if change > -NEEDLESS_LOSS: | |
| 151 | + return None | |
| 152 | + for other, (other_score, other_pv) in lines.items(): | |
| 153 | + if other != move and _as_good(other_score, score) and \ | |
| 154 | + material_change(before, other_pv) - change >= NEEDLESS_LOSS: | |
| 155 | + return "?!" | |
| 156 | + return None | |
| 157 | + | |
| 158 | + | |
| 159 | +def classify(before, move, lines, depth=MIN_DEPTH): | |
| 160 | + """Reine Logik, ohne Engine – gibt ein Symbol oder None zurück. | |
| 161 | + | |
| 162 | + ``lines``: {Zug: (Bewertung aus Sicht des Ziehenden, Hauptlinie)}, die Top-Linien und ``move``. | |
| 163 | + ``depth``: Rechentiefe der Bewertung (begrenzt, wie lange Matts zählen). | |
| 164 | + """ | |
| 165 | + wps = {m: _wp(score) for m, (score, _) in lines.items()} | |
| 166 | + ranked = sorted(wps.values(), reverse=True) | |
| 167 | + best = max(score for score, _ in lines.values()) | |
| 168 | + played = lines[move][0] | |
| 169 | + loss = ranked[0] - wps[move] | |
| 170 | + symbol = "??" if loss >= BLUNDER else "?" if loss >= MISTAKE else "?!" if loss >= INACCURACY else None | |
| 171 | + lost = _lost_mate(best, played, depth) | |
| 172 | + if SEVERITY[lost] > SEVERITY[symbol]: | |
| 173 | + symbol = lost | |
| 174 | + if symbol is None and _mate(best) is not None and _mate(best) <= SHORT_MATE and \ | |
| 175 | + _mate(played) is not None and _mate(played) > _mate(best): | |
| 176 | + symbol = "?!" # kurzes Matt übersehen | |
| 177 | + if symbol is None: | |
| 178 | + symbol = _needless_loss(before, move, lines) | |
| 179 | + if symbol is not None: | |
| 180 | + return symbol | |
| 181 | + | |
| 182 | + second_wp = ranked[1] if len(ranked) > 1 else None | |
| 183 | + best_wp = ranked[0] | |
| 84 | 184 | if loss > 1.0 or second_wp is None: |
| 85 | 185 | return None |
| 86 | 186 | if before.legal_moves.count() < 2: |
| @@ -101,15 +201,17 @@ def key(board): | ||
| 101 | 201 | |
| 102 | 202 | |
| 103 | 203 | def _entry(infos, color, depth): |
| 104 | - scores = {i["pv"][0]: win_pct(i["score"], color) for i in infos if i.get("pv") and "score" in i} | |
| 204 | + """Momentaufnahme: ``scores`` Gewinnchance je Zug, ``lines`` (Bewertung, Hauptlinie) je Zug.""" | |
| 205 | + lines = {i["pv"][0]: (i["score"].pov(color), list(i["pv"])) for i in infos if i.get("pv") and "score" in i} | |
| 206 | + scores = {m: _wp(score) for m, (score, _) in lines.items()} | |
| 105 | 207 | best = max(scores, key=scores.get) if scores else None |
| 106 | - return {"depth": depth, "best": best, "scores": scores, "top": len(scores)} | |
| 208 | + return {"depth": depth, "best": best, "scores": scores, "lines": lines, "top": len(scores)} | |
| 107 | 209 | |
| 108 | 210 | |
| 109 | 211 | def evaluate_move(engine, board, move): |
| 110 | - """Einzelner Zug, gezielt nachgerechnet (für Züge außerhalb der Top-Linien).""" | |
| 212 | + """Einzelner Zug, gezielt nachgerechnet (für Züge außerhalb der Top-Linien): (Bewertung, Hauptlinie).""" | |
| 111 | 213 | info = engine.analyse(board, EXTRA_LIMIT, root_moves=[move]) |
| 112 | - return win_pct(info["score"], board.turn) | |
| 214 | + return info["score"].pov(board.turn), list(info.get("pv") or [move]) | |
| 113 | 215 | |
| 114 | 216 | |
| 115 | 217 | class Thinking: |
| @@ -188,8 +290,10 @@ def classify_with(entry, before, move): | ||
| 188 | 290 | if entry["top"] >= TOP_LINES and ranked[0] - ranked[TOP_LINES - 1] >= BLUNDER: |
| 189 | 291 | return "??" |
| 190 | 292 | return False |
| 191 | - second = ranked[1] if len(ranked) > 1 else None | |
| 192 | - return classify(before, move, ranked[0], second, scores[move]) | |
| 293 | + # einzeln nachgerechnet: nur so tief wie EXTRA_LIMIT, sonst wirkt ein dort übersehenes Matt „verloren“ | |
| 294 | + extra = entry.get("extra", ()) | |
| 295 | + depth = min(entry["depth"], MIN_DEPTH) if move in extra else entry["depth"] | |
| 296 | + return classify(before, move, entry["lines"], depth) | |
| 193 | 297 | |
| 194 | 298 | |
| 195 | 299 | def fallback_job(before, move, entry=None): |
| @@ -200,10 +304,13 @@ def fallback_job(before, move, entry=Non | ||
| 200 | 304 | nonlocal entry |
| 201 | 305 | if not entry or entry["depth"] < MIN_DEPTH: |
| 202 | 306 | infos = engine.analyse(before, EXTRA_LIMIT, multipv=min(TOP_LINES, before.legal_moves.count())) |
| 203 | - entry = _entry(infos, before.turn, 0) | |
| 204 | - entry = {**entry, "scores": dict(entry["scores"])} | |
| 307 | + entry = _entry(infos, before.turn, min((i.get("depth", 0) for i in infos), default=0)) | |
| 308 | + entry = {**entry, "scores": dict(entry["scores"]), "lines": dict(entry["lines"])} | |
| 205 | 309 | if move not in entry["scores"]: |
| 206 | - entry["scores"][move] = evaluate_move(engine, before, move) | |
| 310 | + score, pv = evaluate_move(engine, before, move) | |
| 311 | + entry["lines"][move] = (score, pv) | |
| 312 | + entry["extra"] = (*entry.get("extra", ()), move) | |
| 313 | + entry["scores"][move] = _wp(score) | |
| 207 | 314 | return classify_with(entry, before, move), entry["scores"][move] |
| 208 | 315 | |
| 209 | 316 | return job |
modified schach/explain.py +27 −4
| @@ -17,6 +17,7 @@ import chess | ||
| 17 | 17 | import chess.engine |
| 18 | 18 | import chess.pgn |
| 19 | 19 | |
| 20 | +from . import annotate | |
| 20 | 21 | from .annotate import VALUE, win_pct |
| 21 | 22 | from .threat import threat_job |
| 22 | 23 | |
| @@ -706,8 +707,11 @@ def why_not_job(board, move, maia=None, | ||
| 706 | 707 | if best is None or best == move: |
| 707 | 708 | return None |
| 708 | 709 | other = _explain(engine, board, best, maia, name, limit) |
| 709 | - played = win_pct(engine.analyse(board, limit, root_moves=[move])["score"], board.turn) | |
| 710 | - tree = _explain(engine, board, move, maia, name, limit, better=best, why=_gained(other, played)) | |
| 710 | + info_played = engine.analyse(board, limit, root_moves=[move]) | |
| 711 | + played = win_pct(info_played["score"], board.turn) | |
| 712 | + kept = _kept(board, info.get("pv") or [best], info_played.get("pv") or [move]) | |
| 713 | + why = _gained(other, played, kept) | |
| 714 | + tree = _explain(engine, board, move, maia, name, limit, better=best, why=why) | |
| 711 | 715 | graft(tree, tree.game, other) |
| 712 | 716 | tree.info(tree.first())["label"] = "Gespielt" |
| 713 | 717 | tree.info(tree.game.variations[1])["label"] = "Bester Zug" |
| @@ -726,13 +730,32 @@ def _outcome(tree): | ||
| 726 | 730 | return mate, material(after, tree.hero) - material(tree.game.board(), tree.hero), _chance(tree, leaf) |
| 727 | 731 | |
| 728 | 732 | |
| 729 | -def _gained(best, played): | |
| 733 | +def _kept(board, best_pv, played_pv): | |
| 734 | + """Was der gespielte Zug unnötig hergibt (Text wie „die Dame“), oder None. | |
| 735 | + | |
| 736 | + Wie annotate: nur, wenn der Zug mindestens eine Figur kostet und der beste Zug sie behält.""" | |
| 737 | + played = annotate.material_change(board, played_pv) | |
| 738 | + diff = annotate.material_change(board, best_pv) - played | |
| 739 | + if played > -annotate.NEEDLESS_LOSS or diff < annotate.NEEDLESS_LOSS: | |
| 740 | + return None | |
| 741 | + traded = trade(board, annotate.settle(board, played_pv), board.turn) | |
| 742 | + if traded is not None and traded[0] and not traded[1]: | |
| 743 | + return pieces_text(traded[0]) | |
| 744 | + return gain_text(diff) | |
| 745 | + | |
| 746 | + | |
| 747 | +def _gained(best, played, kept=None): | |
| 730 | 748 | """Was der bessere Zug gebracht hätte, als Satz, mit dem Weg dorthin. ``played``: Gewinnchance |
| 731 | - nach dem gespielten Zug.""" | |
| 749 | + nach dem gespielten Zug, ``kept``: was der gespielte Zug unnötig hergibt (siehe _kept).""" | |
| 732 | 750 | mate, gain, chance = _outcome(best) |
| 733 | 751 | where = "Den Weg zeigt der Pfeil „Bester Zug“ in der Ausgangsstellung (mit <)." |
| 752 | + first = best.first() | |
| 753 | + if first is not None and first.board().is_checkmate(): | |
| 754 | + return "Damit setzt du sofort matt." | |
| 734 | 755 | if mate: |
| 735 | 756 | return f"Damit setzt du matt. {where}" |
| 757 | + if kept is not None: | |
| 758 | + return f"Damit behältst du {kept}. Dein Zug gibt das ohne Not her. {where}" | |
| 736 | 759 | traded = trade(best.game.board(), best.game.end().board(), best.hero) |
| 737 | 760 | if gain > 0 and traded is not None and traded[0]: |
| 738 | 761 | return f"Damit bekommst du {pieces_text(traded[1])} für {pieces_text(traded[0])}. {where}" |
added tests/test_annotate.py +107 −0
| @@ -0,0 +1,107 @@ | ||
| 1 | +import shutil | |
| 2 | +import unittest | |
| 3 | + | |
| 4 | +import chess | |
| 5 | +import chess.engine | |
| 6 | + | |
| 7 | +from schach import annotate, explain | |
| 8 | +from chess.engine import Cp, Mate | |
| 9 | + | |
| 10 | +# Partie gegen Maia 600: Weiß hat früh die Dame gewonnen und tut sich beim Mattsetzen schwer | |
| 11 | +GAME = ("e4 e5 Nf3 d6 Bc4 Nf6 d3 Bg4 O-O c6 Re1 b5 Bb3 Qa5 Bd2 d5 Bxa5 Bxf3 gxf3 Nh5 exd5 Nd7 " | |
| 12 | + "dxc6 Be7 cxd7+ Kxd7 d4 Bh4 dxe5+ Ke7 e6 f5 Qd7+ Kf6 e7 Nf4 Qe6+ Kg5 Qf7 a6 Qxg7+ Kh5 " | |
| 13 | + "Bf7+ Ng6 Re6 h6 Rxg6 Bg5 Rxg5+ Kh4").split() | |
| 14 | + | |
| 15 | + | |
| 16 | +def at(plies): | |
| 17 | + board = chess.Board() | |
| 18 | + for san in GAME[:plies]: | |
| 19 | + board.push_san(san) | |
| 20 | + return board | |
| 21 | + | |
| 22 | + | |
| 23 | +def lines(board, **moves): | |
| 24 | + """{Zug: (Bewertung, Hauptlinie)} aus SAN-Ketten: lines(b, Qd7=(Cp(2000), "Qd7+ Kf6")).""" | |
| 25 | + result = {} | |
| 26 | + for score, sans in moves.values(): | |
| 27 | + probe, pv = board.copy(), [] | |
| 28 | + for san in sans.split(): | |
| 29 | + pv.append(probe.push_san(san)) | |
| 30 | + result[pv[0]] = (score, pv) | |
| 31 | + return result | |
| 32 | + | |
| 33 | + | |
| 34 | +class MateTest(unittest.TestCase): | |
| 35 | + def setUp(self): | |
| 36 | + self.board = at(30) # 16. ?, Weiß am Zug | |
| 37 | + | |
| 38 | + def classify(self, best, played, depth=18): | |
| 39 | + found = lines(self.board, a=(best, "Qd5"), b=(played, "e6")) | |
| 40 | + return annotate.classify(self.board, self.board.parse_san("e6"), found, depth) | |
| 41 | + | |
| 42 | + def test_lost_mate_like_lichess(self): | |
| 43 | + self.assertEqual(self.classify(Mate(5), Cp(1766)), "?!") # danach noch über +10 | |
| 44 | + self.assertEqual(self.classify(Mate(5), Cp(800)), "?") | |
| 45 | + self.assertEqual(self.classify(Mate(5), Cp(300)), "??") | |
| 46 | + self.assertEqual(self.classify(Mate(5), Mate(-3)), "??") | |
| 47 | + | |
| 48 | + def test_lost_mate_only_as_long_as_the_depth_sees_it_reliably(self): | |
| 49 | + self.assertEqual(annotate.mate_horizon(14), 4) # Mindesttiefe, schnell gezogen oder Nachbewertung | |
| 50 | + self.assertEqual(annotate.mate_horizon(18), 5) # Energiesparen | |
| 51 | + self.assertEqual(annotate.mate_horizon(30), 5) # Normal: mehr braucht es nicht | |
| 52 | + self.assertIsNone(self.classify(Mate(5), Cp(1766), depth=14)) | |
| 53 | + self.assertEqual(self.classify(Mate(4), Cp(1766), depth=14), "?!") | |
| 54 | + self.assertIsNone(self.classify(Mate(9), Cp(1766), depth=30)) | |
| 55 | + | |
| 56 | + def test_short_mate_missed(self): | |
| 57 | + self.assertEqual(self.classify(Mate(1), Mate(3)), "?!") | |
| 58 | + self.assertEqual(self.classify(Mate(2), Mate(4)), "?!") | |
| 59 | + | |
| 60 | + def test_longer_mate_only_delayed_is_fine(self): | |
| 61 | + self.assertIsNone(self.classify(Mate(3), Mate(5))) # wie Lichess „MateDelayed“ | |
| 62 | + self.assertIsNone(self.classify(Mate(2), Mate(2))) | |
| 63 | + | |
| 64 | + | |
| 65 | +class NeedlessLossTest(unittest.TestCase): | |
| 66 | + def test_queen_given_away_while_another_move_keeps_it(self): | |
| 67 | + board = at(32) # 17. Dd7+ wurde gespielt, Dd8+ zurückgenommen | |
| 68 | + found = lines(board, a=(Cp(2020), "Qd7+ Kf6 Qf7+ Kg5"), b=(Cp(2063), "Qd8+ Raxd8 Nc3")) | |
| 69 | + self.assertEqual(annotate.classify(board, board.parse_san("Qd8+"), found), "?!") | |
| 70 | + self.assertIsNone(annotate.classify(board, board.parse_san("Qd7+"), found)) | |
| 71 | + | |
| 72 | + def test_no_sign_if_the_material_keeping_move_is_clearly_worse(self): | |
| 73 | + board = at(32) | |
| 74 | + found = lines(board, a=(Cp(200), "Qd7+ Kf6 Qf7+ Kg5"), b=(Cp(2063), "Qd8+ Raxd8 Nc3")) | |
| 75 | + self.assertEqual(annotate.classify(board, board.parse_san("Qd8+"), found), "!!") # einziger Zug, ein Opfer | |
| 76 | + | |
| 77 | + def test_sacrifice_for_faster_mate_is_fine(self): | |
| 78 | + board = at(50) # 26. ?, Weiß am Zug | |
| 79 | + found = lines(board, a=(Mate(3), "Qxh8 Rxh8"), b=(Mate(5), "Rg2 b4")) | |
| 80 | + self.assertIsNone(annotate.classify(board, board.parse_san("Qxh8"), found)) | |
| 81 | + | |
| 82 | + def test_material_change_waits_for_the_recapture(self): | |
| 83 | + board = at(32) | |
| 84 | + pv = lines(board, a=(Cp(0), "Qd8+ Raxd8 Nc3")) | |
| 85 | + self.assertEqual(annotate.material_change(board, next(iter(pv.values()))[1]), -9) | |
| 86 | + | |
| 87 | + | |
| 88 | +@unittest.skipUnless(shutil.which("stockfish"), "stockfish nicht installiert") | |
| 89 | +class WhyNotTest(unittest.TestCase): | |
| 90 | + def why_not(self, board, san): | |
| 91 | + engine = chess.engine.SimpleEngine.popen_uci("stockfish") | |
| 92 | + try: | |
| 93 | + return explain.why_not_job(board, board.parse_san(san), limit=chess.engine.Limit(depth=14))(engine) | |
| 94 | + finally: | |
| 95 | + engine.quit() | |
| 96 | + | |
| 97 | + def test_missed_mate_in_one(self): | |
| 98 | + tree = self.why_not(at(46), "Rxg6") | |
| 99 | + self.assertIn("Besser war Dxg6#: Damit setzt du sofort matt.", tree.first().comment) | |
| 100 | + | |
| 101 | + def test_queen_given_away(self): | |
| 102 | + tree = self.why_not(at(32), "Qd8+") | |
| 103 | + self.assertIn("Damit behältst du die Dame.", tree.first().comment) | |
| 104 | + | |
| 105 | + | |
| 106 | +if __name__ == "__main__": | |
| 107 | + unittest.main() | |