| @@ -26,6 +26,7 @@ HARD_PLIES = 10 # … außer ein Tausch oder ein Schach ist noch nicht zu Ende |
| 26 | 26 | QUIET_PLIES = 4 # ohne Materialwechsel reicht ein kurzer Blick in die Zukunft |
| 27 | 27 | ONLY_MOVE_GAP = 15.0 # so viele Prozentpunkte Gewinnchance liegt der Zug vor dem zweitbesten |
| 28 | 28 | CHILD_LIMIT = chess.engine.Limit(depth=12) # Prüfung der Kandidaten an den Gabelungen |
| 29 | +RIVAL_GAP = 3.0 # so knapp hinter Stockfishs bestem Zug heißt es „ähnlich gut“ |
| 29 | 30 | BRANCH_GAP = 10.0 # eine Gabelung bleibt, wenn das Ergebnis um so viele Prozentpunkte abweicht |
| 30 | 31 | MAIA_MIN = 0.15 # Züge, die Maia seltener spielt, bekommen keinen eigenen Zweig |
| 31 | 32 | MAIA_MAX = 3 |
| @@ -63,7 +64,7 @@ def gain_text(gain): |
| 63 | 64 | def trim(board, pv): |
| 64 | 65 | """Hauptvariante kürzen: bis Matt, bis ein Tausch abgeschlossen ist, höchstens MAX_PLIES. |
| 65 | 66 | |
| 66 | | - Nach jeder Antwort des Gegners: Wird als Nächstes geschlagen oder mattgesetzt, geht es weiter. Sonst ist |
| 67 | + Nach jeder Antwort des Gegners: Wird als Nächstes geschlagen oder Schach gegeben, geht es weiter. Sonst ist |
| 67 | 68 | Schluss, sobald schon geschlagen wurde (Gewinn eingefahren oder Tausch fertig) oder nach |
| 68 | 69 | QUIET_PLIES Halbzügen ganz ohne Schlagen. |
| 69 | 70 | """ |
| @@ -73,8 +74,8 @@ def trim(board, pv): |
| 73 | 74 | for i, move in enumerate(pv[:HARD_PLIES]): |
| 74 | 75 | if move not in probe.legal_moves: |
| 75 | 76 | break |
| 76 | | - if i >= MAX_PLIES and not (sharp or probe.is_capture(move)): |
| 77 | | - break # über MAX_PLIES hinaus nur, um einen Tausch oder ein Schach abzuschließen |
| 77 | + if i >= MAX_PLIES and not (sharp or probe.is_capture(move) or probe.gives_check(move)): |
| 78 | + break # über MAX_PLIES hinaus nur, um einen Tausch oder einen Angriff abzuschließen |
| 78 | 79 | captured = captured or probe.is_capture(move) |
| 79 | 80 | # Schach oder Gabel: die Antwort darauf gehört noch dazu |
| 80 | 81 | sharp = probe.gives_check(move) or bool(describe(probe, move, probe.turn)[1]) |
| @@ -85,7 +86,7 @@ def trim(board, pv): |
| 85 | 86 | if i % 2 == 1 and len(pv) > i + 1: |
| 86 | 87 | following = pv[i + 1] |
| 87 | 88 | if sharp or following in probe.legal_moves and (probe.is_capture(following) or |
| 88 | | - _mates(probe, following)): |
| 89 | + probe.gives_check(following)): |
| 89 | 90 | continue |
| 90 | 91 | if captured or i + 1 >= QUIET_PLIES: |
| 91 | 92 | break |
| @@ -128,6 +129,32 @@ def _hanging(board, square): |
| 128 | 129 | return min(VALUE[board.piece_type_at(a)] for a in attackers) < VALUE[piece.piece_type] |
| 129 | 130 | |
| 130 | 131 | |
| 132 | +def _pawn_attack(board, square): |
| 133 | + """Bauernzug, mit dem der Gegner die Figur auf ``square`` angreifen könnte (der Bauer stünde dabei |
| 134 | + nicht einfach umsonst), sonst None. Nur für Figuren, die mehr wert sind als ein Bauer.""" |
| 135 | + piece = board.piece_at(square) |
| 136 | + if piece is None or piece.piece_type in (chess.PAWN, chess.KING): |
| 137 | + return None |
| 138 | + probe = board.copy(stack=False) |
| 139 | + if probe.turn == piece.color: |
| 140 | + if probe.is_check(): |
| 141 | + return None |
| 142 | + probe.push(chess.Move.null()) |
| 143 | + if any(probe.piece_type_at(a) == chess.PAWN for a in probe.attackers(not piece.color, square)): |
| 144 | + return None # schon angegriffen: das ist „in Gefahr“, kein drohender Angriff |
| 145 | + for push in probe.legal_moves: |
| 146 | + if probe.piece_type_at(push.from_square) != chess.PAWN or probe.is_capture(push): |
| 147 | + continue |
| 148 | + if not chess.BB_PAWN_ATTACKS[not piece.color][push.to_square] & chess.BB_SQUARES[square]: |
| 149 | + continue |
| 150 | + after = probe.copy(stack=False) |
| 151 | + after.push(push) |
| 152 | + if after.attackers(piece.color, push.to_square) and not after.attackers(not piece.color, push.to_square): |
| 153 | + continue # den Bauern könnte man einfach schlagen |
| 154 | + return push |
| 155 | + return None |
| 156 | + |
| 157 | + |
| 131 | 158 | def describe(before, move, hero): |
| 132 | 159 | """Sätze und Kreise ((feld, feld, Farbe), …) zu einem Zug der Variante.""" |
| 133 | 160 | mover = before.turn |
| @@ -181,6 +208,12 @@ def describe(before, move, hero): |
| 181 | 208 | sentences.append(f"{NOMINATIVE[piece.piece_type][0].upper()}{NOMINATIVE[piece.piece_type][1:]} " |
| 182 | 209 | "stand in Gefahr und ist jetzt in Sicherheit.") |
| 183 | 210 | |
| 211 | + push = _pawn_attack(before, move.from_square) if you and not sentences and piece else None |
| 212 | + if push is not None and _pawn_attack(after, move.to_square) is None: |
| 213 | + sentences.append(f"{NOMINATIVE[piece.piece_type][0].upper()}{NOMINATIVE[piece.piece_type][1:]} " |
| 214 | + f"geht dem Angriff {_san(board_null(before), push)} aus dem Weg.") |
| 215 | + shapes.append((push.from_square, push.to_square, "threat")) |
| 216 | + |
| 184 | 217 | if you and not sentences and piece and piece.piece_type == chess.PAWN \ |
| 185 | 218 | and move.to_square in (chess.D4, chess.E4, chess.D5, chess.E5): |
| 186 | 219 | sentences.append("Der Bauer besetzt das Zentrum.") |
| @@ -424,10 +457,11 @@ def prune(tree): |
| 424 | 457 | return tree |
| 425 | 458 | |
| 426 | 459 | |
| 427 | | -def comment(tree, threat=None, only_move=False, better=None): |
| 460 | +def comment(tree, threat=None, only_move=False, better=None, why=None, rival=None): |
| 428 | 461 | """Kommentare und Kreise für jeden Knoten (``node.comment``, ``meta["shapes"]``). |
| 429 | 462 | |
| 430 | | - ``better``: ein besserer Zug als der erste (für „Warum nicht?“).""" |
| 463 | + ``better``: ein besserer Zug als der erste (für „Warum nicht?“), ``why``: Satz, was er gebracht |
| 464 | + hätte. ``rival``: (Zug, Abstand in Prozentpunkten), wenn Stockfish einen anderen Zug vorzieht.""" |
| 431 | 465 | board = tree.game.board() |
| 432 | 466 | hero = tree.hero |
| 433 | 467 | start = material(board, hero) |
| @@ -448,7 +482,12 @@ def comment(tree, threat=None, only_move |
| 448 | 482 | if only_move: |
| 449 | 483 | sentences.append("Alle anderen Züge sind deutlich schlechter.") |
| 450 | 484 | if better is not None: |
| 451 | | - sentences.append(f"Besser war {_san(board, better)}.") |
| 485 | + name = _san(board, better) |
| 486 | + sentences.append(f"Besser war {name}." if why is None else f"Besser war {name}: {why}") |
| 487 | + elif rival is not None: |
| 488 | + other, gap = rival |
| 489 | + sentences.append(f"Stockfish findet {_san(board, other)} " |
| 490 | + + ("ähnlich gut." if gap < RIVAL_GAP else "besser.")) |
| 452 | 491 | elif not sentences: |
| 453 | 492 | sentences.append("Stockfish hält das für den besten Zug.") |
| 454 | 493 | elif before.turn != hero: |
| @@ -582,21 +621,23 @@ def explain_job(board, move, maia=None, |
| 582 | 621 | return job |
| 583 | 622 | |
| 584 | 623 | |
| 585 | | -def _explain(engine, board, move, maia, name, limit, hero=None, better=None, cancel=None): |
| 624 | +def _explain(engine, board, move, maia, name, limit, hero=None, better=None, cancel=None, why=None): |
| 586 | 625 | _check(cancel) |
| 587 | 626 | tree = grow(board, move, engine, maia, name, limit, hero=hero, cancel=cancel) |
| 588 | 627 | _check(cancel) |
| 589 | 628 | if board.turn != tree.hero: |
| 590 | 629 | return comment(prune(tree)) # Zug des Gegners: keine Drohung, kein „einziger Zug“ |
| 591 | 630 | played = tree.meta[tree.first()]["wp"] |
| 592 | | - only_move = False |
| 631 | + only_move, rival = False, None |
| 593 | 632 | if better is None: |
| 594 | | - others = [win_pct(i["score"], board.turn) |
| 595 | | - for i in engine.analyse(board, limit, multipv=3) |
| 596 | | - if i.get("pv") and i["pv"][0] != move and "score" in i] |
| 633 | + lines = [(i["pv"][0], win_pct(i["score"], board.turn)) |
| 634 | + for i in engine.analyse(board, limit, multipv=3) if i.get("pv") and "score" in i] |
| 635 | + others = [wp for m, wp in lines if m != move] |
| 597 | 636 | only_move = bool(others) and played - max(others) >= ONLY_MOVE_GAP |
| 637 | + if lines and lines[0][0] != move: |
| 638 | + rival = (lines[0][0], max(0.0, lines[0][1] - played)) |
| 598 | 639 | threat = None if board.is_check() else threat_job(board, None, played)(engine) |
| 599 | | - return comment(prune(tree), threat, only_move, better) |
| 640 | + return comment(prune(tree), threat, only_move, better, why, rival) |
| 600 | 641 | |
| 601 | 642 | |
| 602 | 643 | def why_not_job(board, move, maia=None, name="Maia", limit=EXPLAIN_LIMIT): |
| @@ -609,8 +650,9 @@ def why_not_job(board, move, maia=None, |
| 609 | 650 | best = (info.get("pv") or [None])[0] |
| 610 | 651 | if best is None or best == move: |
| 611 | 652 | return None |
| 612 | | - tree = _explain(engine, board, move, maia, name, limit, better=best) |
| 613 | 653 | other = _explain(engine, board, best, maia, name, limit) |
| 654 | + played = win_pct(engine.analyse(board, limit, root_moves=[move])["score"], board.turn) |
| 655 | + tree = _explain(engine, board, move, maia, name, limit, better=best, why=_gained(other, played)) |
| 614 | 656 | graft(tree, tree.game, other) |
| 615 | 657 | tree.info(tree.first())["label"] = "Gespielt" |
| 616 | 658 | tree.info(tree.game.variations[1])["label"] = "Bester Zug" |
| @@ -619,6 +661,28 @@ def why_not_job(board, move, maia=None, |
| 619 | 661 | return job |
| 620 | 662 | |
| 621 | 663 | |
| 664 | +def _outcome(tree): |
| 665 | + """Ende der Hauptlinie: (führt zum Matt, Materialgewinn, eigene Gewinnchance).""" |
| 666 | + leaf = tree.game.end() |
| 667 | + if leaf is tree.game: |
| 668 | + return False, 0, 50.0 |
| 669 | + after = leaf.board() |
| 670 | + mate = after.is_checkmate() and after.turn != tree.hero or bool(tree.meta.get(leaf, {}).get("mate")) |
| 671 | + return mate, material(after, tree.hero) - material(tree.game.board(), tree.hero), _chance(tree, leaf) |
| 672 | + |
| 673 | + |
| 674 | +def _gained(best, played): |
| 675 | + """Was der bessere Zug gebracht hätte, als Satz, mit dem Weg dorthin. ``played``: Gewinnchance |
| 676 | + nach dem gespielten Zug.""" |
| 677 | + mate, gain, chance = _outcome(best) |
| 678 | + where = "Den Weg zeigt der Pfeil „Bester Zug“ in der Ausgangsstellung (mit <)." |
| 679 | + if mate: |
| 680 | + return f"Damit setzt du matt. {where}" |
| 681 | + if gain > 0: |
| 682 | + return f"Damit gewinnst du {gain_text(gain)}. {where}" |
| 683 | + return f"Damit stehst du besser (Gewinnchance {round(chance)} statt {round(played)} %). {where}" |
| 684 | + |
| 685 | + |
| 622 | 686 | def explore_job(board, move, hero, maia=None, name="Maia", limit=EXPLAIN_LIMIT): |
| 623 | 687 | """Job fürs Analysebrett: ``move`` selbst gezogen. Für ``hero`` mit „Besser war …“, wenn es |
| 624 | 688 | einen besseren Zug gibt; für den Gegner antwortet Stockfish.""" |