forked from niklasf/python-chess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyglot_tree.py
More file actions
executable file
·39 lines (29 loc) · 1.07 KB
/
polyglot_tree.py
File metadata and controls
executable file
·39 lines (29 loc) · 1.07 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Print a polyglot opening book in tree form."""
import chess
import chess.polyglot
import argparse
def print_tree(args, visited, level=0):
if level >= args.depth:
return
zobrist_hash = chess.polyglot.zobrist_hash(args.board)
if zobrist_hash in visited:
return
visited.add(zobrist_hash)
for entry in args.book.find_all(zobrist_hash):
print("{}├─ \033[1m{}\033[0m (weight: {}, learn: {})".format(
"| " * level,
args.board.san(entry.move()),
entry.weight,
entry.learn))
args.board.push(entry.move())
print_tree(args, visited, level + 1)
args.board.pop()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("book", type=chess.polyglot.open_reader)
parser.add_argument("--depth", type=int, default=5)
parser.add_argument("--fen", type=chess.Board, default=chess.Board(), dest="board")
args = parser.parse_args()
print_tree(args, visited=set())