forked from bolknote/SedChess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.sed
1522 lines (1230 loc) · 39.6 KB
/
chess.sed
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#n
# sed chess by Evgeny Stepanischev (http://bolknote.ru) Aug 2013
1s/.*/\
@\
figures()\
board()\
label(loop)\
label(blackkingmove)\
input()\
move-white()\
check-black-king-exists()\
select-figures(K)\
make-fake-kings()\
select-figures(p)\
label(pawnwhite)\
iter-pawn()\
break-if-end(pawnwhite)\
delete-last-board()\
store-only-iter()\
back(pawnwhite)\
select-figures(q)\
label(queenwhite)\
iter-queen()\
break-if-end(queenwhite)\
delete-last-board()\
store-only-iter()\
back(queenwhite)\
select-figures(k)\
label(kingwhitw)\
iter-king()\
break-if-end(kingwhite)\
delete-last-board()\
store-only-iter()\
back(kingwhite)\
select-figures(i)\
label(bishopwhite)\
iter-bishop()\
break-if-end(bishopwhite)\
delete-last-board()\
store-only-iter()\
back(bishopwhite)\
select-figures(n)\
label(knightwhite)\
iter-knight()\
break-if-end(knightwhite)\
delete-last-board()\
store-only-iter()\
back(knightwhite)\
select-figures(r)\
label(rookwhite)\
iter-rook()\
break-if-end(rookwhite)\
delete-last-board()\
store-only-iter()\
back(rookwhite)\
check-mate()\
break-if-end(blackkingmove)\
move-black()\
check-white-king-exists()\
board()\
back(blackkingmove)\
board()\
select-figures(K)\
label(king)\
iter-king()\
break-if-end(king)\
set-array()\
estimate-white-pieces()\
set-array()\
estimate-black-pieces()\
estimate-black-king()\
sum-array()\
sub-array()\
delete-last-board()\
store-iter()\
back(king)\
select-figures(P)\
label(pawn)\
iter-pawn()\
break-if-end(pawn)\
set-array()\
estimate-white-pieces()\
set-array()\
estimate-black-pieces()\
estimate-black-pawn()\
sum-array()\
sub-array()\
delete-last-board()\
store-iter()\
back(pawn)\
select-figures(Q)\
label(queen)\
iter-queen()\
break-if-end(queen)\
set-array()\
estimate-white-pieces()\
set-array()\
estimate-black-pieces()\
estimate-black-queen()\
sum-array()\
sub-array()\
delete-last-board()\
store-iter()\
back(queen)\
select-figures(I)\
label(bishop)\
iter-bishop()\
break-if-end(bishop)\
set-array()\
estimate-white-pieces()\
set-array()\
estimate-black-pieces()\
estimate-black-bishop()\
sum-array()\
sub-array()\
delete-last-board()\
store-iter()\
back(bishop)\
select-figures(N)\
label(knight)\
iter-knight()\
break-if-end(knight)\
set-array()\
estimate-white-pieces()\
set-array()\
estimate-black-pieces()\
estimate-black-knight()\
sum-array()\
sub-array()\
delete-last-board()\
store-iter()\
back(knight)\
select-figures(R)\
label(rook)\
iter-rook()\
break-if-end(rook)\
set-array()\
estimate-white-pieces()\
estimate-black-pieces()\
sub-array()\
delete-last-board()\
store-iter()\
back(rook)\
find-best-move()\
move-black()\
board()\
check-white-king-exists()\
select-figures(k)\
make-fake-kings()\
select-figures(P)\
label(pawnmoves)\
iter-pawn()\
break-if-end(pawnmoves)\
delete-last-board()\
store-only-iter()\
back(pawnmoves)\
select-figures(Q)\
label(queenmoves)\
iter-queen()\
break-if-end(queenmoves)\
delete-last-board()\
store-only-iter()\
back(queenmoves)\
select-figures(K)\
label(kingmoves)\
iter-king()\
break-if-end(kingmoves)\
delete-last-board()\
store-only-iter()\
back(kingmoves)\
select-figures(I)\
label(bishopmoves)\
iter-bishop()\
break-if-end(bishopmoves)\
delete-last-board()\
store-only-iter()\
back(bishopmoves)\
select-figures(N)\
label(knightmoves)\
iter-knight()\
break-if-end(knightmoves)\
delete-last-board()\
store-only-iter()\
back(knightmoves)\
select-figures(R)\
label(rookmoves)\
iter-rook()\
break-if-end(rookmoves)\
delete-last-board()\
store-only-iter()\
back(rookmoves)\
check-mate()\
back(loop)\
/
# Evaluation matrices are programmed from the book
# "Programming chess and other logic games" Evgeny Kornilov
# Reformatting commands
1s/ *//g; 1s/\n\n*/ /g; 1s/^ //
# Process the incoming team
1!{
/^\([a-h][1-8]\) *\([a-h][1-8]\)$/ {
s//\1 \2/
# Add the values obtained in front of a stack of execution
G; s/\n/ /
# Turn on the performance of teams
b @
}
# A player wants to leave
/^q/ q
# Put any nonsense, erase and return stack commands
i\
[12H[J[1A
s/.*//
g
b
}
:@
s/@\([^ ]* \)/\1@/
# Begin array
/@set-array()/ {
s/^/ARRAY /
b @
}
# Mark
/@label(/ {
b @
}
# Move to the label
/@back(/ {
s/label(\([^)]*\))\(.*\)@back(\1)/@label(\1)\2back(\1)/
b @
}
# Out of the loop, if the top END
/@break-if-end(/ {
/^END */{
s///
s/@break-if-end(\([^)]*\))\(.*\)back(\1)/break-if-end(\1)\2@back(\1)/
}
b @
}
# Input data
/@input()/ {
h; b
}
# Delete the last copy of the board
/@delete-last-board()/ {
s/\(.*\)Board:[^ ]* */\1/
b @
}
# Overlapping boards
/@copy-board()/ {
s/\(Board:[^ ]*\)/\1 \1/
b @
}
# Generation of the initial state boards
/@figures()/ {
# Format: XYFig
# Coordinates of white here and then have to go below the black
# BIG - black, small - white
s/^/Board:\
a8Rb8Nc8Id8Qe8Kf8Ig8Nh8R\
a7Pb7Pc7Pd7Pe7Pf7Pg7Ph7P\
a6 b6 c6 d6 e6 f6 g6 h6 \
a5 b5 c5 d5 e5 f5 g5 h5 \
a4 b4 c4 d4 e4 f4 g4 h4 \
a3 b3 c3 d3 e3 f3 g3 h3 \
a2pb2pc2pd2pe2pf2pg2ph2p\
a1rb1nc1id1qe1kf1ig1nh1r /
# Need a space at the end!
# s/^/Board:\
# a8 b8 c8 d8 e8Kf8 g8 h8r\
# a7 b7 c7 d7 e7 f7 g7 h7 \
# a6 b6 c6 d6 e6nf6 g6 h6 \
# a5 b5 c5 d5 e5 f5 g5 h5 \
# a4 b4 c4 d4 e4 f4 g4 h4 \
# a3 b3 c3 d3 e3 f3 g3 h3 \
# a2 b2 c2 d2 e2 f2 g2 h2k\
# a1 b1 c1 d1 e1 f1 g1 h1 /
s/\n//g
b @
}
# Output boards
/@board()/ {
# Save the stack commands
h
# Remove all but the board (we are always the last board)
s/.*Board://
s/ .*$//
# Decode board
# Pawn, Queen, King, Bishop, Knight, Rook, Zero - psevdokorolʹ to determine the mats chess
s/..z//g
y/pqkinrPQKINR12345678abcd/♟♛♚♝♞♜♙♕♔♗♘♖987654323579/
s/\([1-9e-h]\)\([1-9]\)\(.\)/[\2;\1H\3 /g
# Brighten
s/[8642];[37eg]H/&[48;5;209;37;1m/g
s/[9753];[37eg]H/&[48;5;94;37;1m/g
s/[8642];[59fh]H/&[48;5;94;37;1m/g
s/[9753];[59fh]H/&[48;5;209;37;1m/g
# Double figures
s/e/11/g;s/f/13/g;s/g/15/g;s/h/17/g
s/$/[0m[11H/
# Print the board and return it as it was
i\
[2J[1;3Ha b c d e f g h\
8\
7\
6\
5\
4\
3\
2\
1\
\
Enter the command
p
g
b @
}
# Make progress on a user-entered data
/@move-white()/ {
# Guard basic regex (they need to be carefully protected from malfunctions that,
# else sed will give an error and stops)
# We clean everything but the board and the first two values
h; s/\([^ ]*\) \([^ ]*\).*Board:\([^ ]*\).*/\1 \2 \3/
# Select the cells
s/\([^ ]*\) [^ ]* .*\(\1.\)/&(1:\2)/
s/[^ ]* \([^ ]*\) .*\(\1.\)/&(2:\2)/
# Now they are in the format:
# Nomer_po_poryadku_vvoda: XYFigura
s/.*(\(.....\)).*(\(.....\)).*/\1 \2/
# Now check the following:
# 1. that does not take someone else's, and not an empty shape
/1:..[PQKINR ]/ {
g; s/[^ ]* [^ ]* *//; b @
}
# 2. do not put in place of the figure
/2:..[pqkinr]/ {
g; s/[^ ]* [^ ]* *//; b @
}
# Procedure is as follows:
# Specified coordinates found in the figures change each
# If progress is ahead ...
/2:.*1:/ {
g
# 1 2 3 4 5
/\([^ ]*\) \([^ ]*\) \(.*Board:[^ ]*\)\2.\([^ ]*\)\1\([pqkinr]\)/ {
s//\3\1 \4\2\5/
b move-white::checkpawn
}
}
# Move back
g
# 1 2 3 4 5
s/\([^ ]*\) \([^ ]*\) \(.*Board:[^ ]*\)\1\([pqkinr]\)\([^ ]*\)\2./\3\2\4\5\1 /
# Check to see if the pawn was on the 8th row, if so, it is necessary to convert
# it to the queen (of course can be only one such pawn)
:move-white::checkpawn
s/\([a-h]8\)p/\1q/
b @
}
# Number of remaining pieces
/@count-pieces()/ {
h
# Remove all but the board
s/.*Board://
s/ .*$//
# Remove all but the white pieces
s/[^pqkinrPQKINR]//g
# Believe
s/./1/g
# Return stack commands
G
# G came after a line feed, remove it
s/\n/ /
b @
}
#Estimator available black pieces
/@estimate-black-pieces()/ {
# Pawn - 100, elephant and horse - 300, Rook - 500, Queen - 900, King - 9000,
# else 9000 - psevdofigura black to avoid overflow = 21900
# Cleaning all the excess
h; s/.*Board://; s/ .*$//
# Remove all but counted figures
s/[^PINRQK]//g
# Count the number of * coefficient figure (queen Q - the only one, but may appear in another pawn)
s/P/1/g; s/[NI]/111/g; s/R/11111/g; s/Q/111111111/g
# King put forward to psevdofigure
s/^\(.*\)K/HHHHHHHHH\1/
# Black psevdofigura
s/^/HHHHHHHHH/
# Grouping hundreds of thousands of
s/1111111111/H/g; s/HHHHHHHHHH/T/g
# Insert a colon
s/\(.\)\1*/&:/g
# If there are no units to the end - another colon
/1/ ! s/$/:/
# If not hundreds, up to the last units or colon - even colon
/H/ ! s/[1:]/:&/
y/HT/11/; s/$/:B/
# Add to the saved stack
G; s/\n/ /
b @
}
# Estimator available white pieces
/@estimate-white-pieces()/ {
# Pawn - 100, elephant and horse - 300, Rook - 500, Queen - 900, King - 9000
# Cleaning all the excess
h; s/.*Board://; s/ .*$//
# Remove all but counted figures
s/[^pinrqk]//g
# Count the number of * coefficient figure (queen q - the only one, but may appear in another pawn)
s/p/1/g; s/[ni]/111/g; s/r/11111/g; s/q/111111111/g
# King put forward
s/^\(.*\)k/HHHHHHHHH\1/
# Grouping hundreds of thousands of
s/1111111111/H/g; s/HHHHHHHHHH/T/g
# Insert a colon
s/\(.\)\1*/&:/g
# If there are no units to the end - another colon
/1/ ! s/$/:/
# If not hundreds, up to the last units or colon - even colon
/H/ ! s/[1:]/:&/
y/HT/11/; s/$/:B/
# Add to the saved stack
G; s/\n/ /
b @
}
# For debugging output of the current stack
/@log()/ {
l
q
}
/@l()/ {
h
l
w chess.log
g
}
# Estimator for the position of black pawns
/@estimate-black-pawn()/ {
# Cleaning all the excess
h; s/.*Board://; s/ .*$//
# Leaving only black and white pawns, re-encoding them into understandable coordinates
# Now pawns written like this: XTsvet (where Color - Black or White), separated by a space
s/[a-h][1-8][^Pp]//g; y/Ppabcdefgh/BW12345678/; s/\([1-8]\)[1-8]/ \1/g
# → Step 1
# Find the black pawns on the vertical who are white, whites are the coordinates
# Always after the coordinates of black
:estimate-black-pawn::black
/\([1-8]\)B\(.*\1\)W/ {
s//\1b\2W/
b estimate-black-pawn::black
}
# → Step 2.1
# Translate the coordinates of a sequence of length X
:estimate-black-pawn::x
/[2-8]/ {
s/[2-8]/1&/g
y/2345678/1234567/
b estimate-black-pawn::x
}
# → Step 2.2
# Find a pawn, not screened out in Phase 1, in which the adjacent line on the left are white
:estimate-black-pawn::left
/\( 1*\)B\(.*\11\)W/ {
s//\1b\2W/
b estimate-black-pawn::left
}
# → Step 2.3
# Find a pawn, not screened out in Phase 2, in which on the next line on the right are white
:estimate-black-pawn::right
/ 1\(1*\)B\(.* \1\)W/ {
s// 1\1b\2W/
b estimate-black-pawn::right
}
# As a result, W - white pawns, b - black, B - free black pawns
# Get rid of non-free and white pawns s/ [^ ]*[Wb]//g
# → Step 3
# Consider the cost of the free black pawns
s/ 1B//; s/ 11B/ ::11111B/; s/ 111B/ :1:B/; s/ 1111B/ :1:11111B/; s/ 11111B/ :11:B/
s/ 111111B/ :111:B/; s/ 1111111B/ 1:1111:B/; s/ 11111111B//
# → Step 4
# Saves received, ship stack back, cut out the board and leave the black pawns with coordinates
G; h; s/.*Board://; s/ .*$//; s/[a-h][1-8][^p]//g
# Evaluate the positions of all the pawns
s/.[81]p/::B/g
s/[abcfgh]7p/::1111B/g; s/[de]7p/::B/g
s/[ah][65]p/::111111B/g; s/[bg][65]p/::11111111B/g; s/[cf]6p/::11B/g; s/[de]6p/:1:B/g
s/[bg]5p/:1:11B/g; s/[cf]5p/:1:111111B/g; s/[de]5p/:11:1111B/g
s/[ah]4p/::11111111B/g; s/[bg]4p/:1:11B/g; s/[cf]4p/:1:111111B/g; s/[de]4p/:11:1111B/g
s/[ah][32]p/:1:11B/g; s/[bg][32]p/:1:111111B/g; s/[cf][32]p/:11:1111B/g; s/[de][32]p/:111:11B/g
# Insert spaces between the estimates
s/B/& /g; s/^/ /
# → Step 5
# Return the stored evaluation, we remove the remnants of the stack
G; s/\n\(.*\)\n.*/ \1/
# Add to the saved stack, we clean our garbage, which we piled up -
# Second line there are estimates
G; s/\n.*\n/ /
b @
}
# Estimator for the position of the black king
/@estimate-black-king()/ {
h; s/.*Board://; s/ .*$//
# Allocate King
s/[a-h][1-8][^K]//g
# Consider the weight (the matrix of the game)
s/[ah][18]./::/
s/[de][54]./:111:111111/
s/[cf][54]./:111:/; s/[de][63]./:111:/
s/[bg][54]./:11:1111/; s/[de][72]./:11:1111/; s/[cf][63]./:11:1111/
s/[de][18]./:1:11111111/; s/[ah][54]./:1:11111111/; s/[cf][72]./:1:11111111/; s/[bg][63]./:1:11111111/
s/[bg][72]./:1:11/; s/[ah][63]./:1:11/; s/[cf][81]./:1:11/
s/[a-h][1-9]./::111111/
G; s/\n/B /
b @
}
# Estimator for the position of the black knight
/@estimate-black-knight()/ {
h; s/.*Board://; s/ .*$//
# Select horses
s/[a-h][1-8][^N]//g
# Believe their weight
s/[ah][18]./::B/g
s/[de][54]./:111:11B/g
s/[cf][54]./:11:11111111B/g; s/[de][63]./:11:11111111B/g
s/[cf][36]./:11:1111B/g
s/[bg][54]./:11:B/g; s/[de][72]./:11:B/g; s/[cf][63]./:11:B/g
s/[de][18]./:1:B/g; s/[ah][54]./:1:B/g; s/[cf][72]./:1:B/g; s/[bg][63]./:1:B/g
s/[bg][72]./::11111111B/g; s/[ah][63]./::11111111B/g; s/[cf][81]./::11111111B/g
s/[a-h][1-9]./::1111B/g
s/B/& /g; G; s/ *\n/ /
b @
}
# Estimator for the position of the black bishop
/@estimate-black-bishop()/ {
h; s/.*Board://; s/ .*$//
# Highlight of elephants
s/[a-h][1-8][^I]//g
# Believe their weight
s/[a-h][81]./:::1:1111B/g; s/[ah][1-8]./:::1:1111B/g
s/[bg][72]./:::11:11B/g; s/[c-f][3-6]/:::11:11B/g
s/[a-h][1-9]./:::1:11111111B/g
s/B/& /; G; s/\n/ /
b @
}
# Estimator for the position of the black queen (queen)
/@estimate-black-queen()/ {
h; s/.*Board://; s/ .*$//
# Distinguish the enemy king and queen
s/[a-h][1-8][^Qk]//g
# If one of the figures on the field not return
/Q/,/k/ ! {
g; b @
}
# King pushed forward
s/\(.*\)\(..k\)\(.*\)/\2\1\3/
# If we have a second queen, then put it through a separator
# In front of him and sculpt King
s/\(..k\)\(..Q\)\(..Q\)/ \1\2\# \1\3/
# Remove the figure, the coordinates of the numbers, we arrange spaces around numbers to limit
y/abcdefgh/12345678/; s/\([1-9]\)\(.\)./\1 \2 /g
# Group the coordinates, you get X1 X2 Y1 Y2 (on two lines)
# We captured by three values, but will not confusion as use spaces,
# And between pairs of numbers - "lattice"
s/\([1-8]\) \([1-8]\) \([1-8]\)/\1 \3 \2/g
# Translate the coordinates of a sequence of length coordinate values
:estimate-black-queen::xy
/[2-8]/ {
s/[2-8]/1&/g
y/2345678/1234567/
b estimate-black-queen::xy
}
# Should be able to (8 - the greatest distance):
# 8 - (XM-Xm) + 8 - (YM-Ym) => 16-XM+Xm-YM+Ym => 16-(XM+YM)+(Xm+Ym)
# Sort - a large coordinate ahead
s/ \(11*\) \(\111*\)/ \2 \1/g
#4 numbers obtained for each pair of figures: XM Xm YM Ym, you need to put them in pairs,
# Second deuce portable ahead and add up to 16
s/\(11*\) \(11*\) \(11*\) \(11*\)/1111111111111111\2\4 \1\3/g
# Subtract the second coordinate of the first
s/\(11*\)\(1*\) \1/\2/g
# Ciesla to combine the two figures
s/[# ]//g
# Grouping hundreds of thousands of
s/1111111111/H/g; s/HHHHHHHHHH/T/g
# Insert a colon
s/\(.\)\1*/&:/g
# If there are no units to the end - another colon
/1/ ! s/$/:/
# If not hundreds, up to the last units or colon - even colon
/H/ ! s/[1:]/:&/
y/HT/11/
G; s/\n/B /
b @
}
# Sum up the numbers on the stack until it encounters a word ARRAY
/@sum-array()/ {
h
/ARRAY.*/ {
s///
s/$/ ::::::S/
:sum-array::shift
/[1:][1:]*B/ {
# Addition of discharge
:sum-array::sum
/11*B/ {
s/\(11*\)B\(.*\)\(1*\)S/B\2\1\3S/
s/:1111111111\(1*\)S/1:\1S/
b sum-array::sum
}
# Shift discharge
s/:B/B/g; s/:\(1*\)S/S \1:/
b sum-array::shift
}
s/:\(1*\)S/S \1:/; s/[^1:]//g
G; s/ARRAY/#&/; s/:\n.*#ARRAY */B /
}
b @
}
# Subtraction of numbers on the stack from the first, until a word ARRAY
/@sub-array()/ {
/ *ARRAY.*/ {
h; s///
# Is replaced with the first of a letter to distinguish
s/B */M /
# In front of each number must stop
s/^/:/; s/ / :/g
# From the first day of shooting LSB
s/:\(1*\)\(M.*\)/:\2 :\1#S/
:sub-array::loop
# Now go through the junior ranks of the remaining numbers
:sub-array::minus
/:\(11*\)\(B.*\) :\1\(1*\)#S/ {
s//:\2 :\3#S/
b sub-array::minus
}
# Significant bits to subtract left?
/:11*B/ {
# Transfer the bits to be subtracted Jr.
:sub-array::cy
# If there is nothing to carry, everything turns out the number is less than zero,
# Return a zero exit
/1.*M/ ! {
s/.*/:::B/
b sub-array::end
}
s/\(.*\)1:\(.*M\)/\1:1111111111\2/
/1M/ ! b sub-array::cy
# Add to the subtrahend
s/:\(1*\)\(M.*\) \(:.*\)#S/:\2 \3\1#S/
b sub-array::minus
}
# Cut away all the empty category now, those who do not have any left, remove
s/:\([BM]\)/\1/g; s/ :*B//g
# Take the next digit
s/:\(1*\)\(M.*\) \([^ ]*\)#S/:\2 :\1#S \3S/
# If there is that subtract, subtract
/B/ b sub-array::loop
# Remove superfluous normaliziruem
s/[#MS ]//g; s/://
:sub-array::end
G; s/ARRAY/#&/; s/\n.*#ARRAY */B /
}
b @
}
# Selection of this figure (returns a string)
# XYF__XYF__ where F - the name of the figure, __ - a place in the enumeration position
/@select-figures(.)/ {
h
# Remove all unnecessary data from the parameter marker mark
s/@select-figures(\(.\))\(.*\)/\2 Selected:\1/
s/.*Board://
s/ .*Selected:/ Selected:/
# Highlight of the boards that have user
:select-figures::select
/\([a-h][0-9]\)\(.\)\(.* Selected:\2\)/ {
s//\3\1\2__/
b select-figures::select
}
# Remove the marker and mutilated board
s/.*Selected:.//
# Return stack ago
G; s/\n/END /
b @
}
/@iter-knight()/ {
# Remove the horse that finished the course
s/^...XX//
# Exit if there is nothing to go
/^END/ b @
# Select the first horse
h; s/\(.....\).*/\1/
# Encoding moves: __ - has not been made, XX - made all possible
# Left, Down, Up, Right, first written in the course of two cells, for example:
# LU - left into two, one on top
/__/ {
s//LU/
# Shift the coordinate of X-2, Y+1, 0 - a sign that progress is impossible
y/abcdefgh/00abcdef/
y/12345678/23456780/
b common::go
}
/LU/ {
s//UL/
# X-1, Y+2
y/abcdefgh/0abcdefg/
y/12345678/34567800/
b common::go
}
/UL/ {
s//UR/
# X+1, Y+2
y/abcdefgh/bcdefgh0/
y/12345678/34567800/
b common::go
}
/UR/ {
s//RU/
# X+2, Y+1
y/abcdefgh/cdefgh00/
y/12345678/23456780/
b common::go
}
/RU/ {
s//RD/
# X+2, Y-1
y/abcdefgh/cdefgh00/
y/12345678/01234567/
b common::go
}
/RD/ {
s//DR/
# X+1, Y-2
y/abcdefgh/bcdefgh0/
y/12345678/00123456/
b common::go
}
/DR/ {
s//DL/
# X-1, Y-2
y/abcdefgh/0abcdefg/
y/12345678/00123456/
b common::go
}
/DL/ {
s//XX/
# X-2, Y-1
y/abcdefgh/00abcdef/
y/12345678/01234567/
b common::go
}
b common::go
}
# King goes to one cell anywhere N
# Code to the cardinal W E
# __ → NN → EN → EE → SE → SS → WS → WW → NW → XX S
/@iter-king()/ {
# Remove the king, who finished the course
s/^...XX//
# Exit if there is nothing to go
/^END/ b @
# Highlight of the first (and only) King
h; s/\(.....\).*/\1/
# Change the current selected position to the next
s/$/ __NNENEESESSWSWWNWXX/
s/\(..\) \(.*\1\)\(..\)/\3 \2\3/; s/ .*//
# Replace koordinty, according to the selected position
# Y+1
/N/ y/12345678/23456780/
# Y-1
/S/ y/12345678/01234567/
# X-1
/W/ y/abcdefgh/0abcdefg/
# X+1
/E/ y/abcdefgh/bcdefgh0/
b common::go
}
# Rook goes vertically or horizontally on any number of turns, N
# If no one is on the way W E
# Walks starting from the current position to the specified direction S
/@iter-rook()/ {
# Remove the boat, which finished the course
s/^...XX//
# Exit if there is nothing to go
/^END/ b @
# Select the first boat
h; s/\(.....\).*/\1/
# Our first direction - east, then go to the following areas
/__/ s/\(\(.\).*\)__/\1E\2/
/E0/ s/\(\(.\).*\)E./\1W\2/
/W0/ s/\(.\(.\).*\)W./\1N\2/
/N0/ s/\(.\(.\).*\)N./\1S\2/
s/S0/XX/
/E/ y/abcdefgh/bcdefgh0/
/W/ y/abcdefgh/0abcdefg/
/S/ y/12345678/01234567/
/N/ y/12345678/23456780/
# Rewrite the state in the coordinates of the selected shape as the figure of the progress will be gone
/[SN]/ s/\(.\).\(..\(.\)\)/\1\3\2/
/[WE]/ s/.\(...\(.\)\)/\2\1/
/[0X]/ ! {
# Return stack, remove everything after and before the board on the stack
s/$/#/; G; s/\n.*\(Board:[^ ]*\).*/\1/
# If the selected position psevdokorol, more on what we do not pay attention
/^\(..\).*\1z/ ! {
# Check, whether or not in the selected position has its own shape, if necessary, stop scan immediately
s/^\(..\)R\(.\).*\(\1[PQKINR]\).*/00R\20#\3/
s/^\(..\)r\(.\).*\(\1[pqkinr]\).*/00r\20#\3/
# If there is an alien figure, then you can move and go for it - there is no
s/^\(..\)R\(.\).*\(\1[pqkinr]\).*/\1R\20#\3/
s/^\(..\)r\(.\).*\(\1[PQKINR]\).*/\1r\20#\3/
}
s/#.*//
}
b common::go
}