| @@ -61,6 +61,60 @@ def gain_text(gain): |
| 61 | 61 | return "einen Bauern" |
| 62 | 62 | |
| 63 | 63 | |
| 64 | +INDEFINITE = {chess.PAWN: "einen Bauern", chess.KNIGHT: "einen Springer", chess.BISHOP: "einen Läufer", |
| 65 | + chess.ROOK: "einen Turm", chess.QUEEN: "die Dame"} |
| 66 | +NUMBER = {2: "zwei", 3: "drei", 4: "vier", 5: "fünf", 6: "sechs", 7: "sieben", 8: "acht"} |
| 67 | + |
| 68 | + |
| 69 | +def pieces_text(counts): |
| 70 | + """{Figurenart: Anzahl} als Wörter, wertvollste zuerst: „einen Turm und zwei Bauern“.""" |
| 71 | + parts = [] |
| 72 | + for kind in (chess.QUEEN, chess.ROOK, chess.BISHOP, chess.KNIGHT, chess.PAWN): |
| 73 | + n = counts.get(kind, 0) |
| 74 | + if n == 1: |
| 75 | + parts.append(INDEFINITE[kind]) |
| 76 | + elif n > 1: |
| 77 | + parts.append(f"{NUMBER.get(n, n)} {PLURAL[kind]}") |
| 78 | + return parts[0] if len(parts) == 1 else ", ".join(parts[:-1]) + " und " + parts[-1] |
| 79 | + |
| 80 | + |
| 81 | +def trade(before, after, color): |
| 82 | + """Was ``color`` zwischen zwei Stellungen hergegeben und gewonnen hat: ({Art: Anzahl}, {Art: Anzahl}). |
| 83 | + None, wenn umgewandelt wurde (dann stimmen die Zählungen nicht).""" |
| 84 | + sides = [] |
| 85 | + for side in (color, not color): |
| 86 | + counts = {} |
| 87 | + for kind in (chess.PAWN, chess.KNIGHT, chess.BISHOP, chess.ROOK, chess.QUEEN): |
| 88 | + lost = len(before.pieces(kind, side)) - len(after.pieces(kind, side)) |
| 89 | + if lost < 0: |
| 90 | + return None |
| 91 | + if lost: |
| 92 | + counts[kind] = lost |
| 93 | + sides.append(counts) |
| 94 | + return sides[0], sides[1] |
| 95 | + |
| 96 | + |
| 97 | +def trade_text(before, after, color): |
| 98 | + """Ergebnis eines Tauschs aus Sicht von ``color`` (ohne Punkt), None wenn nichts geschlagen wurde.""" |
| 99 | + traded = trade(before, after, color) |
| 100 | + gain = material(after, color) - material(before, color) |
| 101 | + if traded is None: # Umwandlung: nur der Saldo |
| 102 | + return (f"Du gewinnst {gain_text(gain)}" if gain > 0 else |
| 103 | + f"Du gibst {gain_text(-gain)} her" if gain < 0 else None) |
| 104 | + given, won = traded |
| 105 | + if not given and not won: |
| 106 | + return None |
| 107 | + if not given: |
| 108 | + return f"Du gewinnst {pieces_text(won)}" |
| 109 | + if not won: |
| 110 | + return f"Du gibst {pieces_text(given)} her" |
| 111 | + if given == won: |
| 112 | + return "ein Abtausch, das Material bleibt gleich" |
| 113 | + if gain == 0: |
| 114 | + return f"Du tauschst {pieces_text(given)} gegen {pieces_text(won)}" |
| 115 | + return f"Du gibst {pieces_text(given)} und bekommst {pieces_text(won)}" |
| 116 | + |
| 117 | + |
| 64 | 118 | def trim(board, pv): |
| 65 | 119 | """Hauptvariante kürzen: bis Matt, bis ein Tausch abgeschlossen ist, höchstens MAX_PLIES. |
| 66 | 120 | |
| @@ -522,16 +576,12 @@ def comment(tree, threat=None, only_move |
| 522 | 576 | shapes.append((mate.from_square, mate.to_square, "hint")) |
| 523 | 577 | if not node.variations and not after.is_checkmate(): |
| 524 | 578 | gain = material(after, hero) - start |
| 579 | + result = trade_text(board, after, hero) |
| 525 | 580 | if meta.get("mate"): |
| 526 | 581 | sentences.append("Das führt zum Matt.") |
| 527 | | - elif gain > 0: |
| 528 | | - sentences.append(f"Ergebnis: Du gewinnst {gain_text(gain)}.") |
| 529 | | - elif gain < 0 and _chance(tree, node) >= 50: |
| 530 | | - sentences.append(f"Ergebnis: Du gibst {gain_text(-gain)} her, stehst danach aber gut.") |
| 531 | | - elif gain < 0: |
| 532 | | - sentences.append(f"Ergebnis: Du gibst {gain_text(-gain)} her.") |
| 533 | | - elif _captures(node): |
| 534 | | - sentences.append("Ergebnis: ein Abtausch, das Material bleibt gleich.") |
| 582 | + elif result is not None: |
| 583 | + good = ", stehst danach aber gut" if gain < 0 and _chance(tree, node) >= 50 else "" |
| 584 | + sentences.append(f"Ergebnis: {result}{good}.") |
| 535 | 585 | node.comment = " ".join(sentences) |
| 536 | 586 | meta["shapes"] = tuple(shapes) |
| 537 | 587 | return tree |
| @@ -546,15 +596,6 @@ def _chance(tree, node): |
| 546 | 596 | return 50.0 |
| 547 | 597 | |
| 548 | 598 | |
| 549 | | -def _captures(node): |
| 550 | | - """Wurde auf dem Weg zu ``node`` geschlagen?""" |
| 551 | | - while node.parent is not None: |
| 552 | | - if node.parent.board().is_capture(node.move): |
| 553 | | - return True |
| 554 | | - node = node.parent |
| 555 | | - return False |
| 556 | | - |
| 557 | | - |
| 558 | 599 | def line(board, pv): |
| 559 | 600 | """Linearer Baum aus einer festen Variante (ohne Engine, z. B. für Tests).""" |
| 560 | 601 | game = chess.pgn.Game() |
| @@ -678,6 +719,11 @@ def _gained(best, played): |
| 678 | 719 | where = "Den Weg zeigt der Pfeil „Bester Zug“ in der Ausgangsstellung (mit <)." |
| 679 | 720 | if mate: |
| 680 | 721 | return f"Damit setzt du matt. {where}" |
| 722 | + traded = trade(best.game.board(), best.game.end().board(), best.hero) |
| 723 | + if gain > 0 and traded is not None and traded[0]: |
| 724 | + return f"Damit bekommst du {pieces_text(traded[1])} für {pieces_text(traded[0])}. {where}" |
| 725 | + if gain > 0 and traded is not None: |
| 726 | + return f"Damit gewinnst du {pieces_text(traded[1])}. {where}" |
| 681 | 727 | if gain > 0: |
| 682 | 728 | return f"Damit gewinnst du {gain_text(gain)}. {where}" |
| 683 | 729 | return f"Damit stehst du besser (Gewinnchance {round(chance)} statt {round(played)} %). {where}" |