-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
86 lines (58 loc) · 1.75 KB
/
game.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
import chess
import time
import chess.polyglot
from search import minimax
class Game():
def __init__(self):
self.depth = 4
self.timeout = 60
self.start_time = time.time()
self.board = chess.Board()
self.white_castled = False
self.black_castled = False
self.count = 0
def move(self, move):
if self.board.turn == chess.WHITE:
if (not self.is_endgame()
and not self.white_castled
and self.board.is_castling(move)):
self.white_castled = True
else:
if (not self.is_endgame()
and not self.black_castled
and self.board.is_castling(move)):
self.black_castled = True
self.board.push(move)
def undo(self):
last_move = self.board.pop()
if self.board.turn == chess.WHITE:
if not self.is_endgame() and self.board.is_castling(last_move):
self.white_castled = False
else:
if not self.is_endgame() and self.board.is_castling(last_move):
self.black_castled = False
def go(self):
if not self.board.is_game_over():
if self.is_opening():
try:
move = self.look_up()
time.sleep(2)
return move
except IndexError:
pass
self.start_time = time.time()
best_move = minimax(self,
self.depth,
self.board.turn,
self.timeout)
return best_move
def look_up(self):
with chess.polyglot.open_reader("data/Formula12.bin") as reader:
return reader.find(self.board).move()
def is_opening(self):
return self.board.fullmove_number <= 10
def is_middlegame(self):
return (self.board.fullmove_number > 10
and self.board.fullmove_number >= 20)
def is_endgame(self):
return self.board.fullmove_number > 20