-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.shl
More file actions
474 lines (369 loc) · 12.3 KB
/
search.shl
File metadata and controls
474 lines (369 loc) · 12.3 KB
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use "constants.shl"
use "moves.shl"
use "time"
use "book.shl"
PAWN_PST = [
0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
10, 10, 20, 30, 30, 20, 10, 10,
5, 5, 10, 25, 25, 10, 5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, 0 - 5, 0 - 10, 0, 0, 0 - 10, 0 - 5, 5,
5, 10, 10, 0 - 20, 0 - 20, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0
]
KNIGHT_PST = [
0 - 50, 0 - 40, 0 - 30, 0 - 30, 0 - 30, 0 - 30, 0 - 40, 0 - 50,
0 - 40, 0 - 20, 0, 0, 0, 0, 0 - 20, 0 - 40,
0 - 30, 0, 10, 15, 15, 10, 0, 0 - 30,
0 - 30, 5, 15, 20, 20, 15, 5, 0 - 30,
0 - 30, 0, 15, 20, 20, 15, 0, 0 - 30,
0 - 30, 5, 10, 15, 15, 10, 5, 0 - 30,
0 - 40, 0 - 20, 0, 5, 5, 0, 0 - 20, 0 - 40,
0 - 50, 0 - 40, 0 - 30, 0 - 30, 0 - 30, 0 - 30, 0 - 40, 0 - 50
]
BISHOP_PST = [
0 - 20, 0 - 10, 0 - 10, 0 - 10, 0 - 10, 0 - 10, 0 - 10, 0 - 20,
0 - 10, 0, 0, 0, 0, 0, 0, 0 - 10,
0 - 10, 0, 5, 10, 10, 5, 0, 0 - 10,
0 - 10, 5, 5, 10, 10, 5, 5, 0 - 10,
0 - 10, 0, 10, 10, 10, 10, 0, 0 - 10,
0 - 10, 10, 10, 10, 10, 10, 10, 0 - 10,
0 - 10, 5, 0, 0, 0, 0, 5, 0 - 10,
0 - 20, 0 - 10, 0 - 10, 0 - 10, 0 - 10, 0 - 10, 0 - 10, 0 - 20
]
ROOK_PST = [
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, 10, 10, 10, 10, 5,
0 - 5, 0, 0, 0, 0, 0, 0, 0 - 5,
0 - 5, 0, 0, 0, 0, 0, 0, 0 - 5,
0 - 5, 0, 0, 0, 0, 0, 0, 0 - 5,
0 - 5, 0, 0, 0, 0, 0, 0, 0 - 5,
0 - 5, 0, 0, 0, 0, 0, 0, 0 - 5,
0, 0, 0, 5, 5, 0, 0, 0
]
KING_PST = [
0 - 30, 0 - 40, 0 - 40, 0 - 50, 0 - 50, 0 - 40, 0 - 40, 0 - 30,
0 - 30, 0 - 40, 0 - 40, 0 - 50, 0 - 50, 0 - 40, 0 - 40, 0 - 30,
0 - 30, 0 - 40, 0 - 40, 0 - 50, 0 - 50, 0 - 40, 0 - 40, 0 - 30,
0 - 30, 0 - 40, 0 - 40, 0 - 50, 0 - 50, 0 - 40, 0 - 40, 0 - 30,
0 - 20, 0 - 30, 0 - 30, 0 - 40, 0 - 40, 0 - 30, 0 - 30, 0 - 20,
0 - 10, 0 - 20, 0 - 20, 0 - 20, 0 - 20, 0 - 20, 0 - 20, 0 - 10,
20, 20, 0, 0, 0, 0, 20, 20,
20, 30, 10, 0, 0, 10, 30, 20
]
KING_ENDGAME_PST = [
0 - 50, 0 - 40, 0 - 30, 0 - 20, 0 - 20, 0 - 30, 0 - 40, 0 - 50,
0 - 30, 0 - 20, 0 - 10, 0, 0, 0 - 10, 0 - 20, 0 - 30,
0 - 30, 0 - 10, 20, 30, 30, 20, 0 - 10, 0 - 30,
0 - 30, 0 - 10, 30, 40, 40, 30, 0 - 10, 0 - 30,
0 - 30, 0 - 10, 30, 40, 40, 30, 0 - 10, 0 - 30,
0 - 30, 0 - 10, 20, 30, 30, 20, 0 - 10, 0 - 30,
0 - 30, 0 - 30, 0, 0, 0, 0, 0 - 30, 0 - 30,
0 - 50, 0 - 30, 0 - 30, 0 - 30, 0 - 30, 0 - 30, 0 - 30, 0 - 50
]
tt_table = {}
to tt_lookup key depth alpha beta
if contains(tt_table, key)
entry = tt_table[key]
if entry["depth"] >= depth
val = entry["val"]
flag = entry["flag"]
if flag == 0
give val
if flag == 1
if val >= beta
give val
if flag == 2
if val <= alpha
give val
give null
to tt_store key depth val flag move
entry = {
"depth": depth,
"val": val,
"flag": flag,
"move": move
}
tt_table[key] = entry
killer_table = {}
to add_killer ply move
if not contains(killer_table, ply)
killer_table[ply] = []
killers = killer_table[ply]
already_exists = no
for km in killers
if km.from_sq == move.from_sq and km.to_sq == move.to_sq
already_exists = yes
if not already_exists
add move to killers
if len(killers) > 2
pop(killers, 0)
to is_endgame board
white_pieces = 0
black_pieces = 0
white_queens = 0
black_queens = 0
sqs = board.squares
for i in range 0 64
p = sqs[i]
if p != 0
ptype = get_piece_type(p)
color = get_piece_color(p)
if ptype != PAWN and ptype != KING
if color == WHITE
white_pieces = white_pieces + 1
if ptype == QUEEN
white_queens = white_queens + 1
else
black_pieces = black_pieces + 1
if ptype == QUEEN
black_queens = black_queens + 1
if white_queens == 0 and black_queens == 0
give yes
if white_pieces <= 2 and black_pieces <= 2
give yes
give no
to evaluate board
score = 0
sqs = board.squares
turn = board.turn
is_eg = is_endgame(board)
white_pieces = 0
black_pieces = 0
for i in range 0 64
p = sqs[i]
if p == 0
skip
color = get_piece_color(p)
ptype = get_piece_type(p)
if ptype != PAWN and ptype != KING
if color == WHITE
white_pieces = white_pieces + 1
else
black_pieces = black_pieces + 1
val = 0
if ptype == 1
val = 100
if is_eg
r = int(i / 8)
if color == WHITE
rank_num = 7 - r
if rank_num >= 4
val = val + rank_num * 10
else
rank_num = r
if rank_num >= 4
val = val + rank_num * 10
elif ptype == 2
val = 300
elif ptype == 3
val = 320
elif ptype == 4
val = 500
elif ptype == 5
val = 900
elif ptype == 6
val = 20000
idx = i
if color == 16
r = int(i / 8)
c = i % 8
idx = (7 - r) * 8 + c
pst_val = 0
if ptype == 1
pst_val = PAWN_PST[idx]
elif ptype == 2
pst_val = KNIGHT_PST[idx]
elif ptype == 3
pst_val = BISHOP_PST[idx]
elif ptype == 4
pst_val = ROOK_PST[idx]
elif ptype == 6
if is_eg
pst_val = KING_ENDGAME_PST[idx]
else
pst_val = KING_PST[idx]
val = val + pst_val
if color == turn
score = score + val
else
score = score - val
if is_eg
if white_pieces == 0
wk_sq = board.get_king_square(WHITE)
bk_sq = board.get_king_square(BLACK)
if wk_sq != 0 - 1 and bk_sq != 0 - 1
wk_r = int(wk_sq / 8)
wk_c = wk_sq % 8
dist_to_center = abs(wk_r - 3.5) + abs(wk_c - 3.5)
bk_r = int(bk_sq / 8)
bk_c = bk_sq % 8
dist_between_kings = abs(wk_r - bk_r) + abs(wk_c - bk_c)
mop_up = int(dist_to_center * 10) + int((14 - dist_between_kings) * 5)
if turn == BLACK
score = score + mop_up
else
score = score - mop_up
elif black_pieces == 0
wk_sq = board.get_king_square(WHITE)
bk_sq = board.get_king_square(BLACK)
if wk_sq != 0 - 1 and bk_sq != 0 - 1
bk_r = int(bk_sq / 8)
bk_c = bk_sq % 8
dist_to_center = abs(bk_r - 3.5) + abs(bk_c - 3.5)
wk_r = int(wk_sq / 8)
wk_c = wk_sq % 8
dist_between_kings = abs(wk_r - bk_r) + abs(wk_c - bk_c)
mop_up = int(dist_to_center * 10) + int((14 - dist_between_kings) * 5)
if turn == WHITE
score = score + mop_up
else
score = score - mop_up
give score
to generate_legal_captures board
original_turn = board.turn
pseudo = generate_pseudo_legal_moves(board)
legal_captures = []
for m in pseudo
if m.captured == EMPTY or m.captured == 0
skip
board.make_move(m)
if not is_square_attacked(board, board.get_king_square(original_turn), board.turn)
add m to legal_captures
board.undo_move()
give legal_captures
search_state = { "start_time": 0.0, "time_limit": 100000000.0, "aborted": no, "node_count": 0 }
to check_time
search_state["node_count"] = search_state["node_count"] + 1
if search_state["node_count"] % 1024 == 0
elapsed = time.time() * 1000.0 - search_state["start_time"]
if elapsed > search_state["time_limit"]
search_state["aborted"] = yes
to quiescence board alpha beta
check_time()
if search_state["aborted"] == yes
give 0
stand_pat = evaluate(board)
if stand_pat >= beta
give beta
if stand_pat + 975 < alpha
give alpha
if stand_pat > alpha
alpha = stand_pat
moves = generate_legal_captures(board)
ordered = order_moves(moves, null, 0)
for m in ordered
board.make_move(m)
score = 0 - quiescence(board, 0 - beta, 0 - alpha)
board.undo_move()
if score >= beta
give beta
if score > alpha
alpha = score
give alpha
to make_null_move board
old_turn = board.turn
old_ep = board.ep_square
if board.turn == WHITE
board.set_turn(BLACK)
else
board.set_turn(WHITE)
board.set_ep_square(0 - 1)
give [old_turn, old_ep]
to undo_null_move board state
board.set_turn(state[0])
board.set_ep_square(state[1])
to find_best_move board depth allocated_time = 100000000.0
book_move = get_book_move(board)
if book_move != null
give book_move
moves = generate_legal_moves(board)
if empty(moves)
give null
best_move = null
clear_dict(tt_table)
clear_dict(killer_table)
search_state["start_time"] = time.time() * 1000.0
search_state["time_limit"] = float(allocated_time)
search_state["aborted"] = no
search_state["node_count"] = 0
last_completed_best_move = moves[0]
end_depth = depth + 1
for d in range 1 end_depth
key = str(board.squares) + "," + str(board.turn)
hash_move = null
if contains(tt_table, key)
hash_move = tt_table[key]["move"]
ordered = order_moves(moves, hash_move, 0)
best_val = 0 - 1000000
depth_best_move = null
for m in ordered
board.make_move(m)
val = 0 - negamax(board, d - 1, 0 - 1000000, 0 - best_val, 1)
board.undo_move()
if search_state["aborted"] == yes
stop
if val > best_val
best_val = val
depth_best_move = m
if search_state["aborted"] == yes
stop
last_completed_best_move = depth_best_move
tt_store(key, d, best_val, 0, depth_best_move)
elapsed = time.time() * 1000.0 - search_state["start_time"]
if elapsed * 2.0 > search_state["time_limit"]
stop
give last_completed_best_move
to negamax board depth alpha beta ply
check_time()
if search_state["aborted"] == yes
give 0
key = str(board.squares) + "," + str(board.turn)
tt_val = tt_lookup(key, depth, alpha, beta)
if tt_val != null
give tt_val
if depth == 0
give quiescence(board, alpha, beta)
if depth >= 3 and not is_in_check(board, board.turn)
null_state = make_null_move(board)
score = 0 - negamax(board, depth - 3, 0 - beta, 0 - beta + 1, ply + 1)
undo_null_move(board, null_state)
if score >= beta
give beta
moves = generate_legal_moves(board)
if empty(moves)
if is_in_check(board, board.turn)
give 0 - 100000 + ply
else
give 0
hash_move = null
if contains(tt_table, key)
hash_move = tt_table[key]["move"]
ordered = order_moves(moves, hash_move, ply)
best_val = 0 - 1000000
best_move = null
for m in ordered
board.make_move(m)
val = 0 - negamax(board, depth - 1, 0 - beta, 0 - alpha, ply + 1)
board.undo_move()
if search_state["aborted"] == yes
stop
if val > best_val
best_val = val
best_move = m
if val > alpha
alpha = val
if beta <= alpha
if m.captured == EMPTY or m.captured == 0
add_killer(ply, m)
stop
if search_state["aborted"] == yes
give 0
flag = 0
if best_val <= alpha
flag = 2
elif best_val >= beta
flag = 1
tt_store(key, depth, best_val, flag, best_move)
give best_val