Public repository: timo / schach

Reader

Ein aufgeräumtes Schach-Programm für GNOME

schach / tests / test_annotate.py · 4.4 KB · at 3c63b1d52560 · raw

Outline: 18 definitions
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()