-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogic_board.ps
More file actions
executable file
·431 lines (307 loc) · 6.51 KB
/
logic_board.ps
File metadata and controls
executable file
·431 lines (307 loc) · 6.51 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
% Nicolas Seriot
% 2024-03-23
% https://github.com/nst/PSChess
% https://seriot.ch/projects/pschess.html
% debugging
/CALLSTACK 20 array def
/CS_INDEX 0 def % callstack index
/CS_PRINT {
5 dict begin
(\nCALLSTACK:) =
0 1 CS_INDEX {
/idx exch def
/line CALLSTACK idx get def
idx ==only ( ) =only line =
} for
end
} def
/CS_PUT {
1 dict begin
/s exch def
CALLSTACK CS_INDEX s put
end
/CS_INDEX CS_INDEX 1 add def
} def
/CS_POP {
CALLSTACK CS_INDEX null put
/CS_INDEX CS_INDEX 1 sub def
} def
%/su_ { errordict/stackunderflow } bind def
%errordict/stackunderflow { CS_PRINT su_ } put
%
%/tc_ { errordict/typecheck } bind def
%errordict/typecheck { CS_PRINT signalerror } put
%errordict/undefined { CS_POP CS_PRINT signalerror } put
%errordict/invalidrestore { CS_POP CS_PRINT signalerror } put
%errordict/rangecheck { CS_POP CS_PRINT signalerror } put
%<<
%/break{ /hook /pause load store }
%/cont{ /hook {} store }
%/doprompt{
% (\nbreak>)print
% flush(%lineedit)(r)file
% cvx {exec}stopped pop }
%/pause{ doprompt }
%/hook{}
%>> begin
% ------------------------------------
% CreateBoard() -> b
/CreateBoard_b_ {
(\
rnbqkbnr\
pppppppp\
........\
........\
........\
........\
PPPPPPPP\
RNBQKBNR\
) _s_Dup_s_
} def
% end game
%/CreateBoard_b_ {
%(\
%r.b.k..r\
%pppp.ppp\
%........\
%..b.....\
%Pn.p.q..\
%........\
%...KP..n\
%RNBQ.BN.\
%) _s_Dup_s_
%} def
% PrintDict(d)
/_d_PrintDict {
/d exch def
d {
/v exch def
/k exch def
( k:) =only k =only ( v: ) =only v ==
} forall
} def
% printBoard(board)
/_board_PrintBoard {
(/_board_PrintBoard) CS_PUT
2 dict begin
/b exch def
()=
b 0 8 getinterval =
b 8 8 getinterval =
b 16 8 getinterval =
b 24 8 getinterval =
b 32 8 getinterval =
b 40 8 getinterval =
b 48 8 getinterval =
b 56 8 getinterval =
()=
end
CS_POP
} def
% dup(s) -> s
/_s_Dup_s_ {
(/_s_Dup_s_) CS_PUT
dup length string cvs
CS_POP
} def
% isValidCoordinate(c) -> b
/_c_IsValidCoordinate_b_ {
(/_c_IsValidCoordinate_b_) CS_PUT
/cr exch def
/ret false def
cr length 2 eq {
/i cr _coord_Index_i_ def
i _index_IsInBoardRange_b_ {
/ret true def
} if
} if
ret
CS_POP
} def
% col(index) -> a-h
/_index_Col_a-h_ {
(/_index_Col_a-h_) CS_PUT
/c ( ) _s_Dup_s_ def
1 dict begin
/i exch def
c 0 i 8 mod 97 add put
end
c
CS_POP
} def
% row(index) -> 1-8
/_index_Row_1-8_ {
(/_index_Row_1-8_-h_) CS_PUT
1 dict begin
/i exch def
8 i 8 idiv sub
end
CS_POP
} def
% index(coord) -> i
/_coord_Index_i_ {
(/_coord_Index_i_) CS_PUT
3 dict begin
/cs exch def
/col { cs 0 get 97 sub } bind def
/row { 7 cs 1 get 49 sub sub } bind def
col 0 lt { (invalid col) == } if
col 7 gt { (invalid col) == } if
row 0 lt { (invalid row) == } if
row 7 gt { (invalid row) == } if
row 8 mul col add
end
CS_POP
} def
% coord(index) -> c
/_index_Coord_c_ {
(/_index_Coord_c_) CS_PUT
2 dict begin
/i_ exch def
%i_ type ==only i ==
i_ _index_IsInBoardRange_b_ false eq {
(-- BAD INDEX:) ==only i_ ==
quit
} if
/col i_ 8 mod 97 add def
/row 8 i_ 8 idiv sub 48 add def
%(col: ) ==only col ==
%(row: ) ==only row ==
/c__ 2 string def
c__ 0 col put
c__ 1 row put
c__ % dup ==
end
CS_POP
} def
% piece(board, coord) -> p
/_board_index_Piece_p_ {
(/_board_index_Piece_p_) CS_PUT
% board
% index
1 getinterval % return value
CS_POP
} def
% piece(board, coord) -> p
/_board_coord_Piece_p_ {
(/_board_coord_Piece_p_ BEGIN) CS_PUT
3 dict begin
/c exch def
/board exch def
board c _coord_Index_i_ _board_index_Piece_p_ % return value
end
CS_POP
} def
% isInBoardRange(index) -> b
/_index_IsInBoardRange_b_ {
(/_index_IsInBoardRange_b_) CS_PUT
dup 0 ge
exch
63 le
and
CS_POP
} def
% tells if two arrays have Equal contents
/_a1_a2_HaveEqualContents_b_ {
(/_a1_a2_HaveEqualContents_b_) CS_PUT
5 dict begin
/a2 exch def
/a1 exch def
/r true def
a1 length a2 length eq {
0 1 a1 length 1 sub {
/i exch def
a1 i get a2 i get ne {
/r false def
exit
} if
} for
} {
/r false def
} ifelse
r
end
CS_POP
} def
% dupLowerCase(p) -> p
/_p_DupLowerCase_p_ {
(/_p_DupLowerCase_p_) CS_PUT
3 dict begin
/p exch def
{
(.rnbkqp) p search {
pop pop pop
p
exit
} {
pop
} ifelse
(RNBKQPX) p search {
pop pop pop
/low ( ) def
low 0 p 0 get 32 add put
low
%(p: ) ==only p ==only ( i: ) ==only i ==only ( low: ) ==only low ==
exit
} {
pop
} ifelse
(ERROR: piece unknown: ) =only p ==
null exit
} loop
end
CS_POP
} def
% pieceIndex(board, piece) -> i
/_board_piece_PieceIndex_i_ {
(/_board_piece_PieceIndex_i_) CS_PUT
5 dict begin
/p exch def
/b exch def
/pIndex null def
b p search {
/pIndex exch length def pop pop
} {
pop
} ifelse
pIndex
end
CS_POP
} def
% isWhite(piece) -> b
/_piece_IsWhite_b_ {
(/_piece_IsWhite_b_) CS_PUT
% p on stack
(RNBKQP) exch search
{ pop pop pop true } { pop false } ifelse
CS_POP
} def
% isBlack(piece) -> b
/_piece_IsBlack_b_ {
(/_piece_IsBlack_b_) CS_PUT
% p on stack
(rnbkqp) exch search
{ pop pop pop true } { pop false } ifelse
CS_POP
} def
% isEmpty(piece) -> b
/_piece_IsEmpty_b_ {
(/_piece_IsEmpty_b_) CS_PUT
% p on stack
(.) eq
CS_POP
} def
% dupReversed(a) -> a
/_a_DupReversed_a_ {
(/_a_DupReversed_a_) CS_PUT
2 dict begin
/a1 exch def
/a2 a1 length array def
0 1 a1 length 1 sub {
/i exch def
a2 a1 length i sub 1 sub a1 i get put
} for
a2
end
CS_POP
} def