-
Notifications
You must be signed in to change notification settings - Fork 4
/
shannon.py
305 lines (275 loc) · 6.64 KB
/
shannon.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python3
import chess as c
import sys, math, time
# computer plays as Black by default
COMPC = c.BLACK
PLAYC = c.WHITE
MAXPLIES = 1 # maximum search depth
QPLIES = MAXPLIES + 6
MATETEST = True # if True, include draw and mate on next move detection in the material eval
PAWNRULE = True # use pawn criteria
b = c.Board()
NODES = 0
def getpawnfile(b, col):
pf = 10 * [0]
for i in b.piece_map().keys():
m = b.piece_at(i)
if m and m.color == col:
mm = m.piece_type
if mm == c.PAWN:
mf = c.square_file(i)
pf[mf + 1] += 1
return pf
def getiso(pf):
iso = 0
for i in range(1, 9):
if pf[i] > 0 and pf[i - 1] == 0 and pf[i + 1] == 0:
iso += 1
return iso
def getdoub(pf):
doub = 0
for i in range(1, 9):
if pf[i] > 1:
doub += pf[i] - 1
return doub
def getback(b, col):
back = 0
for i in b.piece_map().keys():
m = b.piece_at(i)
if m and m.color == col:
mm = m.piece_type
if mm == c.PAWN:
sf = c.square_file(i)
left_f = max(0, sf - 1)
right_f = min(7, sf + 1)
sr = c.square_rank(i)
if col == c.WHITE:
top_r = sr
bot_r = 0
else:
top_r = 7
bot_r = sr
num_p = 0
for x in range(left_f, right_f + 1):
for y in range(bot_r, top_r + 1):
q = b.piece_at(8 * y + x)
if q and q.color == col and q.piece_type == c.PAWN:
num_p += 1
if num_p == 1:
if col == c.WHITE:
top_r = 7
bot_r = sr + 1
else:
top_r = sr - 1
bot_r = 0
for y in range(bot_r, top_r + 1):
j = 8 * y + sf
enpawn = False
for att in b.attackers(not col, j):
if b.piece_at(att).piece_type == c.PAWN:
enpawn = True
if enpawn:
back += 1
return back
def getval(b):
"Get total piece value of board"
v = (
len(b.pieces(c.PAWN, c.WHITE)) - len(b.pieces(c.PAWN, c.BLACK))
+ 3 * (len(b.pieces(c.KNIGHT, c.WHITE)) - len(b.pieces(c.KNIGHT, c.BLACK)))
+ 3 * (len(b.pieces(c.BISHOP, c.WHITE)) - len(b.pieces(c.BISHOP, c.BLACK)))
+ 5 * (len(b.pieces(c.ROOK, c.WHITE)) - len(b.pieces(c.ROOK, c.BLACK)))
+ 9 * (len(b.pieces(c.QUEEN, c.WHITE)) - len(b.pieces(c.QUEEN, c.BLACK)))
)
if PAWNRULE:
wf = getpawnfile(b, c.WHITE)
bf = getpawnfile(b, c.BLACK)
wiso = getiso(wf)
biso = getiso(bf)
wdoub = getdoub(wf)
bdoub = getdoub(bf)
wback = getback(b, c.WHITE)
bback = getback(b, c.BLACK)
v = v - .5 * (wdoub - bdoub + wiso - biso + wback - bback)
oturn = b.turn
b.turn = c.WHITE
wmov = len(list(b.legal_moves))
b.turn = c.BLACK
bmov = len(list(b.legal_moves))
b.turn = oturn
v += .1 * (wmov - bmov)
return v
def isdead(b, ml, p):
"Is the position dead? (quiescence)"
if p >= QPLIES or not len(ml):
return True
x = b.pop()
if (b.is_capture(x) and len(b.attackers(not b.turn, x.to_square))):
b.push(x)
return False
else:
b.push(x)
return True
# https://chessprogramming.org/Alpha-Beta
def searchmax(b, ply, alpha, beta):
"Search moves and evaluate positions"
global NODES
NODES += 1
if MATETEST:
res = b.result(claim_draw = True)
if res == '0-1':
return -1000
if res == '1-0':
return 1000
if res == '1/2-1/2':
return 0
ml = order(b, ply)
if ply >= MAXPLIES and isdead(b, ml, ply):
return getval(b)
if ply >= MAXPLIES:
ml2 = []
for x in ml:
if b.is_capture(x):
ml2.append(x)
if len(ml2) == 0: # no considerable moves
return getval(b)
else:
ml2 = ml
for x in ml2:
b.push(x)
t = searchmin(b, ply + 1, alpha, beta)
b.pop()
if t >= beta:
return beta
if t > alpha:
alpha = t
return alpha
def searchmin(b, ply, alpha, beta):
"Search moves and evaluate positions"
global NODES
NODES += 1
if MATETEST:
res = b.result(claim_draw = True)
if res == '0-1':
return -1000
if res == '1-0':
return 1000
if res == '1/2-1/2':
return 0
ml = order(b, ply)
if ply >= MAXPLIES and isdead(b, ml, ply):
return getval(b)
if ply >= MAXPLIES:
ml2 = []
for x in ml:
if b.is_capture(x):
ml2.append(x)
if len(ml2) == 0: # no considerable moves
return getval(b)
else:
ml2 = ml
for x in ml2:
b.push(x)
t = searchmax(b, ply + 1, alpha, beta)
b.pop()
if t <= alpha:
return alpha
if t < beta:
beta = t
return beta
def order(b, ply):
"Move ordering"
if ply > 0:
return list(b.legal_moves)
am, bm = [], []
for x in b.legal_moves:
if b.is_capture(x):
if b.piece_at(x.to_square):
# MVV/LVA sorting (http://home.hccnet.nl/h.g.muller/mvv.html)
am.append((x, 10 * b.piece_at(x.to_square).piece_type
- b.piece_at(x.from_square).piece_type))
else: # to square is empty during en passant capture
am.append((x, 10 - b.piece_at(x.from_square).piece_type))
else:
am.append((x, b.piece_at(x.from_square).piece_type))
am.sort(key = lambda m: m[1])
am.reverse()
bm = [q[0] for q in am]
return bm
def pm():
if COMPC == c.WHITE:
return 1
else:
return -1
def getmove(b, silent = False, usebook = False):
"Get move list for board"
global COMPC, PLAYC, MAXPLIES, NODES
ll = []
NODES = 0
if b.turn == c.WHITE:
COMPC = c.WHITE
PLAYC = c.BLACK
else:
COMPC = c.BLACK
PLAYC = c.WHITE
if not silent:
print(b.unicode())
print(getval(b))
print("FEN:", b.fen())
nl = len(list(b.legal_moves))
start = time.time()
for n, x in enumerate(b.legal_moves):
b.push(x)
p = 0
if COMPC == c.WHITE:
t = searchmin(b, 0, -1e6, 1e6)
else:
t = searchmax(b, 0, -1e6, 1e6)
if MATETEST:
res = b.result(claim_draw = True)
if res == '1/2-1/2':
t = 0
if res == '1-0':
t = 1e8
if res == '0-1':
t = -1e8
if not silent:
print("(%u/%u) %s %.1f %.2f" % (n + 1, nl, x, p, t))
ll.append((x, p, t))
b.pop()
ll.sort(key = lambda m: m[1] + 1000 * m[2])
if COMPC == c.WHITE:
ll.reverse()
print('# %.2f %s' % (ll[0][1] + ll[0][2], [str(ll[0][0])]))
print('info depth %d score cp %d time %d nodes %d pv %s' % (MAXPLIES + 1,
100 * pm () * ll[0][2], 1000 * (time.time() - start), NODES, str(ll[0][0])))
return ll[0][1] + ll[0][2], [str(ll[0][0])]
if __name__ == '__main__':
while True: # game loop
while True:
print(b.unicode())
print(getval(b))
if sys.version < '3':
move = raw_input("Your move? ")
else:
move = input("Your move? ")
try:
try:
b.push_san(move)
except ValueError:
b.push_uci(move)
except:
print("Sorry? Try again. (Or type Control-C to quit.)")
else:
break
if b.result() != '*':
print("Game result:", b.result())
break
tt = time.time()
t, m = getmove(b)
print("My move: %u. %s ( calculation time spent: %u m %u s )" % (
b.fullmove_number, m[0],
(time.time() - tt) // 60, (time.time() - tt) % 60))
b.push(c.Move.from_uci(m[0]))
if b.result() != '*':
print("Game result:", b.result())
break