-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoves.shl
More file actions
393 lines (352 loc) · 11 KB
/
moves.shl
File metadata and controls
393 lines (352 loc) · 11 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
use "constants.shl"
use "math"
to get_row sq
give int(sq / 8)
to get_col sq
give sq % 8
to get_sq r c
give r * 8 + c
structure Move
has from_sq
has to_sq
has piece
has captured = 0
has flags = 0
to generate_pseudo_legal_moves board
moves = []
sqs = board.squares
for i in range 0 64
piece = sqs[i]
if piece == EMPTY
skip
color = 0
if piece < BLACK
color = WHITE
else
color = BLACK
if color != board.turn
skip
piece_type = 0
if piece < BLACK
piece_type = piece - WHITE
else
piece_type = piece - BLACK
if piece_type == PAWN
generate_pawn_moves(board, i, moves)
elif piece_type == KNIGHT
generate_knight_moves(board, i, moves)
elif piece_type == BISHOP
bishop_dirs = [7, 0 - 7, 9, 0 - 9]
generate_sliding_moves(board, i, bishop_dirs, moves)
elif piece_type == ROOK
rook_dirs = [1, 0 - 1, 8, 0 - 8]
generate_sliding_moves(board, i, rook_dirs, moves)
elif piece_type == QUEEN
queen_dirs = [1, 0 - 1, 8, 0 - 8, 7, 0 - 7, 9, 0 - 9]
generate_sliding_moves(board, i, queen_dirs, moves)
elif piece_type == KING
generate_king_moves(board, i, moves)
give moves
to generate_pawn_moves board sq moves
r = get_row(sq)
if r == 0 or r == 7
give no
c = get_col(sq)
sqs = board.squares
p_orig = sqs[sq]
color = get_piece_color(p_orig)
if color == WHITE
target = get_sq(r - 1, c)
if r > 0 and sqs[target] == EMPTY
add Move(sq, target, p_orig) to moves
target2 = get_sq(r - 2, c)
if r == 6 and sqs[target2] == EMPTY
m = Move(sq, target2, p_orig, 0, 1)
add m to moves
for dc in [0 - 1, 1]
nc = c + dc
if nc >= 0 and nc < 8
target = get_sq(r - 1, nc)
tp = sqs[target]
if tp != EMPTY and get_piece_color(tp) == BLACK
m = Move(sq, target, p_orig, tp, 0)
add m to moves
elif target == board.ep_square
m = Move(sq, target, p_orig, PAWN + BLACK, 2)
add m to moves
else
target = get_sq(r + 1, c)
if r < 7 and sqs[target] == EMPTY
add Move(sq, target, p_orig) to moves
target2 = get_sq(r + 2, c)
if r == 1 and sqs[target2] == EMPTY
m = Move(sq, target2, p_orig, 0, 1)
add m to moves
for dc in [0 - 1, 1]
nc = c + dc
if nc >= 0 and nc < 8
target = get_sq(r + 1, nc)
tp = sqs[target]
if tp != EMPTY and get_piece_color(tp) == WHITE
m = Move(sq, target, p_orig, tp, 0)
add m to moves
elif target == board.ep_square
m = Move(sq, target, p_orig, PAWN + WHITE, 2)
add m to moves
to generate_knight_moves board sq moves
r = get_row(sq)
c = get_col(sq)
sqs = board.squares
piece = sqs[sq]
color = get_piece_color(piece)
offsets = [[0 - 2, 0 - 1], [0 - 2, 1], [0 - 1, 0 - 2], [0 - 1, 2], [1, 0 - 2], [1, 2], [2, 0 - 1], [2, 1]]
for off in offsets
nr = r + off[0]
nc = c + off[1]
if nr >= 0 and nr < 8 and nc >= 0 and nc < 8
target = get_sq(nr, nc)
tp = sqs[target]
if tp == EMPTY
add Move(sq, target, piece) to moves
elif get_piece_color(tp) != color
m = Move(sq, target, piece, tp, 0)
add m to moves
to generate_sliding_moves board sq directions moves
r = get_row(sq)
c = get_col(sq)
sqs = board.squares
piece = sqs[sq]
color = get_piece_color(piece)
for d in directions
dr = 0
dc = 0
if d == 1
dc = 1
elif d == 0 - 1
dc = 0 - 1
elif d == 8
dr = 1
elif d == 0 - 8
dr = 0 - 1
elif d == 7
dr = 1
dc = 0 - 1
elif d == 0 - 7
dr = 0 - 1
dc = 1
elif d == 9
dr = 1
dc = 1
elif d == 0 - 9
dr = 0 - 1
dc = 0 - 1
nr = r + dr
nc = c + dc
while nr >= 0 and nr < 8 and nc >= 0 and nc < 8
target = get_sq(nr, nc)
tp = sqs[target]
if tp == EMPTY
add Move(sq, target, piece) to moves
else
if get_piece_color(tp) != color
m = Move(sq, target, piece, tp, 0)
add m to moves
stop
nr = nr + dr
nc = nc + dc
to generate_king_moves board sq moves
r = get_row(sq)
c = get_col(sq)
sqs = board.squares
piece = sqs[sq]
color = get_piece_color(piece)
ks = 0 - 1
ke = 2
for dr in range ks ke
for dc in range ks ke
if dr == 0 and dc == 0
skip
nr = r + dr
nc = c + dc
if nr >= 0 and nr < 8 and nc >= 0 and nc < 8
target = get_sq(nr, nc)
tp = sqs[target]
if tp == EMPTY
add Move(sq, target, piece) to moves
elif get_piece_color(tp) != color
m = Move(sq, target, piece, tp, 0)
add m to moves
castl = board.castling
if color == WHITE
if castl["WK"]
if board.is_empty(61) and board.is_empty(62)
if not is_square_attacked(board, 60, BLACK) and not is_square_attacked(board, 61, BLACK) and not is_square_attacked(board, 62, BLACK)
m = Move(60, 62, piece, EMPTY, 4)
add m to moves
if castl["WQ"]
if board.is_empty(59) and board.is_empty(58) and board.is_empty(57)
if not is_square_attacked(board, 60, BLACK) and not is_square_attacked(board, 59, BLACK) and not is_square_attacked(board, 58, BLACK)
m = Move(60, 58, piece, EMPTY, 4)
add m to moves
else
if castl["BK"]
if board.is_empty(5) and board.is_empty(6)
if not is_square_attacked(board, 4, WHITE) and not is_square_attacked(board, 5, WHITE) and not is_square_attacked(board, 6, WHITE)
m = Move(4, 6, piece, EMPTY, 4)
add m to moves
if castl["BQ"]
if board.is_empty(3) and board.is_empty(2) and board.is_empty(1)
if not is_square_attacked(board, 4, WHITE) and not is_square_attacked(board, 3, WHITE) and not is_square_attacked(board, 2, WHITE)
m = Move(4, 2, piece, EMPTY, 4)
add m to moves
to is_square_attacked board sq color
r = get_row(sq)
c = get_col(sq)
sqs = board.squares
if color == WHITE
dr = 1
else
dr = 0 - 1
nr = r + dr
nc = c - 1
if nr >= 0 and nr < 8 and nc >= 0 and nc < 8
idx = get_sq(nr, nc)
p = sqs[idx]
if get_piece_type(p) == PAWN and get_piece_color(p) == color
give yes
nc = c + 1
if nr >= 0 and nr < 8 and nc >= 0 and nc < 8
idx = get_sq(nr, nc)
p = sqs[idx]
if get_piece_type(p) == PAWN and get_piece_color(p) == color
give yes
dkr = 0 - 2
while dkr <= 2
if dkr == 0
dkr = dkr + 1
skip
dkc = 0 - 2
while dkc <= 2
if dkc == 0
dkc = dkc + 1
skip
if abs(dkr) + abs(dkc) == 3
nr = r + dkr
nc = c + dkc
if nr >= 0 and nr < 8 and nc >= 0 and nc < 8
idx = get_sq(nr, nc)
p = sqs[idx]
if get_piece_type(p) == KNIGHT and get_piece_color(p) == color
give yes
dkc = dkc + 1
dkc = 0 - 2
dkr = dkr + 1
for d in [1, 0 - 1, 8, 0 - 8]
dr = 0
dc = 0
if d == 1
dc = 1
elif d == 0 - 1
dc = 0 - 1
elif d == 8
dr = 1
elif d == 0 - 8
dr = 0 - 1
nr = r + dr
nc = c + dc
while nr >= 0 and nr < 8 and nc >= 0 and nc < 8
idx = get_sq(nr, nc)
p = sqs[idx]
if p != EMPTY
if get_piece_color(p) == color
t = get_piece_type(p)
if t == ROOK or t == QUEEN
give yes
stop
nr = nr + dr
nc = nc + dc
for d in [7, 0 - 7, 9, 0 - 9]
dr = 0
dc = 0
if d == 7
dr = 1
dc = 0 - 1
elif d == 0 - 7
dr = 0 - 1
dc = 1
elif d == 9
dr = 1
dc = 1
elif d == 0 - 9
dr = 0 - 1
dc = 0 - 1
nr = r + dr
nc = c + dc
while nr >= 0 and nr < 8 and nc >= 0 and nc < 8
idx = get_sq(nr, nc)
p = sqs[idx]
if p != EMPTY
if get_piece_color(p) == color
t = get_piece_type(p)
if t == BISHOP or t == QUEEN
give yes
stop
nr = nr + dr
nc = nc + dc
r_start = 0 - 1
r_end = 2
for dr in range r_start r_end
c_start = 0 - 1
c_end = 2
for dc in range c_start c_end
if dr == 0 and dc == 0
skip
nr = r + dr
nc = c + dc
if nr >= 0 and nr < 8 and nc >= 0 and nc < 8
idx = get_sq(nr, nc)
p = sqs[idx]
if get_piece_type(p) == KING and get_piece_color(p) == color
give yes
give no
to is_path_clear board from_sq to_sq
r1 = get_row(from_sq)
c1 = get_col(from_sq)
r2 = get_row(to_sq)
c2 = get_col(to_sq)
dr = 0
if r2 > r1
dr = 1
elif r2 < r1
dr = 0 - 1
dc = 0
if c2 > c1
dc = 1
elif c2 < c1
dc = 0 - 1
curr_r = r1 + dr
curr_c = c1 + dc
while yes
if curr_r == r2 and curr_c == c2
stop
if curr_r < 0 or curr_r >= 8 or curr_c < 0 or curr_c >= 8
stop
sq = get_sq(curr_r, curr_c)
if not board.is_empty(sq)
give no
curr_r = curr_r + dr
curr_c = curr_c + dc
give yes
to is_in_check board color
ksq = board.get_king_square(color)
give is_square_attacked(board, ksq, 24 - color)
to generate_legal_moves board
original_turn = board.turn
pseudo = generate_pseudo_legal_moves(board)
legal = []
for m in pseudo
board.make_move(m)
if not is_square_attacked(board, board.get_king_square(original_turn), board.turn)
add m to legal
board.undo_move()
give legal