-
Notifications
You must be signed in to change notification settings - Fork 3
/
bare.py
executable file
·177 lines (151 loc) · 4.11 KB
/
bare.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
#!/usr/bin/env python3
# A Python chess engine,
# inspired by (but not compatible with)
# http://en.chessbase.com/post/reconstructing-turing-s-paper-machine
from pst import pst
import chess as c
import sys, math, time
# computer plays as Black by default
COMPC = c.BLACK
PLAYC = c.WHITE
MAXPLIES = 4 # inital maximum search depth
PSTAB = .5 # influence of piece-square table on moves, 0 = none
b = c.Board()
def getpos(b):
"Get positional-play value for a board"
ppv = 0
for i in b.piece_map().keys():
m = b.piece_at(i)
if m and m.color == COMPC:
mm = m.piece_type
if mm == c.KING and (
len(b.pieces(c.PAWN, COMPC)) + len(b.pieces(c.PAWN, PLAYC)) ) <= 8: # endgame is different
mm = 8 # for the King
if COMPC == c.WHITE:
j, k = i // 8, i % 8
ppv += PSTAB * pst[mm][8 * (7 - j) + k] / 100
else:
ppv -= PSTAB * pst[mm][i] / 100
return ppv
def getval(b):
"Get total piece value of board"
return (
len(b.pieces(c.PAWN, c.WHITE)) - len(b.pieces(c.PAWN, c.BLACK))
+ 2.8 * (len(b.pieces(c.KNIGHT, c.WHITE)) - len(b.pieces(c.KNIGHT, c.BLACK)))
+ 3.2 * (len(b.pieces(c.BISHOP, c.WHITE)) - len(b.pieces(c.BISHOP, c.BLACK)))
+ 4.79 * (len(b.pieces(c.ROOK, c.WHITE)) - len(b.pieces(c.ROOK, c.BLACK)))
+ 9.29 * (len(b.pieces(c.QUEEN, c.WHITE)) - len(b.pieces(c.QUEEN, c.BLACK)))
)
# https://chessprogramming.wikispaces.com/Alpha-Beta
def searchmax(b, ply, alpha, beta):
"Search moves and evaluate positions"
if ply >= MAXPLIES:
return getval(b)
for x in order(b, ply):
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"
if ply >= MAXPLIES:
return getval(b)
for x in order(b, ply):
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 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
lastpos = getpos(b)
ll = []
if b.turn == c.WHITE:
COMPC = c.WHITE
PLAYC = c.BLACK
else:
COMPC = c.BLACK
PLAYC = c.WHITE
if not silent:
print(b)
print(getval(b))
print("FEN:", b.fen())
nl = len(list(b.legal_moves))
for n, x in enumerate(b.legal_moves):
b.push(x)
p = getpos(b) - lastpos
if COMPC == c.WHITE:
t = searchmin(b, 0, -1e6, 1e6)
else:
t = searchmax(b, 0, -1e6, 1e6)
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] + m[2])
if COMPC == c.WHITE:
ll.reverse()
print('# %.2f %s' % (ll[0][1] + ll[0][2], [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)
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