forked from niklasf/python-chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perft.py
executable file
·161 lines (126 loc) · 4.58 KB
/
perft.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# A test set of more than 6000 positions. The perft node count for a given depth
# is the number of legal move sequences from that position. This can be used
# to check correctness and speed of the legal move generator.
#
# The original test suite was created by Marcel van Kervinck and found at
# http://marcelk.net/rookie/nostalgia/v3/perft-random.epd
from __future__ import print_function
import chess
import unittest
import sys
def perft(board, depth):
if depth > 1:
count = 0
for move in board.legal_moves:
board.push(move)
count += perft(board, depth - 1)
board.pop()
return count
elif depth == 1:
return len(board.legal_moves)
else:
return 1
def into_check_perft(board, depth):
if depth >= 1:
count = 0
for move in board.pseudo_legal_moves:
if board.is_into_check(move):
continue
board.push(move)
count += into_check_perft(board, depth - 1)
board.pop()
return count
else:
return 1
def debug_perft(board, depth):
assert board.is_valid()
if depth >= 1:
count = 0
for move in board.pseudo_legal_moves:
assert move in board.pseudo_legal_moves, (move, board)
if board.is_into_check(move):
assert move not in board.legal_moves, (move, board)
continue
assert move in board.legal_moves, (move, board)
assert move == board.parse_san(board.san(move))
board.push(move)
count += debug_perft(board, depth - 1)
board.pop()
return count
else:
return 1
class PerftTestCase(unittest.TestCase):
def execute_test(self, method, maxnodes):
current_id = None
board = chess.Board()
with open("data/perft-random.epd") as data:
for line in data:
s = line.split()
if not s:
pass
elif s[0] == "id":
current_id = s[1]
sys.stdout.write(".")
sys.stdout.flush()
elif s[0] == "epd":
board.set_epd(" ".join(s[1:]))
elif s[0] == "perft":
depth = int(s[1])
nodes = int(s[2])
if nodes < maxnodes:
self.assertEqual(method(board, depth), nodes, current_id)
sys.stdout.write("\n")
sys.stdout.flush()
def test_into_check(self):
self.execute_test(into_check_perft, 1000)
def test_debug(self):
self.execute_test(debug_perft, 100)
def test_speed(self):
self.execute_test(perft, 10000)
def test_chess960(self):
# Source: http://www.talkchess.com/forum/viewtopic.php?t=55274
# XFEN 00
board = chess.Board("r1k1r2q/p1ppp1pp/8/8/8/8/P1PPP1PP/R1K1R2Q w KQkq - 0 1 ")
self.assertEqual(perft(board, 1), 23)
self.assertEqual(perft(board, 2), 522)
self.assertEqual(perft(board, 3), 12333)
self.assertEqual(perft(board, 4), 285754)
sys.stdout.write(".")
sys.stdout.flush()
# XFEN 01
board = chess.Board("r1k2r1q/p1ppp1pp/8/8/8/8/P1PPP1PP/R1K2R1Q w KQkq - 0 1")
self.assertEqual(perft(board, 1), 28)
self.assertEqual(perft(board, 2), 738)
self.assertEqual(perft(board, 3), 20218)
self.assertEqual(perft(board, 4), 541480)
sys.stdout.write(".")
sys.stdout.flush()
# XFEN 02
board = chess.Board("8/8/8/4B2b/6nN/8/5P2/2R1K2k w Q - 0 1")
self.assertEqual(perft(board, 1), 34)
self.assertEqual(perft(board, 2), 318)
self.assertEqual(perft(board, 3), 9002)
self.assertEqual(perft(board, 4), 118388)
sys.stdout.write(".")
sys.stdout.flush()
# XFEN 03
board = chess.Board("2r5/8/8/8/8/8/6PP/k2KR3 w K - 0 1")
self.assertEqual(perft(board, 1), 17)
self.assertEqual(perft(board, 2), 242)
self.assertEqual(perft(board, 3), 3931)
self.assertEqual(perft(board, 4), 57700)
sys.stdout.write(".")
sys.stdout.flush()
# XFEN 04
board = chess.Board("4r3/3k4/8/8/8/8/6PP/qR1K1R2 w KQ - 0 1")
self.assertEqual(perft(board, 1), 19)
self.assertEqual(perft(board, 2), 628)
self.assertEqual(perft(board, 3), 12858)
self.assertEqual(perft(board, 4), 405636)
sys.stdout.write(".\n")
sys.stdout.flush()
if __name__ == "__main__":
unittest.main()