-
Notifications
You must be signed in to change notification settings - Fork 5
/
play.py
216 lines (189 loc) · 5.76 KB
/
play.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#alpha beta pruning
from keras.models import Model, load_model
import keras
import chess
import copy
import numpy as np
from gamestate import GameState
import tensorflow as tf
import chess.pgn
supervised_path = "./network/DeepLearningModel.h5"
model = load_model(supervised_path)
board = chess.Board()
board2 = chess.Board()
board.push_san("e4")
board.push_san("e5")
board.push_san("Qh5")
board.push_san("Nf6")
board.push_san("a3")
board.push_san("Nxh5")
board.push_san("b3")
board2.push_san("e3")
board2.push_san("e5")
board2.push_san("Nf3")
board2.push_san("e4")
board2.push_san("a3")
board2.push_san("exf3")
board2.push_san("b4")
movemap = {}
def alpha_beta_search(alpha_pos, beta_pos, curr, max_depth, white):
if max_depth == 0:
return curr
if white == True:
first = False
for move in curr.legal_moves:
new_node = copy.deepcopy(curr)
new_node.push(move)
if first == False:
leaf = alpha_beta_search(alpha_pos, beta_pos, new_node, max_depth-1, False)
first = True
if alpha_pos == -1:
alpha_pos = leaf
second = alpha_beta_search(alpha_pos, beta_pos, new_node, max_depth-1, False)
output = prediction(leaf, second)
try:
output = output.numpy()
except:
pass
if output[0][0] > output[0][1]:
better = leaf
else:
better = second
output2 = prediction(alpha_pos, better)
try:
output2 = output2.numpy()
except:
pass
if output2[0][0] > output2[0][1]:
pass
else:
alpha_pos = better
if beta_pos != 1:
final = prediction(alpha_pos, beta_pos)
try:
final = final.numpy()
except:
pass
if final[0][0] > final[0][1]:
break
return better
else:
first = False
for move in curr.legal_moves:
new_node = copy.deepcopy(curr)
new_node.push(move)
if first == False:
leaf = alpha_beta_search(alpha_pos, beta_pos, new_node, max_depth-1, True)
first = True
if beta_pos == 1:
beta_pos = leaf
second = alpha_beta_search(alpha_pos, beta_pos, new_node, max_depth-1, True)
output = prediction(leaf, second)
try:
output = output.numpy()
except:
pass
if (output[0][1]) > (output[0][0]):
better = second
else:
better = leaf
output2 = prediction(beta_pos, better)
try:
output2 = output2.numpy()
except:
pass
if (output2[0][1]) > (output2[0][0]):
beta_pos = better
else:
pass
if alpha_pos != -1:
final = prediction(alpha_pos, beta_pos)
try:
final = final.numpy()
except:
pass
if (final[0][0]) > (final[0][1]):
break
return better
def prediction(pos1, pos2):
global model
global movemap
if pos1.fen() in movemap and pos2.fen() in movemap:
return np.array([[movemap[pos1.fen()], movemap[pos2.fen()]], []])
else:
new1 = np.array(GameState(pos1).bit_encode())[np.newaxis]
new2 = np.array(GameState(pos2).bit_encode())[np.newaxis]
pos = [[new1],[new2]]
output = model(pos)
temp = output.numpy()
movemap[pos1.fen()] = temp[0][0]
movemap[pos2.fen()] = temp[0][1]
return output
#tf.Tensor([[0.14621536 0.8537846 ]], shape=(1, 2), dtype=float32)
def computer(board, depth):
alpha = -1
beta = 1
first = False
for move in board.legal_moves:
new_node = copy.deepcopy(board)
new_node.push(move)
if first == False:
output = alpha_beta_search(alpha, beta, new_node, depth-1, False)
best = move
if alpha == -1:
alpha = output
first = True
else:
temp = alpha_beta_search(alpha, beta, new_node, depth-1, False)
new_output = prediction(temp, output)
try:
new_output = new_output.numpy()
except:
pass
if (new_output[0][0]) > (new_output[0][1]):
new_output2 = temp
else:
new_output2 = output
if new_output2 != output:
best = move
output = new_output2
alpha = prediction(alpha, output)
try:
alpha = alpha.numpy()
except:
pass
if (alpha[0][0]) > (alpha[0][1]):
pass
else:
alpha = output
print(best)
board.push(best)
return board
def player(board):
while 1==1:
try:
move = input("Enter player move: \n")
board.push_san(move)
break
except ValueError:
print("Illegal move; please enter a new move:")
return board
def play():
moveNum = 0
board = chess.Board()
depth = input("Enter max search depth \n")
depth = int(depth)
while 1==1:
if board.is_game_over() == True:
break
print(board)
if moveNum % 2 == 0:
board = computer(board, depth) #computer is white
else:
board = player(board)
moveNum += 1
print(board)
print("Game over.")
game = chess.pgn.Game.from_board(board)
print(game)
play()