-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.shl
More file actions
284 lines (242 loc) · 7.88 KB
/
board.shl
File metadata and controls
284 lines (242 loc) · 7.88 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
use "constants.shl"
structure Board
has dummy = 0
has squares = []
has turn = WHITE
has castling = { "WK": yes, "WQ": yes, "BK": yes, "BQ": yes }
has ep_square = 0 - 1
has halfmove = 0
has fullmove = 1
has history = []
to get_king_square color
for i in range 0 64
p = squares[i]
if p != EMPTY and get_piece_type(p) == KING and get_piece_color(p) == color
give i
give 0 - 1
to reset
squares = []
repeat 64 times
add EMPTY to squares
squares[0] = ROOK + BLACK
squares[1] = KNIGHT + BLACK
squares[2] = BISHOP + BLACK
squares[3] = QUEEN + BLACK
squares[4] = KING + BLACK
squares[5] = BISHOP + BLACK
squares[6] = KNIGHT + BLACK
squares[7] = ROOK + BLACK
for i in range 8 16
squares[i] = PAWN + BLACK
for i in range 48 56
squares[i] = PAWN + WHITE
squares[56] = ROOK + WHITE
squares[57] = KNIGHT + WHITE
squares[58] = BISHOP + WHITE
squares[59] = QUEEN + WHITE
squares[60] = KING + WHITE
squares[61] = BISHOP + WHITE
squares[62] = KNIGHT + WHITE
squares[63] = ROOK + WHITE
turn = WHITE
ep_square = 0 - 1
halfmove = 0
fullmove = 1
history = []
to set_fen fen_str
squares = []
repeat 64 times
add EMPTY to squares
history = []
parts = split(fen_str)
if empty(parts)
give no
placement = parts[0]
row = 0
col = 0
n = len(placement)
for i in range 0 n
char_val = placement[i]
if char_val == "/"
row = row + 1
col = 0
elif char_val == "1" or char_val == "2" or char_val == "3" or char_val == "4" or char_val == "5" or char_val == "6" or char_val == "7" or char_val == "8"
col = col + int(char_val)
else
piece = EMPTY
if char_val == "p"
piece = PAWN + BLACK
elif char_val == "n"
piece = KNIGHT + BLACK
elif char_val == "b"
piece = BISHOP + BLACK
elif char_val == "r"
piece = ROOK + BLACK
elif char_val == "q"
piece = QUEEN + BLACK
elif char_val == "k"
piece = KING + BLACK
elif char_val == "P"
piece = PAWN + WHITE
elif char_val == "N"
piece = KNIGHT + WHITE
elif char_val == "B"
piece = BISHOP + WHITE
elif char_val == "R"
piece = ROOK + WHITE
elif char_val == "Q"
piece = QUEEN + WHITE
elif char_val == "K"
piece = KING + WHITE
squares[row * 8 + col] = piece
col = col + 1
turn = WHITE
if len(parts) > 1
if parts[1] == "b"
turn = BLACK
castling = { "WK": no, "WQ": no, "BK": no, "BQ": no }
if len(parts) > 2
c_rights = parts[2]
n_c = len(c_rights)
for i in range 0 n_c
c_char = c_rights[i]
if c_char == "K"
castling["WK"] = yes
elif c_char == "Q"
castling["WQ"] = yes
elif c_char == "k"
castling["BK"] = yes
elif c_char == "q"
castling["BQ"] = yes
ep_square = 0 - 1
if len(parts) > 3
ep_str = parts[3]
if ep_str != "-" and len(ep_str) == 2
f_idx = ord(ep_str[0]) - ord("a")
r_idx = 8 - int(ep_str[1])
ep_square = r_idx * 8 + f_idx
halfmove = 0
if len(parts) > 4
halfmove = int(parts[4])
fullmove = 1
if len(parts) > 5
fullmove = int(parts[5])
to get_piece square
give squares[square]
to set_piece square piece
squares[square] = piece
to is_empty square
give squares[square] == EMPTY
to get_color piece
if piece == EMPTY
give 0
if piece < BLACK
give WHITE
else
give BLACK
to get_type piece
if piece == EMPTY
give EMPTY
if piece < BLACK
give piece - WHITE
else
give piece - BLACK
to set_turn t
turn = t
to set_ep_square sq
ep_square = sq
to make_move move
state = {
"turn": turn,
"castling": { "WK": castling["WK"], "WQ": castling["WQ"], "BK": castling["BK"], "BQ": castling["BQ"] },
"ep_square": ep_square,
"halfmove": halfmove,
"move": move
}
add state to history
squares[move.to_sq] = move.piece
if get_piece_type(move.piece) == PAWN
to_row = int(move.to_sq / 8)
if to_row == 0
squares[move.to_sq] = QUEEN + WHITE
elif to_row == 7
squares[move.to_sq] = QUEEN + BLACK
squares[move.from_sq] = EMPTY
if move.flags == 2
if turn == WHITE
squares[move.to_sq + 8] = EMPTY
else
squares[move.to_sq - 8] = EMPTY
if move.flags == 4
if move.to_sq == 62
squares[61] = ROOK + WHITE
squares[63] = EMPTY
elif move.to_sq == 58
squares[59] = ROOK + WHITE
squares[56] = EMPTY
elif move.to_sq == 6
squares[5] = ROOK + BLACK
squares[7] = EMPTY
elif move.to_sq == 2
squares[3] = ROOK + BLACK
squares[0] = EMPTY
if move.from_sq == 60
castling["WK"] = no
castling["WQ"] = no
elif move.from_sq == 4
castling["BK"] = no
castling["BQ"] = no
if move.from_sq == 56 or move.to_sq == 56
castling["WQ"] = no
if move.from_sq == 63 or move.to_sq == 63
castling["WK"] = no
if move.from_sq == 0 or move.to_sq == 0
castling["BQ"] = no
if move.from_sq == 7 or move.to_sq == 7
castling["BK"] = no
ep_square = 0 - 1
if get_piece_type(move.piece) == PAWN
if abs(move.to_sq - move.from_sq) == 16
ep_square = int((move.from_sq + move.to_sq) / 2)
if turn == WHITE
turn = BLACK
else
turn = WHITE
fullmove = fullmove + 1
to undo_move
if empty(history)
give no
state = history.pop()
move = state["move"]
squares[move.from_sq] = move.piece
squares[move.to_sq] = move.captured
if move.flags == 2
squares[move.to_sq] = EMPTY
if state["turn"] == WHITE
squares[move.to_sq + 8] = PAWN + BLACK
else
squares[move.to_sq - 8] = PAWN + WHITE
if move.flags == 4
if move.to_sq == 62
squares[63] = ROOK + WHITE
squares[61] = EMPTY
elif move.to_sq == 58
squares[56] = ROOK + WHITE
squares[59] = EMPTY
elif move.to_sq == 6
squares[7] = ROOK + BLACK
squares[5] = EMPTY
elif move.to_sq == 2
squares[0] = ROOK + BLACK
squares[3] = EMPTY
turn = state["turn"]
castling = state["castling"]
ep_square = state["ep_square"]
halfmove = state["halfmove"]
if turn == BLACK
fullmove = fullmove - 1
give yes
to create_board
b = Board(0)
b.reset()
give b