-
Notifications
You must be signed in to change notification settings - Fork 4
/
plan.py
executable file
·166 lines (140 loc) · 3.72 KB
/
plan.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
#!/usr/bin/env python3
# A Python chess engine based on Plankalkül (1948) by Konrad Zuse
import chess as c
import sys, math, time
from random import random
# computer plays as Black by default
COMPC = c.BLACK
PLAYC = c.WHITE
MAXPLIES = 3 # maximum search depth
b = c.Board()
NODES = 0
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 * (len(b.pieces(c.KNIGHT, c.WHITE)) - len(b.pieces(c.KNIGHT, c.BLACK)))
+ 2 * (len(b.pieces(c.BISHOP, c.WHITE)) - len(b.pieces(c.BISHOP, c.BLACK)))
+ 3 * (len(b.pieces(c.ROOK, c.WHITE)) - len(b.pieces(c.ROOK, c.BLACK)))
+ 4 * (len(b.pieces(c.QUEEN, c.WHITE)) - len(b.pieces(c.QUEEN, c.BLACK)))
)
# https://chessprogramming.org/Alpha-Beta
def searchmax(b, ply, alpha, beta):
"Search moves and evaluate positions"
global NODES
NODES += 1
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"
global NODES
NODES += 1
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, 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 = random()
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] + 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