-
Notifications
You must be signed in to change notification settings - Fork 2
/
krevetka.py
428 lines (389 loc) · 13.2 KB
/
krevetka.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import chess
import chess.polyglot
import chess.engine
import chess.syzygy
import copy
import time
from random import *
import os
from tools import *
win = True
try:
from winsound import Beep
except:
win = False
coord = chess.Board()
def new_board():
global coord
coord = chess.Board()
def draw():
print("a b c d e f g h\n---------------")
print(coord)
print(evaluate(coord)) # mc_evaluation(coord))
def string_coord(board):
c=""
for i in str(board):
if i!="\n":
if i!=" ":
c+=i
return c
"""
def minimax(node, depth, maxplayer):
EVAL = evaluation(node)
if depth == 0 or EVAL in [0, -100, 100]:
return EVAL
if maxplayer:#blancs=True, nors=False
value = -100
for move in list(map(str,node.legal_moves)):
child = copy.copy(node)
child.push_san(move)
value = max(value, minimax(child, depth - 1, False))
else:
value = 100
for move in list(map(str,node.legal_moves)):
child = copy.copy(node)
child.push_san(move)
value = min(value, minimax(child, depth - 1, True))
return value
"""
def change_fen(fen):
global coord
coord = chess.Board(fen)
def evaluate(board):
c = string_coord(board)
score = 0
val = {" ": 0, ".": 0, "p": -100, "P": 100, "n": -320, "N": 320, "b": -330,
"B": 330, "q": -880, "Q": 880, "r": -510, "R": 510, "k": 0, "K": 0}
for i in c:
score += val[i] # material difference
for i in range(64):
if board.is_attacked_by(chess.WHITE, i): # possible moves
if i in [27, 28, 35, 36]: # in the center by white
score += 20
else:
score += 10
if board.is_attacked_by(chess.BLACK, i):
if i in [27, 28, 35, 36]: # in the center by black
score -= 20
else:
score -= 10
#score += c[8:16].count("P")*100 #pion passé 7e rangée
#score -= c[48:56].count("p")*100
#score += c[16:24].count("P")*80 #pion 6e rangée
#score -= c[40:48].count("p")*80
#score += c[:8].count("n")*5
#score -= c[56:].count("N")*5
#score += board.is_attacked_by(chess.WHITE, 27)+board.is_attacked_by(chess.WHITE, 28)+board.is_attacked_by(chess.WHITE, 35)+board.is_attacked_by(chess.WHITE, 36)
#score -= board.is_attacked_by(chess.BLACK, 27)+board.is_attacked_by(chess.BLACK, 28)+board.is_attacked_by(chess.BLACK, 35)+board.is_attacked_by(chess.BLACK, 36)
if "R" in c[8:16]:
score += 200 # tour en 7e rangée
if "r" in c[48:56]:
score -= 200
if board.is_stalemate() or coord.can_claim_draw():
score = 0
elif board.is_checkmate():
score = 10000
if board.turn:
score *= -1
return score
def use_syzygy(board):
try:
if len(list(board.legal_moves))>0:#"syzygy/3-4-5" in os.listdir() and len(coord.piece_map())<=5:
with chess.syzygy.open_tablebase("syzygy/3-4-5") as tablebase:
eval_pos = tablebase.get_dtz(coord)
if eval_pos != None:
print("table",tablebase.get_dtz(coord))
print("moves",len(list(board.legal_moves)))
for move in list(board.legal_moves):
#print(move)
Board=coord.copy()
Board.push_san(str(move))
if Board.is_checkmate():
return str(move)
print(str(move),tablebase.get_dtz(Board))
eval_move=tablebase.get_dtz(Board)
if eval_pos==-eval_move+1 and eval_move!=0:
return str(move)
else:
return ""
return ""
except Exception as e:
print(e)
return ""
def analyse_position(board, engine, time_thinking=1, time_is_depth=False):
"""TODO"""
if time_is_depth:
analysis = engine.analyse(board, chess.engine.Limit(depth=time_thinking))
else:
analysis = engine.analyse(board, chess.engine.Limit(time=time_thinking))
return {"score": analysis["score"],
"moves": analysis["pv"],
"depth": analysis["depth"]}
def play_engine(board, engine, book="Nothing", time_thinking=1, time_is_depth=False):
"""using other chess engines"""
move = play_auto(board, book)
if move != None:
board.push_san(move)
else:
if time_is_depth:
result = engine.play(board, chess.engine.Limit(depth=time_thinking))
else:
result = engine.play(board, chess.engine.Limit(time=time_thinking))
board.push(result.move)
draw()
def play_auto(board, book="Nothing"):
"""playing automatically some moves"""
opening_moves = []
move = None
print(book)
if "polyglot" in os.listdir():
if book in os.listdir("polyglot"):
with chess.polyglot.open_reader("polyglot/"+book) as reader:
for entry in reader.find_all(board):
opening_moves.append(entry.move)
possible_move = ""
if len(list(board.legal_moves)) == 1:
move = str(list(board.legal_moves)[0])
elif 1==1: # end of game already known
possible_move = use_syzygy(board)
if len(opening_moves)>0:
move = str(choice(opening_moves))
elif possible_move!="":
move = str(possible_move)
return move
def play_random(board, book="baron30.bin"):
"""playing random moves"""
n = play_auto(board, book)
if n is None:
n = str(choice(list(board.legal_moves)))
board.push_san(str(n))
def play_black3(board, book="baron30.bin", evaluation=evaluate):
t = time.time()
opening_moves = []
move = play_auto(board, book)
if move is None:
l = list(map(str, board.legal_moves))
n = {}
for i in l:
coord2=copy.copy(board)
coord2.push_san(i)
N = {}
l2=list(map(str,coord2.legal_moves))
if l2 == []:
e2=evaluation(coord2)
n[e2] = i
if evaluate(coord2) == -1000:move = i;break
else:
for j in l2:
coord3=copy.copy(coord2)
coord3.push_san(j)
M={}
l3=list(map(str,coord3.legal_moves))
if l3 == []:
N[evaluation(coord3)] = j
else:
for k in l3:
coord4=copy.copy(coord3)
coord4.push_san(k)
e4=evaluation(coord4)
M[e4] = k
if evaluate(coord4) == -1000:break
N[min(M)]=j
n[max(N)]=i
move = n[min(n)]
board.push_san(str(move))
if win:
Beep(400, 100)
print(str(board.move_stack[-1]))
print("time:",time.time() - t,"sec")
print("evaluation:",evaluation(board))
def play_white3(board, book="baron30.bin", evaluation=evaluate):
t = time.time()
move = play_auto(board, book)
if move is None:
l = list(map(str,board.legal_moves))
n={}
for i in l:
coord2=copy.copy(board)
coord2.push_san(i)
N = {}
l2 = list(map(str,coord2.legal_moves))
if l2 == []:
e2=evaluation(coord2)
n[e2] = i
if evaluate(coord2) == 1000:move = i;break
else:
for j in l2:
coord3=copy.copy(coord2)
coord3.push_san(j)
M = {}
l3 = list(map(str,coord3.legal_moves))
if l3 == []:
N[evaluation(coord3)] = j
else:
for k in l3:
coord4=copy.copy(coord3)
coord4.push_san(k)
e4 = evaluation(coord4)
M[e4] = k
if evaluate(coord4) == 1000:
break
N[max(M)]=j
n[min(N)]=i
move = n[max(n)]
board.push_san(move)
if win:
Beep(400, 100)
print(str(board.move_stack[-1]))
print("time",time.time() - t,"sec")
print("evaluation:",evaluation(board))
def play_human(move):
try:
coord.push_san(move)
print(str(coord.move_stack[-1]))
print("evaluation:", evaluate(coord))
print(move)
except ValueError:
pass
def deep_eval(board, depth=3, number=100):
score = 0
for i in range(number):
child = copy.copy(board)
for j in range(depth):
try:
play_random(child)
except IndexError:
break
score += evaluate(child)
return score
def play_white2(board, book="baron30.bin", evaluation=evaluate):
t = time.time()
move = play_auto(board, book)
if move is None:
l=list(map(str,board.legal_moves))
n={}
for i in l:
coord2=copy.copy(board)
coord2.push_san(i)
N={}
l2=list(map(str,coord2.legal_moves))
if l2 == []:
e2=evaluation(coord2)
n[e2] = i
if evaluate(coord2) == 1000:
move = i
break
else:
for j in l2:
coord3=copy.copy(coord2)
coord3.push_san(j)
N[evaluation(coord3)] = j
n[min(N)]=i
move = n[max(n)]
board.push_san(move)
if win:
Beep(400, 100)
print(str(board.move_stack[-1]))
print("time",time.time() - t,"sec")
print("evaluation:",evaluation(board))
def play_black2(board, book="baron30.bin", evaluation=evaluate):
t = time.time()
move = play_auto(board, book)
if move is None:
l = list(map(str, board.legal_moves))
n = {}
for i in l:
coord2=copy.copy(board)
coord2.push_san(i)
N = {}
l2=list(map(str,coord2.legal_moves))
if l2 == []:
e2=evaluation(coord2)
n[e2] = i
if evaluate(coord2) == -1000:
move = i
break
else:
for j in l2:
coord3=copy.copy(coord2)
coord3.push_san(j)
N[evaluation(coord3)] = j
n[max(N)]=i
move = n[min(n)]
board.push_san(move)
if win:
Beep(400, 100)
print(str(board.move_stack[-1]))
print("time:",time.time() - t,"sec")
print("evaluation:",evaluation(board))
def play_white1(board, book="baron30.bin", evaluation=evaluate):
t = time.time()
move = play_auto(board, book)
if move is None:
l=list(map(str,board.legal_moves))
n={}
for i in l:
coord2 = copy.copy(board)
coord2.push_san(i)
n[evaluation(coord2)] = i
move = n[max(n)]
board.push_san(move)
if win:
Beep(400, 100)
print(str(board.move_stack[-1]))
print("time",time.time() - t,"sec")
print("evaluation:",evaluation(board))
def play_black1(board, book="Nothing", evaluation=evaluate):
t = time.time()
move = play_auto(board, book)
if move is None:
l=list(map(str,board.legal_moves))
n={}
for i in l:
coord2 = copy.copy(board)
coord2.push_san(i)
n[evaluation(coord2)] = i
move = n[min(n)]
board.push_san(move)
if win:
Beep(400, 100)
print(str(board.move_stack[-1]))
print("time",time.time() - t,"sec")
print("evaluation:",evaluation(board))
def play_white(board, lvl, book):
if lvl == 0:
play_random(board, book)
elif lvl == 1:
play_white1(board, book)
elif lvl == 2:
play_white2(board, book)
else:
play_white3(board, book)
def play_black(board, lvl, book):
if lvl == 0:
play_random(board, book)
elif lvl == 1:
play_black1(board, book)
elif lvl == 2:
play_black2(board, book)
else:
play_black3(board, book)
#coord = chess.Board()
if __name__ == "__main__":
while True not in [coord.is_checkmate(), coord.is_stalemate()]:
draw()
#play_white(coord)
play_human(input("move:"))
draw()
if True not in [coord.is_checkmate(), coord.is_stalemate()]:
#play_black(coord)
#black()
play_human(input("move:"))
elif coord.is_stalemate():
print("DRAW")
else:
print("Checkmate")
if coord.is_stalemate():
print("DRAW")
else:
print("Checkmate")