-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbook.py
More file actions
88 lines (69 loc) · 2.44 KB
/
Copy pathbook.py
File metadata and controls
88 lines (69 loc) · 2.44 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
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
import chess.polyglot
import os
from config import config
import sys
import atexit
from typing import Optional,List,Dict,Tuple,Dict
from chess.polyglot import zobrist_hash
def get_base_path():
if getattr(sys, 'frozen', False): # If it's a PyInstaller executable
return os.path.dirname(sys.executable)
else:
return os.path.dirname(os.path.abspath(__file__))
BASE_PATH = get_base_path()
BOOKS_FOLDER = os.path.join(BASE_PATH, "books")
if not os.path.exists(BOOKS_FOLDER):
os.makedirs(BOOKS_FOLDER) # create the folder (and all necessary subfolders)
book:chess.polyglot.MemoryMappedReader = None
def open_book():
"""
Opens a book file
Args:
bookFileName: the name of the book file to open
Returns:
a MemoryMappedReader object
"""
global book
if not config.book:
# No opening book configured -> skip cleanly. Otherwise join(folder, "")
# points at the books folder itself and we'd try to open a directory,
# producing a misleading "permission denied / is a directory" warning.
return
try:
book = chess.polyglot.MemoryMappedReader(os.path.abspath(os.path.join(BOOKS_FOLDER,config.book)))
num_positions = sum(1 for _ in book)
print(f"Book {config.book} loaded successfully. {num_positions} position found")
except FileNotFoundError:
print(f"Book file {config.book} not found. Please check the configuration.")
except Exception as e:
print(f"An error occurred while opening the book: {e}")
def close_book():
global book
if book:
book.close()
book = None
atexit.register(close_book)
def getMovesFromBook(board: chess.Board) -> List[chess.polyglot.Entry]:
"""
Gets all moves from the book for the given board position
Args:
board: chess.Board current position
Returns:
a chess.Move object if found, None otherwise
"""
return [m for m in book.find_all(board,minimum_weight=0)]
def isInBook(board:chess.Board)->bool:
"""
Check if a board position is in current book
Returns
True if board is in book
"""
m = book.get(board, minimum_weight=0)
if m is None:
return False
return True
if __name__ == "__main__":
open_book()
board = chess.Board("rnbqkb1r/ppp1pppp/3p1n2/8/3PP3/8/PPP2PPP/RNBQKBNR w KQkq - 1 3")
print(hex(zobrist_hash(board)))
close_book()