-
Notifications
You must be signed in to change notification settings - Fork 5
/
moves.c
2743 lines (2511 loc) · 121 KB
/
moves.c
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
/*
* moves.c - Move generation and checking
*
* Copyright 1991 by Digital Equipment Corporation, Maynard,
* Massachusetts.
*
* Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
* 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free
* Software Foundation, Inc.
*
* Enhancements Copyright 2005 Alessandro Scotti
*
* The following terms apply to Digital Equipment Corporation's copyright
* interest in XBoard:
* ------------------------------------------------------------------------
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of Digital not be
* used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
*
* DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
* DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
* ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
* ------------------------------------------------------------------------
*
* The following terms apply to the enhanced version of XBoard
* distributed by the Free Software Foundation:
* ------------------------------------------------------------------------
*
* GNU XBoard is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* GNU XBoard is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/. *
*
*------------------------------------------------------------------------
** See the file ChangeLog for a revision history. */
#include "config.h"
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#if HAVE_STRING_H
# include <string.h>
#else /* not HAVE_STRING_H */
# include <strings.h>
#endif /* not HAVE_STRING_H */
#include "common.h"
#include "backend.h"
#include "moves.h"
#include "parser.h"
int WhitePiece P((ChessSquare));
int BlackPiece P((ChessSquare));
int SameColor P((ChessSquare, ChessSquare));
int PosFlags(int index);
int quickFlag;
char *pieceDesc[EmptySquare];
char *defaultDesc[EmptySquare] = {
"fmWfceFifmnD", "N", "B", "R", "Q",
"F", "A", "BN", "RN", "W", "K",
"mRcpR", "N0", "BW", "RF", "gQ",
"", "", "QN", "", "N", "",
"", "", "", "", "",
"", "", "", "", "", "",
"", "", "", "", "",
"", "", "", "", "", "K"
};
int
WhitePiece (ChessSquare piece)
{
return (int) piece >= (int) WhitePawn && (int) piece < (int) BlackPawn;
}
int
BlackPiece (ChessSquare piece)
{
return (int) piece >= (int) BlackPawn && (int) piece < (int) EmptySquare;
}
#if 0
int
SameColor (ChessSquare piece1, ChessSquare piece2)
{
return ((int) piece1 >= (int) WhitePawn && /* [HGM] can be > King ! */
(int) piece1 < (int) BlackPawn &&
(int) piece2 >= (int) WhitePawn &&
(int) piece2 < (int) BlackPawn)
|| ((int) piece1 >= (int) BlackPawn &&
(int) piece1 < (int) EmptySquare &&
(int) piece2 >= (int) BlackPawn &&
(int) piece2 < (int) EmptySquare);
}
#else
#define SameColor(piece1, piece2) (piece1 < EmptySquare && piece2 < EmptySquare && (piece1 < BlackPawn) == (piece2 < BlackPawn) || piece1 == DarkSquare || piece2 == DarkSquare)
#endif
unsigned char pieceToChar[EmptySquare+1] = {
'P', 'N', 'B', 'R', 'Q', 'F', 'E', 'A', 'C', 'W', 'M',
'O', 'H', 'I', 'J', 'G', 'D', 'V', 'L', 'S', 'U', 'K',
'p', 'n', 'b', 'r', 'q', 'f', 'e', 'a', 'c', 'w', 'm',
'o', 'h', 'i', 'j', 'g', 'd', 'v', 'l', 's', 'u', 'k',
'x' };
unsigned char pieceNickName[EmptySquare];
int promoPartner[EmptySquare];
char
PieceToChar (ChessSquare p)
{
int c;
if((int)p < 0 || (int)p >= (int)EmptySquare) return('?'); /* [HGM] for safety */
c = pieceToChar[(int) p];
if(c & 128) c = c & 63 | 64;
return c;
}
char
PieceSuffix (ChessSquare p)
{
int c;
if((int)p < 0 || (int)p >= (int)EmptySquare) return 0; /* [HGM] for safety */
c = pieceToChar[(int) p];
if(c < 128) return 0;
return SUFFIXES[c - 128 >> 6];
}
int
PieceToNumber (ChessSquare p) /* [HGM] holdings: count piece type, ignoring non-participating piece types */
{
int i=0;
ChessSquare start = (int)p >= (int)BlackPawn ? BlackPawn : WhitePawn;
while(start++ != p) if(pieceToChar[start-1] != '.' && pieceToChar[start-1] != '+') i++;
return i;
}
ChessSquare
CharToPiece (int c)
{
int i;
if(c == '.') return EmptySquare;
for(i=0; i< (int) EmptySquare; i++)
if(pieceNickName[i] == c) return (ChessSquare) i;
for(i=0; i< (int) EmptySquare; i++)
if(pieceToChar[i] == c) return (ChessSquare) i;
return EmptySquare;
}
void
CopyBoard (Board to, Board from)
{
int i, j;
for (i = 0; i < BOARD_HEIGHT; i++)
for (j = 0; j < BOARD_WIDTH; j++)
to[i][j] = from[i][j];
for (j = 0; j < BOARD_FILES; j++) // [HGM] gamestate: copy castling rights and ep status
to[VIRGIN][j] = from[VIRGIN][j],
to[CASTLING][j] = from[CASTLING][j];
to[HOLDINGS_SET] = 0; // flag used in ICS play
}
int
CompareBoards (Board board1, Board board2)
{
int i, j;
for (i = 0; i < BOARD_HEIGHT; i++)
for (j = 0; j < BOARD_WIDTH; j++) {
if (board1[i][j] != board2[i][j])
return FALSE;
}
return TRUE;
}
char defaultName[] = "PNBRQ......................................K" // white
"pnbrq......................................k"; // black
char shogiName[] = "PNBRLS...G.++++++..........................K" // white
"pnbrls...g.++++++..........................k"; // black
char xqName[] = "PH.R.AE..K.C................................" // white
"ph.r.ae..k.c................................"; // black
char *
CollectPieceDescriptors ()
{ // make a line of piece descriptions for use in the PGN Piece tag:
// dump all engine defined pieces, and pieces with non-standard names,
// but suppress black pieces that are the same as their white counterpart
ChessSquare p;
static char buf[MSG_SIZ], s[2];
char *m, *pieceName = defaultName;
int len, c, d;
*buf = NULLCHAR;
if(!pieceDefs) return "";
if(gameInfo.variant == VariantChu) return ""; // for now don't do this for Chu Shogi
if(gameInfo.variant == VariantShogi) pieceName = shogiName;
if(gameInfo.variant == VariantXiangqi) pieceName = xqName;
for(p=WhitePawn; p<EmptySquare; p++) {
if((c = pieceToChar[p]) == '.' || c == '~') continue; // does not participate
m = pieceDesc[p]; d = (c == '+' ? pieceToChar[DEMOTED(p)] : c);
if(p >= BlackPawn && pieceToChar[BLACK_TO_WHITE p] == (c & ~32)
&& (c != '+' || pieceToChar[DEMOTED(BLACK_TO_WHITE p)] == d)) {// black member of normal pair
char *wm = pieceDesc[BLACK_TO_WHITE p];
if(!m && !wm || m && wm && !strcmp(wm, m)) continue; // moves as a white piece
} else // white or unpaired black
if((p < BlackPawn || CharToPiece(d & ~32) != EmptySquare) && // white or lone black
!pieceDesc[p] /*&& pieceName[p] == c*/) continue; // orthodox piece known by its usual name
// TODO: listing pieces because of unusual name can only be done if we have accurate Betza of all defaults
if(!m) m = defaultDesc[p];
if(!m) continue;
len = strlen(buf);
*s = (d > 128 ? SUFFIXES[d-128>>6] : 0); d = 64 + (d & 63);
snprintf(buf+len, MSG_SIZ-len, "%s%s%c%s:%s", len ? ";" : "", c == '+' ? "+" : "", d, s, m);
}
return buf;
}
int
LoadPieceDesc (char *s)
{
ChessSquare piece;
static char suf[] = SUFFIXES;
char *r, *p, *q = s;
int ok = TRUE, promoted, c;
while(q && *s) {
p = s;
q = strchr(s, ';');
if(q) *q = 0, s = q+1;
if(*p == '+') promoted = 1, p++; else promoted = 0;
c = *p++;
if(!c) { ok = FALSE; continue; } // bad syntax
if(*p && (r = strchr(suf, *p))) c += 64*(r - suf + 1), p++;
if(*p++ != ':') { ok = FALSE; continue; } // bad syntax
if(!strcmp(p, "(null)")) continue; // handle bug in writing of XBoard 4.8.0
piece = CharToPiece(c);
if(piece >= EmptySquare) { ok = FALSE; continue; } // non-existent piece
if(promoted) {
piece = promoPartner[piece];
if(pieceToChar[piece] != '+') { ok = FALSE; continue; } // promoted form does not exist
}
ASSIGN(pieceDesc[piece], p);
if(piece < BlackPawn && (pieceToChar[WHITE_TO_BLACK piece] == pieceToChar[piece] + 32 || promoted)) {
ASSIGN(pieceDesc[WHITE_TO_BLACK piece], p);
}
pieceDefs = TRUE;
if(q) *q = ';';
}
return ok;
}
// [HGM] gen: configurable move generation from Betza notation sent by engine.
// Some notes about two-leg moves: GenPseudoLegal() works in two modes, depending on whether a 'kill-
// square has been set: without one is generates all moves, and a global int legNr flags in bits 0 and 1
// if the move has 1 or 2 legs. Only the marking of squares makes use of this info, by only marking
// target squares of leg 1 (rejecting null move). A dummy move with MoveType 'FirstLeg' to the relay square
// is generated, so a cyan marker can be put there, and other functions can ignore such a move. When the
// user selects this square, it becomes the kill-square. Once a kill-square is set, only 2-leg moves are
// generated that use that square as relay, plus 1-leg moves, so the 1-leg move that goes to the kill-square
// can be marked during 2nd-leg entry to terminate the move there. For judging the pseudo-legality of the
// 2nd leg, the from-square has to be considered empty, although the moving piece is still on it.
Boolean pieceDefs;
// alphabet "abcdefghijklmnopqrstuvwxyz"
char symmetry[] = "FBNW.FFW.NKN.NW.QR....W..N";
char xStep[] = "2110.130.102.10.00....0..2";
char yStep[] = "2132.133.313.20.11....1..3";
char dirType[] = "01000104000200000260050000";
char upgrade[] = "AFCD.BGH.JQL.NO.KW....R..Z";
char rotate[] = "DRCA.WHG.JKL.NO.QB....F..Z";
// alphabet "a b c d e f g h i j k l m n o p q r s t u v w x y z "
int dirs1[] = { 0,0x3C,0,0,0,0xC3,0,0, 0,0,0,0xF0,0,0,0,0,0,0x0F,0 ,0,0,0 ,0,0,0,0 };
int dirs2[] = { 0,0x18,0,0,0,0x81,0,0xFF,0,0,0,0x60,0,0,0,0,0,0x06,0x66,0,0,0x99,0,0,0,0 };
int dirs3[] = { 0,0x38,0,0,0,0x83,0,0xFF,0,0,0,0xE0,0,0,0,0,0,0x0E,0xEE,0,0,0xBB,0,0,0,0 };
int dirs4[] = { 0,0x10,0,0,0,0x01,0,0xFF,0,0,0,0x40,0,0,0,0,0,0x04,0x44,0,0,0x11,0,0,0,0 };
int rot[][4] = { // rotation matrices for each direction
{ 1, 0, 0, 1 },
{ 0, 1, 1, 0 },
{ 0, 1,-1, 0 },
{ 1, 0, 0,-1 },
{-1, 0, 0,-1 },
{ 0,-1,-1, 0 },
{ 0,-1, 1, 0 },
{-1, 0, 0, 1 }
};
void
OK (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR cl)
{
(*(int*)cl)++;
}
void
MovesFromString (Board board, int flags, int f, int r, int tx, int ty, int angle, int range, char *desc, MoveCallback cb, VOIDSTAR cl)
{
char buf[80], *p = desc, *atom = NULL;
int mine, his, dir, bit, occup, i, ep, promoRank = -1;
ChessMove promo= NormalMove; ChessSquare pc = board[r][f];
if(pc == DarkSquare) return; // this is not a piece, but a 'hole' in the board
if(flags & F_WHITE_ON_MOVE) his = 2, mine = 1; else his = 1, mine = 2;
if(pc == WhitePawn || pc == WhiteLance) promo = WhitePromotion, promoRank = BOARD_HEIGHT-1; else
if(pc == BlackPawn || pc == BlackLance) promo = BlackPromotion, promoRank = 0;
while(*p) { // more moves to go
int expo = -1, dx, dy, x, y, mode, dirSet, ds2=0, retry=0, initial=0, jump=1, skip = 0, all = 0;
char *cont = NULL;
while(*p == 'i') initial++, desc = ++p;
while(islower(*p)) p++; // skip prefixes
if(!isupper(*p)) return; // syntax error: no atom
dx = xStep[*p-'A'] - '0';// step vector of atom
dy = yStep[*p-'A'] - '0';
dirSet = 0; // build direction set based on atom symmetry
switch(symmetry[*p-'A']) {
case 'B': expo = 0; // bishop, slide
case 'F': all = 0xAA; // diagonal atom (degenerate 4-fold)
if(tx >= 0) goto king; // continuation legs specified in K/Q system!
while(islower(*desc) && (i = dirType[*desc-'a']) != '0') {
int b = dirs1[*desc-'a']; // use wide version
if( islower(desc[1]) &&
((i | dirType[desc[1]-'a']) & 3) == 3) { // combinable (perpendicular dim)
b = dirs1[*desc-'a'] & dirs1[desc[1]-'a']; // intersect wide & perp wide
desc += 2;
} else desc++;
dirSet |= b;
}
dirSet &= 0xAA; if(!dirSet) dirSet = 0xAA;
break;
case 'R': expo = 0; // rook, slide
case 'W': all = 0x55; // orthogonal atom (non-deg 4-fold)
if(tx >= 0) goto king; // continuation legs specified in K/Q system!
while(islower(*desc) && (dirType[*desc-'a'] & ~4) != '0') dirSet |= dirs2[*desc++-'a'];
dirSet &= 0x55; if(!dirSet) dirSet = 0x55;
dirSet = (dirSet << angle | dirSet >> 8-angle) & 255; // re-orient direction system
break;
case 'N': all = 0xFF; // oblique atom (degenerate 8-fold)
if(tx >= 0) goto king; // continuation legs specified in K/Q system!
if(*desc == 'h') { // chiral direction sets 'hr' and 'hl'
dirSet = (desc[1] == 'r' ? 0x55 : 0xAA); desc += 2;
} else
while(islower(*desc) && (i = dirType[*desc-'a']) != '0') {
int b = dirs2[*desc-'a']; // when alone, use narrow version
if(desc[1] == 'h') b = dirs1[*desc-'a'], desc += 2; // dirs1 is wide version
else if(*desc == desc[1] || islower(desc[1]) && i < '4'
&& ((i | dirType[desc[1]-'a']) & 3) == 3) { // combinable (perpendicular dim or same)
b = dirs1[*desc-'a'] & dirs2[desc[1]-'a']; // intersect wide & perp narrow
desc += 2;
} else desc++;
dirSet |= b;
}
if(!dirSet) dirSet = 0xFF;
break;
case 'Q': expo = 0; // queen, slide
case 'K': all = 0xFF; // non-deg (pseudo) 8-fold
king:
while(islower(*desc) && (i = dirType[*desc-'a']) != '0') {
int b = dirs4[*desc-'a']; // when alone, use narrow version
if(desc[1] == *desc) desc++; // doubling forces alone
else if(islower(desc[1]) && i < '4'
&& ((i | dirType[desc[1]-'a']) & 3) == 3) { // combinable (perpendicular dim or same)
b = dirs3[*desc-'a'] & dirs3[desc[1]-'a']; // intersect wide & perp wide
desc += 2;
} else desc++;
dirSet |= b;
}
if(!dirSet) dirSet = (tx < 0 ? 0xFF // default is all directions, but in continuation leg
: all == 0xFF ? 0xEF : 0x45); // omits backward, and for 4-fold atoms also diags
dirSet = (dirSet << angle | dirSet >> 8-angle) & 255; // re-orient direction system
ds2 = dirSet & 0xAA; // extract diagonal directions
if(dirSet &= 0x55) // start with orthogonal moves, if present
retry = 1, dx = 0; // and schedule the diagonal moves for later
else dx = dy, dirSet = ds2; // if no orthogonal directions, do diagonal immediately
break; // should not have direction indicators
default: return; // syntax error: invalid atom
}
if(mine == 2 && tx < 0) dirSet = dirSet >> 4 | dirSet << 4 & 255; // invert black moves
mode = 0; // build mode mask
if(*desc == 'm') mode |= 4, desc++; // move to empty
if(*desc == 'c') mode |= his, desc++; // capture foe
if(*desc == 'd') mode |= mine, desc++; // destroy (capture friend)
if(*desc == 'e') mode |= 8, desc++; // e.p. capture last mover
if(*desc == 't') mode |= 16, desc++; // exclude enemies as hop platform ('test')
if(*desc == 'p') mode |= 32, desc++; // hop over occupied
if(*desc == 'g') mode |= 64, desc++; // hop and toggle range
if(*desc == 'o') mode |= 128, desc++; // wrap around cylinder board
if(*desc == 'y') mode |= 512, desc++; // toggle range on empty square
if(*desc == 'n') jump = 0, desc++; // non-jumping
while(*desc == 'j') jump++, desc++; // must jump (on B,R,Q: skip first square)
if(*desc == 'a') cont = ++desc; // move again after doing what preceded it
if(isdigit(*++p)) expo = atoi(p++); // read exponent
if(expo > 9) p++; // allow double-digit
desc = p; // this is start of next move
if(initial == 2) { if(board[r][f] != initialPosition[r-2*his+3][f]) continue; initial = 0; } else
if(initial && !range) {
if( (board[r][f] != initialPosition[r][f] ||
r == 0 && board[TOUCHED_W] & 1<<f ||
r == BOARD_HEIGHT-1 && board[TOUCHED_B] & 1<<f )) continue;
initial = 0;
}
if(expo > 0 && dx == 0 && dy == 0) { // castling indicated by O + number
mode |= 1024; dy = 1;
}
if(expo < 0) expo = 1; // use 1 for default
if(!cont) {
if(!(mode & 15)) mode |= his + 4; // no mode spec, use default = mc
} else {
strncpy(buf, cont, 80); cont = buf; // copy next leg(s), so we can modify
atom = buf; while(islower(*atom)) atom++; // skip to atom
if(mode & 32) mode ^= 256 + 32; // in non-final legs 'p' means 'pass through'
if(mode & 64 + 512) {
mode |= 256; // and 'g' too, but converts leaper <-> slider
if(mode & 512) mode ^= 0x304; // and 'y' is m-like 'g'
*atom = upgrade[*atom-'A']; // replace atom, BRQ <-> FWK
atom[1] = atom[2] = '\0'; // make sure any old range is stripped off
if(expo == 1) atom[1] = '0'; // turn other leapers into riders
}
if(!(mode & 0x30F)) mode |= 4; // and default of this leg = m
}
if(dy == 1) skip = jump - 1, jump = 1; // on W & F atoms 'j' = skip first square
do {
for(dir=0, bit=1; dir<8; dir++, bit += bit) { // loop over directions
int i = expo, j = skip, hop = mode, vx, vy, loop = 0;
if(!(bit & dirSet)) continue; // does not move in this direction
if(dy != 1 || mode & 1024) j = 0; //
vx = dx*rot[dir][0] + dy*rot[dir][1]; // rotate step vector
vy = dx*rot[dir][2] + dy*rot[dir][3];
if(tx < 0) x = f, y = r; // start square
else x = tx, y = ty; // from previous to-square if continuation
do { // traverse ray
x += vx; y += vy; // step to next square
if(y < 0 || y >= BOARD_HEIGHT) break; // vertically off-board: always done
if(x < BOARD_LEFT) { if(mode & 128) x += BOARD_RGHT - BOARD_LEFT, loop++; else break; }
if(x >= BOARD_RGHT) { if(mode & 128) x -= BOARD_RGHT - BOARD_LEFT, loop++; else break; }
if(j) { j--; continue; } // skip irrespective of occupation
if(board[y][x] == DarkSquare) break; // black squares are supposed to be off board
if(!jump && board[y - vy + vy/2][x - vx + vx/2] != EmptySquare) break; // blocked
if(jump > 1 && board[y - vy + vy/2][x - vx + vx/2] == EmptySquare) break; // no hop
if(x == f && y == r && !loop) occup = 4; else // start square counts as empty (if not around cylinder!)
if(board[y][x] < BlackPawn) occup = 0x101; else
if(board[y][x] < EmptySquare) occup = 0x102; else
occup = 4;
if(initial && expo - i + 1 != range) { if(occup == 4) continue; else break; }
if(cont) { // non-final leg
if(mode&16 && his&occup) occup &= 3;// suppress hopping foe in t-mode
if(occup & mode) { // valid intermediate square, do continuation
char origAtom = *atom;
int rg = (expo != 1 ? expo - i + 1 : range); // pass length of last *slider* leg
if(!(bit & all)) *atom = rotate[*atom - 'A']; // orth-diag interconversion to make direction valid
if(occup & mode & 0x104) // no side effects, merge legs to one move
MovesFromString(board, flags, f, r, x, y, dir, rg, cont, cb, cl);
if(occup & mode & 3 && (killX < 0 || kill2X < 0 && (legNr > 1 || killX == x && killY == y) ||
(legNr == 1 ? kill2X == x && kill2Y == y : killX == x && killY == y))) { // destructive first leg
int cnt = 0;
legNr <<= 1;
MovesFromString(board, flags, f, r, x, y, dir, rg, cont, &OK, &cnt); // count possible continuations
legNr >>= 1;
if(cnt) { // and if there are
if(legNr & 1 ? killX < 0 : kill2X < 0) cb(board, flags, FirstLeg, r, f, y, x, cl); // then generate their first leg
legNr <<= 1;
MovesFromString(board, flags, f, r, x, y, dir, rg, cont, cb, cl);
legNr >>= 1;
}
}
*atom = origAtom; // undo any interconversion
}
if(occup != 4) break; // occupied squares always terminate the leg
continue;
}
if(hop & 32+64) { if(occup != 4) { if(hop & 64 && i != 1) i = 2; hop &= 31; } continue; } // hopper
ep = board[EP_RANK];
if(mode & 8 && occup == 4 && board[EP_FILE] == x && (y == (ep & 127) || y - vy == ep - 128)) { // to e.p. square (or 2nd e.p. square)
cb(board, flags, mine == 1 ? WhiteCapturesEnPassant : BlackCapturesEnPassant, r, f, y, x, cl);
}
if(mode & 1024) { // castling
i = 2; // kludge to elongate move indefinitely
if(occup == 4) continue; // skip empty squares
if((x == BOARD_LEFT + skip || x > BOARD_LEFT + skip && vx < 0 && board[y][x-1-skip] == DarkSquare)
&& board[y][x] == initialPosition[y][x]) { // reached initial corner piece
if(pc != WhiteKing && pc != BlackKing || expo == 1) { // non-royal castling (to be entered as two-leg move via 'Rook')
if(killX < 0) cb(board, flags, FirstLeg, r, f, y, x, cl); if(killX < f)
legNr <<= 1, cb(board, flags, NormalMove, r, f, y, f - expo, cl), legNr >>= 1;
} else
cb(board, flags, mine == 1 ? WhiteQueenSideCastle : BlackQueenSideCastle, r, f, y, f - expo, cl);
}
if((x == BOARD_RGHT-1-skip || x < BOARD_RGHT-1-skip && vx > 0 && board[y][x+1+skip] == DarkSquare)
&& board[y][x] == initialPosition[y][x]) {
if(pc != WhiteKing && pc != BlackKing || expo == 1) {
if(killX < 0) cb(board, flags, FirstLeg, r, f, y, x, cl); if(killX > f)
legNr <<= 1, cb(board, flags, NormalMove, r, f, y, f + expo, cl), legNr >>= 1;
} else
cb(board, flags, mine == 1 ? WhiteKingSideCastle : BlackKingSideCastle, r, f, y, f + expo, cl);
}
break;
}
if(mode & 16 && (board[y][x] == WhiteKing || board[y][x] == BlackKing)) break; // tame piece, cannot capture royal
if(occup & mode) cb(board, flags, y == promoRank ? promo : NormalMove, r, f, y, x, cl); // allowed, generate
if(occup != 4) break; // not valid transit square
} while(--i);
}
dx = dy; dirSet = ds2; // prepare for diagonal moves of K,Q
} while(retry-- && ds2); // and start doing them
if(tx >= 0) break; // don't do other atoms in continuation legs
}
} // next atom
// [HGM] move generation now based on hierarchy of subroutines for rays and combinations of rays
void
SlideForward (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
int i, rt, ft = ff;
for (i = 1;; i++) {
rt = rf + i;
if (rt >= BOARD_HEIGHT) break;
if (SameColor(board[rf][ff], board[rt][ft])) break;
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
if (board[rt][ft] != EmptySquare) break;
}
}
void
SlideBackward (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
int i, rt, ft = ff;
for (i = 1;; i++) {
rt = rf - i;
if (rt < 0) break;
if (SameColor(board[rf][ff], board[rt][ft])) break;
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
if (board[rt][ft] != EmptySquare) break;
}
}
void
SlideVertical (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
SlideForward(board, flags, rf, ff, callback, closure);
SlideBackward(board, flags, rf, ff, callback, closure);
}
void
SlideSideways (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
int i, s, rt = rf, ft;
for(s = -1; s <= 1; s+= 2) {
for (i = 1;; i++) {
ft = ff + i*s;
if (ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
if (SameColor(board[rf][ff], board[rt][ft])) break;
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
if (board[rt][ft] != EmptySquare) break;
}
}
}
void
SlideDiagForward (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
int i, s, rt, ft;
for(s = -1; s <= 1; s+= 2) {
for (i = 1;; i++) {
rt = rf + i;
ft = ff + i * s;
if (rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
if (SameColor(board[rf][ff], board[rt][ft])) break;
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
if (board[rt][ft] != EmptySquare) break;
}
}
}
void
SlideDiagBackward (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
int i, s, rt, ft;
for(s = -1; s <= 1; s+= 2) {
for (i = 1;; i++) {
rt = rf - i;
ft = ff + i * s;
if (rt < 0 || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
if (SameColor(board[rf][ff], board[rt][ft])) break;
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
if (board[rt][ft] != EmptySquare) break;
}
}
}
void
Rook (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
SlideVertical(board, flags, rf, ff, callback, closure);
SlideSideways(board, flags, rf, ff, callback, closure);
}
void
Bishop (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
SlideDiagForward(board, flags, rf, ff, callback, closure);
SlideDiagBackward(board, flags, rf, ff, callback, closure);
}
void
Sting (Board board, int flags, int rf, int ff, int dy, int dx, MoveCallback callback, VOIDSTAR closure)
{ // Lion-like move of Horned Falcon and Soaring Eagle
int ft = ff + dx, rt = rf + dy;
if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) return;
legNr += 2;
if (!SameColor(board[rf][ff], board[rt][ft]))
callback(board, flags, killX < 0 && board[rt][ft] != EmptySquare ? FirstLeg : NormalMove, rf, ff, rt, ft, closure);
legNr -= 2;
ft += dx; rt += dy;
if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) return;
legNr += 2;
if (!SameColor(board[rf][ff], board[rt][ft]))
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
if (!SameColor(board[rf][ff], board[rf+dy][ff+dx]))
callback(board, flags, NormalMove, rf, ff, rf, ff, closure);
legNr -= 2;
}
void
StepForward (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
int ft = ff, rt = rf + 1;
if (rt >= BOARD_HEIGHT) return;
if (SameColor(board[rf][ff], board[rt][ft])) return;
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
}
void
StepBackward (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
int ft = ff, rt = rf - 1;
if (rt < 0) return;
if (SameColor(board[rf][ff], board[rt][ft])) return;
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
}
void
StepSideways (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
int ft, rt = rf;
ft = ff + 1;
if (!(rt >= BOARD_HEIGHT || ft >= BOARD_RGHT) && !SameColor(board[rf][ff], board[rt][ft]))
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
ft = ff - 1;
if (!(rt >= BOARD_HEIGHT || ft < BOARD_LEFT) && !SameColor(board[rf][ff], board[rt][ft]))
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
}
void
StepDiagForward (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
int ft, rt = rf + 1;
if (rt >= BOARD_HEIGHT) return;
ft = ff + 1;
if (!(rt >= BOARD_HEIGHT || ft >= BOARD_RGHT) && !SameColor(board[rf][ff], board[rt][ft]))
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
ft = ff - 1;
if (!(rt >= BOARD_HEIGHT || ft < BOARD_LEFT) && !SameColor(board[rf][ff], board[rt][ft]))
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
}
void
StepDiagBackward (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
int ft, rt = rf - 1;
if(rt < 0) return;
ft = ff + 1;
if (!(rt < 0 || ft >= BOARD_RGHT) && !SameColor(board[rf][ff], board[rt][ft]))
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
ft = ff - 1;
if (!(rt < 0 || ft < BOARD_LEFT) && !SameColor(board[rf][ff], board[rt][ft]))
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
}
void
StepVertical (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
StepForward(board, flags, rf, ff, callback, closure);
StepBackward(board, flags, rf, ff, callback, closure);
}
void
Ferz (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
StepDiagForward(board, flags, rf, ff, callback, closure);
StepDiagBackward(board, flags, rf, ff, callback, closure);
}
void
Wazir (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
StepVertical(board, flags, rf, ff, callback, closure);
StepSideways(board, flags, rf, ff, callback, closure);
}
void
Knight (Board board, int flags, int rf, int ff, MoveCallback callback, VOIDSTAR closure)
{
int i, j, s, rt, ft;
for (i = -1; i <= 1; i += 2)
for (j = -1; j <= 1; j += 2)
for (s = 1; s <= 2; s++) {
rt = rf + i*s;
ft = ff + j*(3-s);
if (!(rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT)
&& ( gameInfo.variant != VariantXiangqi || board[rf+i*(s-1)][ff+j*(2-s)] == EmptySquare)
&& !SameColor(board[rf][ff], board[rt][ft]))
callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
}
}
/* Call callback once for each pseudo-legal move in the given
position, except castling moves. A move is pseudo-legal if it is
legal, or if it would be legal except that it leaves the king in
check. In the arguments, epfile is EP_NONE if the previous move
was not a double pawn push, or the file 0..7 if it was, or
EP_UNKNOWN if we don't know and want to allow all e.p. captures.
Promotion moves generated are to Queen only.
*/
void
GenPseudoLegal (Board board, int flags, MoveCallback callback, VOIDSTAR closure, ChessSquare filter)
// speed: only do moves with this piece type
{
int rf, ff;
int i, j, d, s, fs, rs, rt, ft, m;
int epfile = (signed char)board[EP_STATUS]; // [HGM] gamestate: extract ep status from board
int promoRank = gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand || gameInfo.variant == VariantChuChess ? 3 : 1;
for (rf = 0; rf < BOARD_HEIGHT; rf++)
for (ff = BOARD_LEFT; ff < BOARD_RGHT; ff++) {
ChessSquare piece;
if(board[rf][ff] == EmptySquare) continue;
if ((flags & F_WHITE_ON_MOVE) != (board[rf][ff] < BlackPawn)) continue; // [HGM] speed: wrong color
m = 0; piece = board[rf][ff];
if(PieceToChar(piece) == '~')
piece = (ChessSquare) ( DEMOTED(piece) );
if(filter != EmptySquare && piece != filter) continue;
if(pieceDefs && pieceDesc[piece]) { // [HGM] gen: use engine-defined moves
MovesFromString(board, flags, ff, rf, -1, -1, 0, 0, pieceDesc[piece], callback, closure);
continue;
}
if(IS_SHOGI(gameInfo.variant))
piece = (ChessSquare) ( SHOGI piece );
switch ((int)piece) {
/* case EmptySquare: [HGM] this is nonsense, and conflicts with Shogi cases */
default:
/* can't happen ([HGM] except for faries...) */
break;
case WhitePawn:
if(gameInfo.variant == VariantXiangqi) {
/* [HGM] capture and move straight ahead in Xiangqi */
if (rf < BOARD_HEIGHT-1 &&
!SameColor(board[rf][ff], board[rf + 1][ff]) ) {
callback(board, flags, NormalMove,
rf, ff, rf + 1, ff, closure);
}
/* and move sideways when across the river */
for (s = -1; s <= 1; s += 2) {
if (rf >= BOARD_HEIGHT>>1 &&
ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
!WhitePiece(board[rf][ff+s]) ) {
callback(board, flags, NormalMove,
rf, ff, rf, ff+s, closure);
}
}
break;
}
if (rf < BOARD_HEIGHT-1 && board[rf + 1][ff] == EmptySquare) {
callback(board, flags,
rf >= BOARD_HEIGHT-1-promoRank ? WhitePromotion : NormalMove,
rf, ff, rf + 1, ff, closure);
}
if (rf <= (BOARD_HEIGHT>>1)-3 && board[rf+1][ff] == EmptySquare && // [HGM] grand: also on 3rd rank on 10-board
gameInfo.variant != VariantShatranj && /* [HGM] */
gameInfo.variant != VariantCourier && /* [HGM] */
board[rf+2][ff] == EmptySquare ) {
callback(board, flags, NormalMove,
rf, ff, rf+2, ff, closure);
}
for (s = -1; s <= 1; s += 2) {
if (rf < BOARD_HEIGHT-1 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
((flags & F_KRIEGSPIEL_CAPTURE) ||
BlackPiece(board[rf + 1][ff + s]))) {
callback(board, flags,
rf >= BOARD_HEIGHT-1-promoRank ? WhitePromotion : NormalMove,
rf, ff, rf + 1, ff + s, closure);
}
if (rf >= BOARD_HEIGHT+1>>1) {// [HGM] grand: 4th & 5th rank on 10-board
if (ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
(epfile == ff + s || epfile == EP_UNKNOWN) && rf < BOARD_HEIGHT-3 &&
board[rf][ff + s] == BlackPawn &&
board[rf+1][ff + s] == EmptySquare) {
callback(board, flags, WhiteCapturesEnPassant,
rf, ff, rf+1, ff + s, closure);
}
}
}
break;
case BlackPawn:
if(gameInfo.variant == VariantXiangqi) {
/* [HGM] capture straight ahead in Xiangqi */
if (rf > 0 && !SameColor(board[rf][ff], board[rf - 1][ff]) ) {
callback(board, flags, NormalMove,
rf, ff, rf - 1, ff, closure);
}
/* and move sideways when across the river */
for (s = -1; s <= 1; s += 2) {
if (rf < BOARD_HEIGHT>>1 &&
ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
!BlackPiece(board[rf][ff+s]) ) {
callback(board, flags, NormalMove,
rf, ff, rf, ff+s, closure);
}
}
break;
}
if (rf > 0 && board[rf - 1][ff] == EmptySquare) {
callback(board, flags,
rf <= promoRank ? BlackPromotion : NormalMove,
rf, ff, rf - 1, ff, closure);
}
if (rf >= (BOARD_HEIGHT+1>>1)+2 && board[rf-1][ff] == EmptySquare && // [HGM] grand
gameInfo.variant != VariantShatranj && /* [HGM] */
gameInfo.variant != VariantCourier && /* [HGM] */
board[rf-2][ff] == EmptySquare) {
callback(board, flags, NormalMove,
rf, ff, rf-2, ff, closure);
}
for (s = -1; s <= 1; s += 2) {
if (rf > 0 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
((flags & F_KRIEGSPIEL_CAPTURE) ||
WhitePiece(board[rf - 1][ff + s]))) {
callback(board, flags,
rf <= promoRank ? BlackPromotion : NormalMove,
rf, ff, rf - 1, ff + s, closure);
}
if (rf < BOARD_HEIGHT>>1) {
if (ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
(epfile == ff + s || epfile == EP_UNKNOWN) && rf > 2 &&
board[rf][ff + s] == WhitePawn &&
board[rf-1][ff + s] == EmptySquare) {
callback(board, flags, BlackCapturesEnPassant,
rf, ff, rf-1, ff + s, closure);
}
}
}
break;
case WhiteUnicorn:
case BlackUnicorn:
case WhiteKnight:
case BlackKnight:
for (i = -1; i <= 1; i += 2)
for (j = -1; j <= 1; j += 2)
for (s = 1; s <= 2; s++) {
rt = rf + i*s;
ft = ff + j*(3-s);
if (!(rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT)
&& ( gameInfo.variant != VariantXiangqi || board[rf+i*(s-1)][ff+j*(2-s)] == EmptySquare)
&& !SameColor(board[rf][ff], board[rt][ft]))
callback(board, flags, NormalMove,
rf, ff, rt, ft, closure);
}
break;
case SHOGI WhiteKnight:
for (s = -1; s <= 1; s += 2) {
if (rf < BOARD_HEIGHT-2 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
!SameColor(board[rf][ff], board[rf + 2][ff + s])) {
callback(board, flags, NormalMove,
rf, ff, rf + 2, ff + s, closure);
}
}
break;
case SHOGI BlackKnight:
for (s = -1; s <= 1; s += 2) {
if (rf > 1 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
!SameColor(board[rf][ff], board[rf - 2][ff + s])) {
callback(board, flags, NormalMove,
rf, ff, rf - 2, ff + s, closure);
}
}
break;
case WhiteCannon:
case BlackCannon:
for (d = 0; d <= 1; d++)
for (s = -1; s <= 1; s += 2) {
m = 0;
for (i = 1;; i++) {
rt = rf + (i * s) * d;
ft = ff + (i * s) * (1 - d);
if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
if (m == 0 && board[rt][ft] == EmptySquare)
callback(board, flags, NormalMove,
rf, ff, rt, ft, closure);
if (m == 1 && board[rt][ft] != EmptySquare &&
!SameColor(board[rf][ff], board[rt][ft]) )
callback(board, flags, NormalMove,
rf, ff, rt, ft, closure);
if (board[rt][ft] != EmptySquare && m++) break;
}
}
break;
/* Gold General (and all its promoted versions) . First do the */
/* diagonal forward steps, then proceed as normal Wazir */
case SHOGI (PROMO WhitePawn):
if(gameInfo.variant == VariantShogi) goto WhiteGold;
case SHOGI (PROMO BlackPawn):
if(gameInfo.variant == VariantShogi) goto BlackGold;
case SHOGI WhiteAxe:
case SHOGI BlackAxe:
SlideVertical(board, flags, rf, ff, callback, closure);
break;
case SHOGI (PROMO WhiteKnight):
if(gameInfo.variant == VariantShogi) goto WhiteGold;
case SHOGI WhiteClaw:
case SHOGI BlackDrunk:
case SHOGI BlackAlfil:
Ferz(board, flags, rf, ff, callback, closure);
StepSideways(board, flags, rf, ff, callback, closure);
StepBackward(board, flags, rf, ff, callback, closure);
break;
case SHOGI (PROMO BlackKnight):
if(gameInfo.variant == VariantShogi) goto BlackGold;
case SHOGI BlackClaw:
case SHOGI WhiteDrunk:
case SHOGI WhiteAlfil:
Ferz(board, flags, rf, ff, callback, closure);
StepSideways(board, flags, rf, ff, callback, closure);
StepForward(board, flags, rf, ff, callback, closure);
break;
case SHOGI WhiteGnu:
case SHOGI BlackGnu:
if(gameInfo.variant == VariantShogi) goto BlackGold;
SlideVertical(board, flags, rf, ff, callback, closure);
Ferz(board, flags, rf, ff, callback, closure);
StepSideways(board, flags, rf, ff, callback, closure);
break;
case SHOGI (PROMO WhiteQueen):
case SHOGI WhiteTokin:
case SHOGI WhiteWazir:
WhiteGold:
StepDiagForward(board, flags, rf, ff, callback, closure);
Wazir(board, flags, rf, ff, callback, closure);
break;
case SHOGI (PROMO BlackQueen):
case SHOGI BlackTokin:
case SHOGI BlackWazir:
BlackGold:
StepDiagBackward(board, flags, rf, ff, callback, closure);
Wazir(board, flags, rf, ff, callback, closure);
break;
case WhiteWazir:
case BlackWazir:
if(gameInfo.variant == VariantXiangqi) {
int palace = (piece == WhiteWazir ? 1 : BOARD_HEIGHT-2); // Palace center
if(ff <= BOARD_WIDTH/2 && !SameColor(board[rf][ff+1], piece)) callback(board, flags, NormalMove, rf, ff, rf, ff+1, closure);
if(ff >= BOARD_WIDTH/2 && !SameColor(board[rf][ff-1], piece)) callback(board, flags, NormalMove, rf, ff, rf, ff-1, closure);
if(rf >= palace && !SameColor(board[rf-1][ff], piece)) callback(board, flags, NormalMove, rf, ff, rf-1, ff, closure);
if(rf <= palace && !SameColor(board[rf+1][ff], piece)) callback(board, flags, NormalMove, rf, ff, rf+1, ff, closure);
break;
}
Wazir(board, flags, rf, ff, callback, closure);
break;
case SHOGI WhiteMarshall: