Debug Seirawan Chess
[capablanca.git] / lasker-2.2.3 / src / movecheck.c
1 /*
2    Copyright (c) 1993 Richard V. Nash.
3    Copyright (c) 2000 Dan Papasian
4    Copyright (C) Andrew Tridgell 2002
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 // [HGM] some explanation of the parser code:
24 // The routine parse_move() calls is_move() to recognize some simple cases,
25 // like OO-castle and long algebraic with or without dash. What does not fall
26 // in this class is handed to alg_is_move(), whch is really a continuation
27 // of is_move(), to recognize SAN.
28 // Depending on the type of move syntax, parse_move can extract from- and to-square
29 // immediately, or transate the OO-castling to internal from-to representation.
30 // Only for SAN syntax the routine alg_pars_move() is called to extract the
31 // given elements, (through get_move_info(), which is the same as alg_is_move(),
32 // xcept that it does not discard the value of the elements), and disambiguate 
33 // the move (i.e. determines the from-square) by looking for a piece of the given
34 // type on the board for which the move is pseudo-legal (using legal_move() ).
35 // Th
36
37 /* Simply tests if the input string is a move or not. */
38 /* If it matches patterns below */
39 /* Add to this list as you improve the move parser */
40 /* MS_COMP e2e4 */
41 /* MS_COMPDASH e2-e4 */
42 /* MS_CASTLE o-o, o-o-o */
43 /* Not done yet */
44 /* MS_ALG e4, Nd5 Ncd5 */
45 int is_move(const char *mstr)
46 {
47   int len = strlen(mstr);
48   if ((len > 3) && (mstr[len - 2] == '='))
49     len -= 2;
50
51   /* remove the 'mates' marker */
52   if (mstr[len - 1] == '#')
53     len--;
54
55   if (len == 4) {               /* Test for e2e4 */
56     if (isfile(mstr[0]) && isrank(mstr[1]) &&
57         isfile(mstr[2]) && isrank(mstr[3])) {
58       return MS_COMP;
59     }
60   }
61   if (len == 5) {               /* Test for e2-e4 */
62     if (isfile(mstr[0]) && isrank(mstr[1]) &&
63         (mstr[2] == '-') &&
64         isfile(mstr[3]) && isrank(mstr[4])) {
65       return MS_COMPDASH;
66     }
67   }
68   if (len == 3) {               /* Test for o-o */
69     if ((mstr[0] == 'o') && (mstr[1] == '-') && (mstr[2] == 'o')) {
70       return MS_KCASTLE;
71     }
72     if ((mstr[0] == 'O') && (mstr[1] == '-') && (mstr[2] == 'O')) {
73       return MS_KCASTLE;
74     }
75     if ((mstr[0] == '0') && (mstr[1] == '-') && (mstr[2] == '0')) {
76       return MS_KCASTLE;
77     }
78   }
79   if (len == 2) {               /* Test for oo */
80     if ((mstr[0] == 'o') && (mstr[1] == 'o')) {
81       return MS_KCASTLE;
82     }
83     if ((mstr[0] == 'O') && (mstr[1] == 'O')) {
84       return MS_KCASTLE;
85     }
86     if ((mstr[0] == '0') && (mstr[1] == '0')) {
87       return MS_KCASTLE;
88     }
89   }
90   if (len == 5) {               /* Test for o-o-o */
91     if ((mstr[0] == 'o') && (mstr[1] == '-') && (mstr[2] == 'o') && (mstr[3] == '-') && (mstr[4] == 'o')) {
92       return MS_QCASTLE;
93     }
94     if ((mstr[0] == 'O') && (mstr[1] == '-') && (mstr[2] == 'O') && (mstr[3] == '-') && (mstr[4] == 'O')) {
95       return MS_QCASTLE;
96     }
97     if ((mstr[0] == '0') && (mstr[1] == '-') && (mstr[2] == '0') && (mstr[3] == '-') && (mstr[4] == '0')) {
98       return MS_QCASTLE;
99     }
100   }
101   if (len == 3) {               /* Test for ooo */
102     if ((mstr[0] == 'o') && (mstr[1] == 'o') && (mstr[2] == 'o')) {
103       return MS_QCASTLE;
104     }
105     if ((mstr[0] == 'O') && (mstr[1] == 'O') && (mstr[2] == 'O')) {
106       return MS_QCASTLE;
107     }
108     if ((mstr[0] == '0') && (mstr[1] == '0') && (mstr[2] == '0')) {
109       return MS_QCASTLE;
110     }
111   }
112   return alg_is_move(mstr);
113 }
114
115
116 int NextPieceLoop(board_t b, int *f, int *r, int color, int w, int h)
117 {
118   for (;;) {
119     (*r) = (*r) + 1;
120     if (*r >= h) {
121       *r = 0;
122       *f = *f + 1;
123       if (*f >= w)
124         break;
125     }
126     if ((b[*f][*r] != NOPIECE) && iscolor(b[*f][*r], color))
127       return 1;
128   }
129   return 0;
130 }
131
132 int InitPieceLoop(board_t b, int *f, int *r, int color)
133 {
134   *f = 0;
135   *r = -1;
136   return 1;
137 }
138
139 /* All of the routines assume that the obvious problems have been checked */
140 /* See legal_move() */
141 static int legal_pawn_move( struct game_state_t *gs, int ff, int fr, int tf, int tr )
142 {
143   if (ff == tf) {
144     if (gs->board[tf][tr] != NOPIECE && !gs->palace && gs->promoType != 3) return 0; // [HGM] XQ and Shogi pawns can capture straight ahead
145     if (gs->onMove == WHITE) {
146       if (tr - fr == 1) return 1;
147       if ((fr <= gs->pawnDblStep) && (tr - fr == 2) && gs->board[ff][fr+1]==NOPIECE) return 1;
148     } else {
149       if (fr - tr == 1) return 1;
150       if ((fr >= gs->ranks - 1 - gs->pawnDblStep) && (fr - tr == 2) && gs->board[ff][fr-1]==NOPIECE) return 1;
151     }
152     return 0;
153   }
154   if (ff != tf && gs->promoType != 3) { /* Capture ? ([HGM] but not in Shogi) */
155     if ((ff - tf != 1) && (tf - ff != 1)) return 0;
156     if (gs->onMove == WHITE) {
157       if(gs->palace) return (fr >= gs->ranks/2 && fr == tr); // [HGM] XQ promoted pawns
158       if (tr != fr+1) return 0;
159       if ((gs->board[tf][tr] != NOPIECE) && iscolor(gs->board[tf][tr],BLACK))
160         return 1;
161       if (gs->ep_possible[0][ff] == 1) {
162         if ((tf==ff+1) && (gs->board[ff+1][fr] == B_PAWN)) return 1;
163       } else if (gs->ep_possible[0][ff] == -1) {
164         if ((tf==ff-1) && (gs->board[ff-1][fr] == B_PAWN)) return 1;
165       }
166     } else {
167       if(gs->palace) return (fr < gs->ranks/2 && fr == tr); // [HGM] XQ promoted pawns
168       if (tr != fr-1) return 0;
169       if ((gs->board[tf][tr] != NOPIECE) && iscolor(gs->board[tf][tr],WHITE))
170         return 1;
171       if (gs->ep_possible[1][ff] == 1) {
172         if ((tf==ff+1) && (gs->board[ff+1][fr] == W_PAWN)) return 1;
173       } else if (gs->ep_possible[1][ff] == -1) {
174         if ((tf==ff-1) && (gs->board[ff-1][fr] == W_PAWN)) return 1;
175       }
176     }
177   }
178   return 0;
179 }
180
181 static int legal_knight_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
182 {
183   int dx, dy;
184
185   dx = ff - tf;
186   dy = fr - tr;
187   if (abs(dx) == 2) {
188     if (abs(dy) == 1)
189       return 1;
190   }
191   if (abs(dy) == 2) {
192     if (abs(dx) == 1)
193       return 1;
194   }
195   return 0;
196 }
197
198 static int legal_horse_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
199 {
200   int dx, dy;
201
202   dx = ff - tf;
203   dy = fr - tr;
204   if (abs(dx) == 2) {
205     if (abs(dy) == 1 && gs->board[(ff+tf)/2][fr] == NOPIECE)
206       return 1;
207   }
208   if (abs(dy) == 2) {
209     if (abs(dx) == 1 && gs->board[ff][(fr+tr)/2] == NOPIECE)
210       return 1;
211   }
212   return 0;
213 }
214
215 static int legal_honorablehorse_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
216 {
217   int dx, dy;
218
219   dx = ff - tf;
220   dy = fr - tr;
221   if (dy == (gs->onMove == WHITE ? -2 : 2)) {
222     if (abs(dx) == 1)
223       return 1;
224   }
225   return 0;
226 }
227
228 static int legal_bishop_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
229 {
230   int dx, dy, x, y;
231   int startx, starty;
232   int count;
233   int incx, incy;
234
235   if (ff > tf) {
236     dx = ff - tf;
237     incx = -1;
238   } else {
239     dx = tf - ff;
240     incx = 1;
241   }
242   startx = ff + incx;
243   if (fr > tr) {
244     dy = fr - tr;
245     incy = -1;
246   } else {
247     dy = tr - fr;
248     incy = 1;
249   }
250   starty = fr + incy;
251   if (dx != dy)
252     return 0;                   /* Not diagonal */
253   if (dx == 1)
254     return 1;                   /* One square, ok */
255   count = dx - 1;
256   for (x = startx, y = starty; count; x += incx, y += incy, count--) {
257     if (gs->board[x][y] != NOPIECE)
258       return 0;
259   }
260   return 1;
261 }
262
263 static int legal_rook_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
264 {
265   int i;
266   int start, stop;
267
268   if (ff == tf) {
269     if (abs(fr - tr) == 1)
270       return 1;
271     if (fr < tr) {
272       start = fr + 1;
273       stop = tr - 1;
274     } else {
275       start = tr + 1;
276       stop = fr - 1;
277     }
278     for (i = start; i <= stop; i++) {
279       if (gs->board[ff][i] != NOPIECE)
280         return 0;
281     }
282     return 1;
283   } else if (fr == tr) {
284     if (abs(ff - tf) == 1)
285       return 1;
286     if (ff < tf) {
287       start = ff + 1;
288       stop = tf - 1;
289     } else {
290       start = tf + 1;
291       stop = ff - 1;
292     }
293     for (i = start; i <= stop; i++) {
294       if (gs->board[i][fr] != NOPIECE)
295         return 0;
296     }
297     return 1;
298   } else {
299     return 0;
300   }
301 }
302
303 static int legal_cannon_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
304 {
305   int i, cnt=0;
306   int start, stop;
307
308   if (ff == tf) {
309     if (fr < tr) {
310       start = fr + 1;
311       stop = tr - 1;
312     } else {
313       start = tr + 1;
314       stop = fr - 1;
315     }
316     for (i = start; i <= stop; i++) {
317       if (gs->board[ff][i] != NOPIECE) cnt++;
318     }
319     return (cnt == 0 && gs->board[tf][tr] == NOPIECE) ||
320            (cnt == 1 && gs->board[tf][tr] != NOPIECE);
321   } else if (fr == tr) {
322     if (ff < tf) {
323       start = ff + 1;
324       stop = tf - 1;
325     } else {
326       start = tf + 1;
327       stop = ff - 1;
328     }
329     for (i = start; i <= stop; i++) {
330       if (gs->board[i][fr] != NOPIECE) cnt++;
331     }
332     return (cnt == 0 && gs->board[tf][tr] == NOPIECE) ||
333            (cnt == 1 && gs->board[tf][tr] != NOPIECE);
334   } else {
335     return 0;
336   }
337 }
338
339 static int legal_lance_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
340 {
341   int i;
342   int start, stop;
343
344   if (ff == tf) {
345     if (abs(fr - tr) == 1)
346       return 1;
347     if (fr < tr) {
348       if(gs->onMove != WHITE) return 0;
349       start = fr + 1;
350       stop = tr - 1;
351     } else {
352       if(gs->onMove == WHITE) return 0;
353       start = tr + 1;
354       stop = fr - 1;
355     }
356     for (i = start; i <= stop; i++) {
357       if (gs->board[ff][i] != NOPIECE)
358         return 0;
359     }
360     return 1;
361   }
362 }
363
364 static int legal_queen_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
365 {
366   return legal_rook_move(gs, ff, fr, tf, tr) || legal_bishop_move(gs, ff, fr, tf, tr);
367 }
368
369 static int legal_cardinal_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
370 {
371   return legal_knight_move(gs, ff, fr, tf, tr) || legal_bishop_move(gs, ff, fr, tf, tr);
372 }
373
374 static int legal_marshall_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
375 {
376   return legal_rook_move(gs, ff, fr, tf, tr) || legal_knight_move(gs, ff, fr, tf, tr);
377 }
378
379 /* Ckeck, if square (kf,kr) is attacked by enemy piece.
380  * Used in castling from/through check testing.
381  */
382
383 /* new one from soso: */
384 static int is_square_attacked (struct game_state_t *gs, int kf, int kr)
385 {
386   struct game_state_t fakeMove;
387   int oldk = gs->onMove == WHITE ? gs->wkmoved : gs->bkmoved;
388
389   fakeMove = *gs;
390   fakeMove.board[oldk][kr] = NOPIECE; // [HGM] castle: this routine is called only when King has not moved
391   fakeMove.board[kf][kr] = KING | fakeMove.onMove;
392   fakeMove.onMove = CToggle (fakeMove.onMove);
393   if (in_check(&fakeMove)) return 1;
394     else return 0;
395 }
396
397 /* old one:
398 static int is_square_attacked(struct game_state_t * gs, int kf, int kr)
399 {
400   int f, r;
401   gs->onMove = CToggle(gs->onMove);
402
403   for (InitPieceLoop(gs->board, &f, &r, gs->onMove);
404        NextPieceLoop(gs->board, &f, &r, gs->onMove, gs->files, gs->ranks);) {
405     if (legal_move(gs, f, r, kf, kr)) {
406       gs->onMove = CToggle(gs->onMove);
407       return 1;
408     }
409   }
410   gs->onMove = CToggle(gs->onMove);
411   return 0;
412 }
413 */
414
415 static int legal_man_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
416 {
417   if (abs(ff - tf) > 1)
418     return 0;
419   if (abs(fr - tr) > 1)
420     return 0;
421   return 1;
422 }
423
424 static int legal_wazir_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
425 {
426   if(gs->palace && (tr > gs->palace && tr < gs->ranks - gs->palace ||
427      tf < (gs->files - gs->palace)/2 || tf >= (gs->files + gs->palace)/2))
428     return 0;
429   if (abs(ff - tf) == 1 && fr == tr)
430     return 1;
431   if (abs(fr - tr) == 1 && ff == tf)
432     return 1;
433   return 0;
434 }
435
436 static int legal_dababba_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
437 {
438   if (abs(ff - tf) == 2 && fr == tr)
439     return 1;
440   if (abs(fr - tr) == 2 && ff == tf)
441     return 1;
442   return 0;
443 }
444
445 static int legal_ferz_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
446 {
447   if (abs(ff - tf) != 1)
448     return 0;
449   if (abs(fr - tr) != 1)
450     return 0;
451   return 1;
452 }
453
454 static int legal_mandarin_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
455 {
456   if(gs->palace && (tr > gs->palace && tr < gs->ranks - gs->palace ||
457      tf < (gs->files - gs->palace)/2 || tf >= (gs->files + gs->palace)/2))
458     return 0;
459   if (abs(ff - tf) != 1)
460     return 0;
461   if (abs(fr - tr) != 1)
462     return 0;
463   return 1;
464 }
465
466 static int legal_alfil_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
467 {
468   if (abs(ff - tf) != 2)
469     return 0;
470   if (abs(fr - tr) != 2)
471     return 0;
472   return 1;
473 }
474
475 static int legal_elephant_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
476 {
477   if (abs(ff - tf) != 2)
478     return 0;
479   if (abs(fr - tr) != 2)
480     return 0;
481   if(gs->board[(ff+tf)/2][(fr+tr)/2] != NOPIECE) return 0; // blocked
482   if((tr >= gs->ranks/2) != (fr >= gs->ranks/2)) return 0; // do not cross river
483   return 1;
484 }
485
486 static int legal_gold_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
487 {
488   return legal_wazir_move(gs, ff, fr, tf, tr) || (abs(ff-tf) == 1 && tr == fr + (gs->onMove==WHITE ? 1 : -1));
489 }
490
491 static int legal_silver_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
492 {
493   return legal_ferz_move(gs, ff, fr, tf, tr) || (tf == ff && tr == fr + (gs->onMove==WHITE ? 1 : -1) );
494 }
495
496 static int legal_woody_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
497 {
498   return legal_wazir_move(gs, ff, fr, tf, tr) || legal_dababba_move(gs, ff, fr, tf, tr);
499 }
500
501 static int legal_squirrel_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
502 {
503   return legal_alfil_move(gs, ff, fr, tf, tr) || legal_dababba_move(gs, ff, fr, tf, tr) 
504                                                         || legal_knight_move(gs, ff, fr, tf, tr);
505 }
506
507 static int legal_mastodon_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
508 {
509   return legal_man_move(gs, ff, fr, tf, tr) || legal_alfil_move(gs, ff, fr, tf, tr)
510                                                         || legal_dababba_move(gs, ff, fr, tf, tr);
511 }
512
513 static int legal_centaur_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
514 {
515   return legal_man_move(gs, ff, fr, tf, tr) || legal_knight_move(gs, ff, fr, tf, tr);
516 }
517
518 static int legal_amazon_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
519 {
520   return legal_queen_move(gs, ff, fr, tf, tr) || legal_knight_move(gs, ff, fr, tf, tr);
521 }
522
523 static int legal_dragonking_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
524 {
525   return legal_rook_move(gs, ff, fr, tf, tr) || legal_ferz_move(gs, ff, fr, tf, tr);
526 }
527
528 static int legal_dragonhorse_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
529 {
530   return legal_bishop_move(gs, ff, fr, tf, tr) || legal_wazir_move(gs, ff, fr, tf, tr);
531 }
532
533 static int legal_modernelephant_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
534 {
535   return legal_ferz_move(gs, ff, fr, tf, tr) || legal_alfil_move(gs, ff, fr, tf, tr);
536 }
537
538 static int legal_priestess_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
539 {
540   return legal_knight_move(gs, ff, fr, tf, tr) || legal_ferz_move(gs, ff, fr, tf, tr)
541                                                 || legal_alfil_move(gs, ff, fr, tf, tr);
542 }
543
544 static int legal_minister_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
545 {
546   return legal_knight_move(gs, ff, fr, tf, tr) || legal_wazir_move(gs, ff, fr, tf, tr)
547                                                 || legal_dababba_move(gs, ff, fr, tf, tr);
548 }
549
550 static int legal_king_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
551 {
552   int result;
553
554   // [HGM] castle: test first if valid as regular King move; result = 1 or 0
555   if(gs->royalKnight)
556     result = legal_knight_move(gs, ff, fr, tf, tr);
557   else if(gs->palace) {
558     result = legal_wazir_move(gs, ff, fr, tf, tr);
559     if(!result && ff == tf && piecetype(gs->board[tf][tr]) == KING) { // XQ regicide
560       int i, d = (tr>fr ? 1 : -1);
561       for(i=fr+d; i!=tr; i+=d) 
562         if(gs->board[ff][i] != NOPIECE) return 0; // line of sight blocked
563       return 1;
564     }
565   } else
566     result = legal_man_move(gs, ff, fr, tf, tr);
567
568   if(result) return 1;
569   // [HGM] castle: orthodox legal castlings given as King move return 2
570
571   if (gs->onMove == WHITE) {
572     /* King side castling */
573     if ((fr == 0) && (tr == 0) && (ff == gs->files/2) && (tf == gs->files-2) && (gs->wkmoved >= 0)
574         && (gs->wkrmoved >= 0) && (gs->board[gs->files-3][0] == NOPIECE) &&
575         (gs->board[gs->files-2][0] == NOPIECE) && (gs->board[gs->files-1][0] == W_ROOK) &&
576         (gs->board[gs->files/2+1][0] == NOPIECE) && (!is_square_attacked(gs, gs->files/2+1, 0)) &&
577         (!is_square_attacked(gs, gs->files/2, 0)) && (!is_square_attacked(gs, gs->files-3, 0))) {
578       return 2;
579     }
580     /* Queen side castling */
581     if ((fr == 0) && (tr == 0) && (ff == gs->files/2) && (tf == 2) && (gs->wkmoved >= 0)
582         && (gs->wqrmoved >= 0) && (gs->board[3][0] == NOPIECE) &&
583         (gs->board[2][0] == NOPIECE) && (gs->board[1][0] == NOPIECE) &&
584         (gs->board[0][0] == W_ROOK) &&
585         (gs->board[gs->files/2-1][0] == NOPIECE) && (!is_square_attacked(gs, gs->files/2-1, 0)) &&
586         (!is_square_attacked(gs, gs->files/2, 0)) && (!is_square_attacked(gs, 3, 0))) {
587       return 2;
588     }
589   } else {                      /* Black */
590     /* King side castling */
591     if ((fr == gs->ranks-1) && (tr == gs->ranks-1) && (ff == gs->files/2) && (tf == gs->files-2) && (gs->bkmoved >= 0)
592         && (gs->bkrmoved >= 0) && (gs->board[gs->files-3][7] == NOPIECE) &&
593         (gs->board[gs->files-2][gs->ranks-1] == NOPIECE) && (gs->board[gs->files-1][gs->ranks-1] == B_ROOK) &&
594         (gs->board[gs->files/2+1][gs->ranks-1] == NOPIECE) && (!is_square_attacked(gs, gs->files/2+1, gs->ranks-1)) &&
595         (!is_square_attacked(gs, gs->files/2, gs->ranks-1)) && (!is_square_attacked(gs, gs->files-3, gs->ranks-1))) {
596       return 2;
597     }
598     /* Queen side castling */
599     if ((fr == gs->ranks-1) && (tr == gs->ranks-1) && (ff == gs->files/2) && (tf == 2) && (gs->bkmoved >= 0)
600         && (gs->bqrmoved >= 0) && (gs->board[3][gs->ranks-1] == NOPIECE) &&
601         (gs->board[2][gs->ranks-1] == NOPIECE) && (gs->board[1][gs->ranks-1] == NOPIECE) &&
602         (gs->board[0][gs->ranks-1] == B_ROOK) &&
603         (gs->board[gs->files/2-1][gs->ranks-1] == NOPIECE) && (!is_square_attacked(gs, gs->files/2-1, gs->ranks-1)) &&
604         (!is_square_attacked(gs, gs->files/2, gs->ranks-1)) && (!is_square_attacked(gs, 3, gs->ranks-1))) {
605       return 2;
606     }
607   }
608
609   return 0; // neither regular King move nor castling
610 }
611
612 static void add_pos(int tof, int tor, int *posf, int *posr, int *numpos)
613 {
614   posf[*numpos] = tof;
615   posr[*numpos] = tor;
616   (*numpos)++;
617 }
618
619 static void possible_pawn_moves(struct game_state_t * gs,
620                                   int onf, int onr,
621                                   int *posf, int *posr, int *numpos)
622 {
623   if (gs->onMove == WHITE) {
624     if (gs->board[onf][onr + 1] == NOPIECE || gs->palace || gs->promoType == 3) {
625       add_pos(onf, onr + 1, posf, posr, numpos);
626       if ((onr <= gs->pawnDblStep) && (gs->board[onf][onr + 2] == NOPIECE))
627         add_pos(onf, onr + 2, posf, posr, numpos);
628     }
629     if (onf > 0) {
630       if (gs->board[onf - 1][onr + 1] != NOPIECE &&
631           iscolor(gs->board[onf - 1][onr + 1], BLACK) &&
632           !gs->palace && gs->promoType != 3) // no diagonal capture in XQ and Shogi
633         add_pos(onf - 1, onr + 1, posf, posr, numpos);
634       if(gs->palace && onr >= gs->ranks/2 && (gs->board[onf-1][onr] || iscolor(gs->board[onf-1][onr], BLACK)))
635         add_pos(onf - 1, onr, posf, posr, numpos); // XQ promoted pawn
636     }
637     if (onf < gs->files-1) {
638       if (gs->board[onf + 1][onr + 1] != NOPIECE &&
639           iscolor(gs->board[onf + 1][onr + 1], BLACK) &&
640           !gs->palace && gs->promoType != 3) // no diagonal capture in XQ and Shogi
641         add_pos(onf + 1, onr + 1, posf, posr, numpos);
642       if(gs->palace && onr >= gs->ranks/2 && (gs->board[onf+1][onr] || iscolor(gs->board[onf+1][onr], BLACK)))
643         add_pos(onf + 1, onr, posf, posr, numpos); // XQ promoted pawn
644     }
645     if (gs->ep_possible[0][onf] == -1)
646       add_pos(onf - 1, onr + 1, posf, posr, numpos);
647     if (gs->ep_possible[0][onf] == 1)
648       add_pos(onf + 1, onr + 1, posf, posr, numpos);
649   } else {
650     if (gs->board[onf][onr - 1] == NOPIECE || gs->palace || gs->promoType == 3) {
651       add_pos(onf, onr - 1, posf, posr, numpos);
652       if ((onr >= gs->ranks - gs->pawnDblStep - 1) && (gs->board[onf][onr - 2] == NOPIECE))
653         add_pos(onf, onr - 2, posf, posr, numpos);
654     }
655     if (onf > 0) {
656       if (gs->board[onf - 1][onr - 1] != NOPIECE &&
657           iscolor(gs->board[onf - 1][onr - 1], WHITE) &&
658           !gs->palace && gs->promoType != 3) // no diagonal capture in XQ and Shogi
659         add_pos(onf - 1, onr - 1, posf, posr, numpos);
660       if(gs->palace && onr < gs->ranks/2 && !iscolor(gs->board[onf-1][onr], BLACK))
661         add_pos(onf - 1, onr, posf, posr, numpos); // XQ promoted pawn
662     }
663     if (onf < gs->files-1) {
664       if (gs->board[onf + 1][onr - 1] != NOPIECE &&
665           iscolor(gs->board[onf + 1][onr - 1], WHITE) &&
666           !gs->palace && gs->promoType != 3) // no diagonal capture in XQ and Shogi
667         add_pos(onf + 1, onr - 1, posf, posr, numpos);
668       if(gs->palace && onr < gs->ranks/2 && !iscolor(gs->board[onf+1][onr], BLACK))
669         add_pos(onf + 1, onr, posf, posr, numpos); // XQ promoted pawn
670     }
671     if (gs->ep_possible[1][onf] == -1)
672       add_pos(onf - 1, onr - 1, posf, posr, numpos);
673     if (gs->ep_possible[1][onf] == 1)
674       add_pos(onf + 1, onr - 1, posf, posr, numpos);
675   }
676 }
677
678 static void possible_knight_moves(struct game_state_t * gs,
679                                     int onf, int onr,
680                                     int *posf, int *posr, int *numpos)
681 {
682   static int knightJumps[8][2] = {{-1, 2}, {1, 2}, {2, -1}, {2, 1},
683   {-1, -2}, {1, -2}, {-2, 1}, {-2, -1}};
684   int f, r;
685   int j;
686
687   for (j = 0; j < 8; j++) {
688     f = knightJumps[j][0] + onf;
689     r = knightJumps[j][1] + onr;
690     if ((f < 0) || (f >= gs->files))
691       continue;
692     if ((r < 0) || (r >= gs->ranks))
693       continue;
694     if ((gs->board[f][r] == NOPIECE) ||
695         (iscolor(gs->board[f][r], CToggle(gs->onMove))))
696       add_pos(f, r, posf, posr, numpos);
697   }
698 }
699
700 static void possible_horse_moves(struct game_state_t * gs,
701                                     int onf, int onr,
702                                     int *posf, int *posr, int *numpos)
703 {
704   static int knightJumps[8][4] = {{-1, 2, 0, 1}, {1, 2, 0, 1}, {2, -1, 1, 0}, {2, 1, 1, 0},
705   {-1, -2, 0, -1}, {1, -2, 0, -1}, {-2, 1, -1, 0}, {-2, -1, -1, 0}};
706   int f, r;
707   int j;
708
709   for (j = 0; j < 8; j++) {
710     f = knightJumps[j][0] + onf;
711     r = knightJumps[j][1] + onr;
712     if ((f < 0) || (f >= gs->files))
713       continue;
714     if ((r < 0) || (r >= gs->ranks))
715       continue;
716     if ((gs->board[knightJumps[j][2] + onf][knightJumps[j][3] + onr] == NOPIECE) && 
717         ((gs->board[f][r] == NOPIECE) || (iscolor(gs->board[f][r], CToggle(gs->onMove)))))
718       add_pos(f, r, posf, posr, numpos);
719   }
720 }
721
722 static void possible_bishop_moves(struct game_state_t * gs,
723                                     int onf, int onr,
724                                     int *posf, int *posr, int *numpos)
725 {
726   int f, r;
727
728   /* Up Left */
729   f = onf;
730   r = onr;
731   for (;;) {
732     f--;
733     r++;
734     if ((f < 0) || (f >= gs->files))
735       break;
736     if ((r < 0) || (r >= gs->ranks))
737       break;
738     if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
739       break;
740     add_pos(f, r, posf, posr, numpos);
741     if (gs->board[f][r] != NOPIECE)
742       break;
743   }
744   /* Up Right */
745   f = onf;
746   r = onr;
747   for (;;) {
748     f++;
749     r++;
750     if ((f < 0) || (f >= gs->files))
751       break;
752     if ((r < 0) || (r >= gs->ranks))
753       break;
754     if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
755       break;
756     add_pos(f, r, posf, posr, numpos);
757     if (gs->board[f][r] != NOPIECE)
758       break;
759   }
760   /* Down Left */
761   f = onf;
762   r = onr;
763   for (;;) {
764     f--;
765     r--;
766     if ((f < 0) || (f >= gs->files))
767       break;
768     if ((r < 0) || (r >= gs->ranks))
769       break;
770     if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
771       break;
772     add_pos(f, r, posf, posr, numpos);
773     if (gs->board[f][r] != NOPIECE)
774       break;
775   }
776   /* Down Right */
777   f = onf;
778   r = onr;
779   for (;;) {
780     f++;
781     r--;
782     if ((f < 0) || (f >= gs->files))
783       break;
784     if ((r < 0) || (r >= gs->ranks))
785       break;
786     if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
787       break;
788     add_pos(f, r, posf, posr, numpos);
789     if (gs->board[f][r] != NOPIECE)
790       break;
791   }
792 }
793
794 static void possible_rook_moves(struct game_state_t * gs,
795                                   int onf, int onr,
796                                   int *posf, int *posr, int *numpos)
797 {
798   int f, r;
799
800   /* Left */
801   f = onf;
802   r = onr;
803   for (;;) {
804     f--;
805     if ((f < 0) || (f >= gs->files))
806       break;
807     if ((r < 0) || (r >= gs->ranks))
808       break;
809     if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
810       break;
811     add_pos(f, r, posf, posr, numpos);
812     if (gs->board[f][r] != NOPIECE)
813       break;
814   }
815   /* Right */
816   f = onf;
817   r = onr;
818   for (;;) {
819     f++;
820     if ((f < 0) || (f >= gs->files))
821       break;
822     if ((r < 0) || (r >= gs->ranks))
823       break;
824     if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
825       break;
826     add_pos(f, r, posf, posr, numpos);
827     if (gs->board[f][r] != NOPIECE)
828       break;
829   }
830   /* Up */
831   f = onf;
832   r = onr;
833   for (;;) {
834     r++;
835     if ((f < 0) || (f >= gs->files))
836       break;
837     if ((r < 0) || (r >= gs->ranks))
838       break;
839     if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
840       break;
841     add_pos(f, r, posf, posr, numpos);
842     if (gs->board[f][r] != NOPIECE)
843       break;
844   }
845   /* Down */
846   f = onf;
847   r = onr;
848   for (;;) {
849     r--;
850     if ((f < 0) || (f >= gs->files))
851       break;
852     if ((r < 0) || (r >= gs->ranks))
853       break;
854     if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
855       break;
856     add_pos(f, r, posf, posr, numpos);
857     if (gs->board[f][r] != NOPIECE)
858       break;
859   }
860 }
861
862 static void possible_cannon_moves(struct game_state_t * gs,
863                                   int onf, int onr,
864                                   int *posf, int *posr, int *numpos)
865 {
866   int f, r, i;
867
868   /* Left */
869   f = onf;
870   r = onr;
871   for (i=0;;) {
872     f--;
873     if ((f < 0) || (f >= gs->files))
874       break;
875     if ((r < 0) || (r >= gs->ranks))
876       break;
877     if ((gs->board[f][r] != NOPIECE) && i++ == 0) continue;
878     if(i == 0)
879         add_pos(f, r, posf, posr, numpos); // no hop: non-capt
880     else if(i == 2 && !iscolor(gs->board[f][r], gs->onMove)) 
881         add_pos(f, r, posf, posr, numpos); // hop: capt
882     if (gs->board[f][r] != NOPIECE)
883       break;
884   }
885   /* Right */
886   f = onf;
887   r = onr;
888   for (i=0;;) {
889     f++;
890     if ((f < 0) || (f >= gs->files))
891       break;
892     if ((r < 0) || (r >= gs->ranks))
893       break;
894     if ((gs->board[f][r] != NOPIECE) && i++ == 0) continue;
895     if(i == 0)
896         add_pos(f, r, posf, posr, numpos); // no hop: non-capt
897     else if(i == 2 && !iscolor(gs->board[f][r], gs->onMove)) 
898         add_pos(f, r, posf, posr, numpos); // hop: capt
899     if (gs->board[f][r] != NOPIECE)
900       break;
901   }
902   /* Up */
903   f = onf;
904   r = onr;
905   for (i=0;;) {
906     r++;
907     if ((f < 0) || (f >= gs->files))
908       break;
909     if ((r < 0) || (r >= gs->ranks))
910       break;
911     if ((gs->board[f][r] != NOPIECE) && i++ == 0) continue;
912     if(i == 0)
913         add_pos(f, r, posf, posr, numpos); // no hop: non-capt
914     else if(i == 2 && !iscolor(gs->board[f][r], gs->onMove)) 
915         add_pos(f, r, posf, posr, numpos); // hop: capt
916     if (gs->board[f][r] != NOPIECE)
917       break;
918   }
919   /* Down */
920   f = onf;
921   r = onr;
922   for (i=0;;) {
923     r--;
924     if ((f < 0) || (f >= gs->files))
925       break;
926     if ((r < 0) || (r >= gs->ranks))
927       break;
928     if ((gs->board[f][r] != NOPIECE) && i++ == 0) continue;
929     if(i == 0)
930         add_pos(f, r, posf, posr, numpos); // no hop: non-capt
931     else if(i == 2 && !iscolor(gs->board[f][r], gs->onMove)) 
932         add_pos(f, r, posf, posr, numpos); // hop: capt
933     if (gs->board[f][r] != NOPIECE)
934       break;
935   }
936 }
937
938 static void possible_lance_moves(struct game_state_t * gs,
939                                   int onf, int onr,
940                                   int *posf, int *posr, int *numpos)
941 {
942   int f, r;
943
944   /* Up */
945   f = onf;
946   r = onr;
947   for (;gs->onMove == WHITE;) {
948     r++;
949     if ((f < 0) || (f >= gs->files))
950       break;
951     if ((r < 0) || (r >= gs->ranks))
952       break;
953     if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
954       break;
955     add_pos(f, r, posf, posr, numpos);
956     if (gs->board[f][r] != NOPIECE)
957       break;
958   }
959   /* Down */
960   f = onf;
961   r = onr;
962   for (;gs->onMove == BLACK;) {
963     r--;
964     if ((f < 0) || (f >= gs->files))
965       break;
966     if ((r < 0) || (r >= gs->ranks))
967       break;
968     if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
969       break;
970     add_pos(f, r, posf, posr, numpos);
971     if (gs->board[f][r] != NOPIECE)
972       break;
973   }
974 }
975
976 static void possible_cardinal_moves(struct game_state_t * gs,
977                                    int onf, int onr,
978                                    int *posf, int *posr, int *numpos)
979 {
980   possible_knight_moves(gs, onf, onr, posf, posr, numpos);
981   possible_bishop_moves(gs, onf, onr, posf, posr, numpos);
982 }
983
984 static void possible_marshall_moves(struct game_state_t * gs,
985                                    int onf, int onr,
986                                    int *posf, int *posr, int *numpos)
987 {
988   possible_rook_moves(gs, onf, onr, posf, posr, numpos);
989   possible_knight_moves(gs, onf, onr, posf, posr, numpos);
990 }
991
992 static void possible_queen_moves(struct game_state_t * gs,
993                                    int onf, int onr,
994                                    int *posf, int *posr, int *numpos)
995 {
996   possible_rook_moves(gs, onf, onr, posf, posr, numpos);
997   possible_bishop_moves(gs, onf, onr, posf, posr, numpos);
998 }
999
1000 static void possible_alfil_moves(struct game_state_t * gs,
1001                                   int onf, int onr,
1002                                   int *posf, int *posr, int *numpos)
1003 {
1004   static int kingJumps[4][2] = {{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
1005   int f, r;
1006   int j;
1007
1008   for (j = 0; j < 4; j++) {
1009     f = 2*kingJumps[j][0] + onf;
1010     r = 2*kingJumps[j][1] + onr;
1011     if ((f < 0) || (f >= gs->files))
1012       continue;
1013     if ((r < 0) || (r >= gs->ranks))
1014       continue;
1015     if ((gs->board[f][r] == NOPIECE) ||
1016         (iscolor(gs->board[f][r], CToggle(gs->onMove))))
1017       add_pos(f, r, posf, posr, numpos);
1018   }
1019 }
1020
1021 static void possible_ferz_moves(struct game_state_t * gs,
1022                                   int onf, int onr,
1023                                   int *posf, int *posr, int *numpos)
1024 {
1025   static int kingJumps[4][2] = {{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
1026   int f, r;
1027   int j;
1028
1029   for (j = 0; j < 4; j++) {
1030     f = kingJumps[j][0] + onf;
1031     r = kingJumps[j][1] + onr;
1032     if ((f < 0) || (f >= gs->files))
1033       continue;
1034     if ((r < 0) || (r >= gs->ranks))
1035       continue;
1036     if ((gs->board[f][r] == NOPIECE) ||
1037         (iscolor(gs->board[f][r], CToggle(gs->onMove))))
1038       add_pos(f, r, posf, posr, numpos);
1039   }
1040 }
1041
1042 static void possible_mandarin_moves(struct game_state_t * gs,
1043                                   int onf, int onr,
1044                                   int *posf, int *posr, int *numpos)
1045 {
1046   static int kingJumps[4][2] = {{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
1047   int f, r;
1048   int j;
1049
1050   for (j = 0; j < 4; j++) {
1051     f = kingJumps[j][0] + onf;
1052     r = kingJumps[j][1] + onr;
1053     if ((f < 0) || (f >= gs->files))
1054       continue;
1055     if ((r < 0) || (r >= gs->ranks))
1056       continue;
1057     if(gs->palace && (r >= gs->palace && r < gs->ranks - gs->palace ||
1058        f < (gs->files - gs->palace)/2 || f >= (gs->files + gs->palace)/2))
1059       continue;
1060     if ((gs->board[f][r] == NOPIECE) ||
1061         (iscolor(gs->board[f][r], CToggle(gs->onMove))))
1062       add_pos(f, r, posf, posr, numpos);
1063   }
1064 }
1065
1066 static void possible_wazir_moves(struct game_state_t * gs,
1067                                   int onf, int onr,
1068                                   int *posf, int *posr, int *numpos)
1069 {
1070   static int kingJumps[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
1071   int f, r;
1072   int j;
1073
1074   for (j = 0; j < 4; j++) {
1075     f = kingJumps[j][0] + onf;
1076     r = kingJumps[j][1] + onr;
1077     if ((f < 0) || (f >= gs->files))
1078       continue;
1079     if ((r < 0) || (r >= gs->ranks))
1080       continue;
1081     if(gs->palace && (r >= gs->palace && r < gs->ranks - gs->palace ||
1082        f < (gs->files - gs->palace)/2 || f >= (gs->files + gs->palace)/2))
1083       continue;
1084     if ((gs->board[f][r] == NOPIECE) ||
1085         (iscolor(gs->board[f][r], CToggle(gs->onMove))))
1086       add_pos(f, r, posf, posr, numpos);
1087   }
1088 }
1089
1090 static void possible_dababba_moves(struct game_state_t * gs,
1091                                   int onf, int onr,
1092                                   int *posf, int *posr, int *numpos)
1093 {
1094   static int kingJumps[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
1095   int f, r;
1096   int j;
1097
1098   for (j = 0; j < 4; j++) {
1099     f = 2*kingJumps[j][0] + onf;
1100     r = 2*kingJumps[j][1] + onr;
1101     if ((f < 0) || (f >= gs->files))
1102       continue;
1103     if ((r < 0) || (r >= gs->ranks))
1104       continue;
1105     if ((gs->board[f][r] == NOPIECE) ||
1106         (iscolor(gs->board[f][r], CToggle(gs->onMove))))
1107       add_pos(f, r, posf, posr, numpos);
1108   }
1109 }
1110
1111 static void possible_man_moves(struct game_state_t * gs,
1112                                    int onf, int onr,
1113                                    int *posf, int *posr, int *numpos)
1114 {
1115   possible_wazir_moves(gs, onf, onr, posf, posr, numpos);
1116   possible_ferz_moves(gs, onf, onr, posf, posr, numpos);
1117 }
1118
1119 static void possible_dragonking_moves(struct game_state_t * gs,
1120                                    int onf, int onr,
1121                                    int *posf, int *posr, int *numpos)
1122 {
1123   possible_rook_moves(gs, onf, onr, posf, posr, numpos);
1124   possible_ferz_moves(gs, onf, onr, posf, posr, numpos);
1125 }
1126
1127 static void possible_dragonhorse_moves(struct game_state_t * gs,
1128                                    int onf, int onr,
1129                                    int *posf, int *posr, int *numpos)
1130 {
1131   possible_wazir_moves(gs, onf, onr, posf, posr, numpos);
1132   possible_bishop_moves(gs, onf, onr, posf, posr, numpos);
1133 }
1134
1135 static void possible_centaur_moves(struct game_state_t * gs,
1136                                    int onf, int onr,
1137                                    int *posf, int *posr, int *numpos)
1138 {
1139   possible_man_moves(gs, onf, onr, posf, posr, numpos);
1140   possible_knight_moves(gs, onf, onr, posf, posr, numpos);
1141 }
1142
1143 static void possible_woody_moves(struct game_state_t * gs,
1144                                    int onf, int onr,
1145                                    int *posf, int *posr, int *numpos)
1146 {
1147   possible_wazir_moves(gs, onf, onr, posf, posr, numpos);
1148   possible_dababba_moves(gs, onf, onr, posf, posr, numpos);
1149 }
1150
1151 static void possible_squirrel_moves(struct game_state_t * gs,
1152                                    int onf, int onr,
1153                                    int *posf, int *posr, int *numpos)
1154 {
1155   possible_alfil_moves(gs, onf, onr, posf, posr, numpos);
1156   possible_dababba_moves(gs, onf, onr, posf, posr, numpos);
1157   possible_knight_moves(gs, onf, onr, posf, posr, numpos);
1158 }
1159
1160 static void possible_mastodon_moves(struct game_state_t * gs,
1161                                    int onf, int onr,
1162                                    int *posf, int *posr, int *numpos)
1163 {
1164   possible_man_moves(gs, onf, onr, posf, posr, numpos);
1165   possible_alfil_moves(gs, onf, onr, posf, posr, numpos);
1166   possible_dababba_moves(gs, onf, onr, posf, posr, numpos);
1167 }
1168
1169 static void possible_amazon_moves(struct game_state_t * gs,
1170                                    int onf, int onr,
1171                                    int *posf, int *posr, int *numpos)
1172 {
1173   possible_queen_moves(gs, onf, onr, posf, posr, numpos);
1174   possible_knight_moves(gs, onf, onr, posf, posr, numpos);
1175 }
1176
1177 static void possible_modernelephant_moves(struct game_state_t * gs,
1178                                    int onf, int onr,
1179                                    int *posf, int *posr, int *numpos)
1180 {
1181   possible_ferz_moves(gs, onf, onr, posf, posr, numpos);
1182   possible_alfil_moves(gs, onf, onr, posf, posr, numpos);
1183 }
1184
1185 static void possible_priestess_moves(struct game_state_t * gs,
1186                                    int onf, int onr,
1187                                    int *posf, int *posr, int *numpos)
1188 {
1189   possible_ferz_moves(gs, onf, onr, posf, posr, numpos);
1190   possible_alfil_moves(gs, onf, onr, posf, posr, numpos);
1191   possible_knight_moves(gs, onf, onr, posf, posr, numpos);
1192 }
1193
1194 static void possible_minister_moves(struct game_state_t * gs,
1195                                    int onf, int onr,
1196                                    int *posf, int *posr, int *numpos)
1197 {
1198   possible_wazir_moves(gs, onf, onr, posf, posr, numpos);
1199   possible_dababba_moves(gs, onf, onr, posf, posr, numpos);
1200   possible_knight_moves(gs, onf, onr, posf, posr, numpos);
1201 }
1202
1203 static void possible_gold_moves(struct game_state_t * gs,
1204                                    int onf, int onr,
1205                                    int *posf, int *posr, int *numpos)
1206 {
1207   possible_wazir_moves(gs, onf, onr, posf, posr, numpos);
1208   if(gs->onMove == WHITE) {
1209     if(onr < gs->ranks-1)
1210       if(onf > 0 && !iscolor(gs->board[onf-1][onr+1], WHITE))
1211         add_pos(onf-1, onr+1, posf, posr, numpos);
1212       if(onf < gs->files-1 && !iscolor(gs->board[onf+1][onr+1], WHITE))
1213         add_pos(onf+1, onr+1, posf, posr, numpos);
1214   } else {
1215     if(onr > 0)
1216       if(onf > 0 && !iscolor(gs->board[onf-1][onr-1], BLACK))
1217         add_pos(onf-1, onr-1, posf, posr, numpos);
1218       if(onf < gs->files-1 && !iscolor(gs->board[onf+1][onr-1], BLACK))
1219         add_pos(onf+1, onr-1, posf, posr, numpos);
1220   }
1221 }
1222
1223 static void possible_silver_moves(struct game_state_t * gs,
1224                                    int onf, int onr,
1225                                    int *posf, int *posr, int *numpos)
1226 {
1227   possible_ferz_moves(gs, onf, onr, posf, posr, numpos);
1228   if(gs->onMove == WHITE) {
1229     if(onr < gs->ranks-1 && !iscolor(gs->board[onf][onr+1], WHITE))
1230       add_pos(onf, onr+1, posf, posr, numpos);
1231   } else {
1232     if(onr > 0 && !iscolor(gs->board[onf][onr-1], BLACK))
1233       add_pos(onf, onr-1, posf, posr, numpos);
1234   }
1235 }
1236
1237 static void possible_honorablehorse_moves(struct game_state_t * gs,
1238                                   int onf, int onr,
1239                                   int *posf, int *posr, int *numpos)
1240 {
1241   int f, r = onr + (gs->onMove == WHITE ? 2 : -2);
1242
1243   if(r < 0 || r >= gs->ranks) return;
1244   if(onf > 0) {
1245     if ((gs->board[onf-1][r] == NOPIECE) ||
1246         (iscolor(gs->board[onf-1][r], CToggle(gs->onMove))))
1247       add_pos(onf - 1, r, posf, posr, numpos);
1248   }
1249   if(onf < gs->files - 1) {
1250     if ((gs->board[onf+1][r] == NOPIECE) ||
1251         (iscolor(gs->board[onf+1][r], CToggle(gs->onMove))))
1252       add_pos(onf + 1, r, posf, posr, numpos);
1253   }
1254 }
1255
1256 static void possible_king_moves(struct game_state_t * gs,
1257                                   int onf, int onr,
1258                                   int *posf, int *posr, int *numpos)
1259 {
1260   if(gs->royalKnight)
1261     possible_knight_moves(gs, onf, onr, posf, posr, numpos);
1262   else if(gs->palace)
1263     possible_wazir_moves(gs, onf, onr, posf, posr, numpos);
1264   else
1265     possible_man_moves(gs, onf, onr, posf, posr, numpos);
1266 }
1267
1268 static void possible_elephant_moves(struct game_state_t * gs,
1269                                    int onf, int onr,
1270                                    int *posf, int *posr, int *numpos)
1271 {
1272   static int kingJumps[4][2] = {{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
1273   int f, r;
1274   int j;
1275
1276   for (j = 0; j < 4; j++) {
1277     f = 2*kingJumps[j][0] + onf;
1278     r = 2*kingJumps[j][1] + onr;
1279     if ((f < 0) || (f >= gs->files))
1280       continue;
1281     if ((r < 0) || (r >= gs->ranks))
1282       continue;
1283     if ((gs->board[(f+onf)/2][(r+onr)/2] == NOPIECE) && ((gs->board[f][r] == NOPIECE) ||
1284         (iscolor(gs->board[f][r], CToggle(gs->onMove)))))
1285       add_pos(f, r, posf, posr, numpos);
1286   }
1287 }
1288
1289 /* Doesn't check for check */
1290 int legal_move(struct game_state_t * gs,
1291                int fFile, int fRank,
1292                int tFile, int tRank)
1293 {
1294   int move_piece;
1295   int legal;
1296
1297   if (fFile == ALG_DROP) {
1298     move_piece = fRank;
1299     if(gs->drops != 1) return 0; // [HGM] variants: no drops in this variant
1300     if (move_piece == KING)
1301       return 0;
1302     if (gs->holding[gs->onMove==WHITE ? 0 : 1][move_piece-1] == 0)
1303       return 0;
1304     if (gs->board[tFile][tRank] != NOPIECE)
1305       return 0;
1306     if (gs->promoType == 3) { // Shogi
1307       int r;
1308       switch(move_piece) {
1309         case PAWN:  // check for own Pawn in same file
1310           for(r=0; r<gs->ranks; r++) if(gs->board[tFile][r] == (gs->onMove|PAWN)) return 0;
1311         case LANCE: // Pawns and Lances not on last rank
1312           if(gs->onMove == WHITE && tRank >= gs->ranks-1) return 0;
1313           if(gs->onMove == BLACK && tRank < 1) return 0;
1314           break;
1315         case HONORABLEHORSE: // Knights at least two ranks from edge
1316           if(gs->onMove == WHITE && tRank >= gs->ranks-2) return 0;
1317           if(gs->onMove == BLACK && tRank < 2) return 0;
1318         default: ;
1319       }
1320     } else
1321     if (move_piece == PAWN && (tRank == 0 || tRank == gs->ranks-1))
1322       return 0;
1323     return 1;
1324   } else if(fFile == ALG_CASTLE) {
1325         // [HGM] castle: this code can handle any type of free castling
1326         // it does not check if Rook and King from squares correspond to the rights, as the
1327         // user never enters such squares, but they are copied from the rights on entering o-o-o
1328         int backRank, kRook, qRook, fKing, leftEmpty, rightEmpty, leftCheck, rightCheck, f;
1329         if(!gs->castlingStyle) return 0;   // no castling in this variant
1330         if(gs->onMove == WHITE) {
1331                 if(gs->wkmoved < 0) return 0; // King moved
1332                 fKing = gs->wkmoved;
1333                 backRank = 0;
1334                 kRook = gs->wkrmoved;
1335                 qRook = gs->wqrmoved;
1336         } else {
1337                 if(gs->bkmoved < 0) return 0; // King moved
1338                 fKing = gs->bkmoved;
1339                 backRank = gs->ranks-1;
1340                 kRook = gs->bkrmoved;
1341                 qRook = gs->bqrmoved;
1342         }
1343         if((tRank > tFile ? qRook : kRook) < 0) return 0; // Rook moved
1344         // here we verified rights do exist, so from squares (fRank and fKing) must be valid
1345         if(gs->board[fRank][backRank] != (ROOK | gs->onMove) ) return 0; // only with own Rook
1346         if(gs->board[fKing][backRank] != (KING | gs->onMove) ) return 0; // only with own King
1347
1348         // by now we know that K and R are in correct position, and still have rights
1349         if(tRank > tFile) { // R ends right of K: q-side
1350                 leftEmpty  = fRank < tFile ? fRank+1 : tFile+1;
1351                 rightEmpty = tRank < fKing ? fKing-1 : tRank-1;
1352         } else { // k-side
1353                 leftEmpty  = tRank < fKing ? tRank+1 : fKing+1;
1354                 rightEmpty = fRank < tFile ? fRank-1 : tFile-1;
1355         }
1356         for(f=leftEmpty; f<=rightEmpty; f++) // check if other pieces block castling
1357                 if(f != fRank && f != fKing && gs->board[f][backRank] != NOPIECE) return 0;
1358
1359         leftCheck  = fKing < tFile ? fKing : tFile+1;
1360         rightCheck = fKing < tFile ? tFile-1 : fKing;
1361         for(f=leftCheck; f<=rightCheck; f++) // check if King passes attacked square or was in check
1362                 if(is_square_attacked(gs, f, backRank)) return 0;
1363
1364         return 1; // passed all tests
1365   } else {
1366     move_piece = piecetype(gs->board[fFile][fRank]);
1367   }
1368   if (gs->board[fFile][fRank] == NOPIECE)
1369     return 0;
1370   if (!iscolor(gs->board[fFile][fRank], gs->onMove))    /* Wrong color */
1371     return 0;
1372   if ((gs->board[tFile][tRank] != NOPIECE) &&
1373       iscolor(gs->board[tFile][tRank], gs->onMove))     /* Can't capture own */
1374     return 0;
1375   if ((fFile == tFile) && (fRank == tRank))     /* Same square */
1376     return 0;
1377   switch (move_piece) {
1378   case PAWN:
1379     legal = legal_pawn_move(gs, fFile, fRank, tFile, tRank);
1380     break;
1381   case KNIGHT:
1382     legal = legal_knight_move(gs, fFile, fRank, tFile, tRank);
1383     break;
1384   case BISHOP:
1385     legal = legal_bishop_move(gs, fFile, fRank, tFile, tRank);
1386     break;
1387   case ROOK:
1388     legal = legal_rook_move(gs, fFile, fRank, tFile, tRank);
1389     break;
1390   case HAWK:
1391   case CARDINAL:
1392   case PRINCESS:
1393     legal = legal_cardinal_move(gs, fFile, fRank, tFile, tRank);
1394     break;
1395   case SELEPHANT:
1396   case MARSHALL:
1397   case EMPRESS:
1398     legal = legal_marshall_move(gs, fFile, fRank, tFile, tRank);
1399     break;
1400   case MAN:
1401   case MAN2:
1402     legal = legal_man_move(gs, fFile, fRank, tFile, tRank);
1403     break;
1404   case QUEEN:
1405     legal = legal_queen_move(gs, fFile, fRank, tFile, tRank);
1406     break;
1407   case ELEPHANT:
1408     legal = legal_elephant_move(gs, fFile, fRank, tFile, tRank);
1409     break;
1410   case AMAZON:
1411     legal = legal_amazon_move(gs, fFile, fRank, tFile, tRank);
1412     break;
1413   case WOODY:
1414     legal = legal_woody_move(gs, fFile, fRank, tFile, tRank);
1415     break;
1416   case SQUIRREL:
1417     legal = legal_squirrel_move(gs, fFile, fRank, tFile, tRank);
1418     break;
1419   case MASTODON:
1420     legal = legal_mastodon_move(gs, fFile, fRank, tFile, tRank);
1421     break;
1422   case CENTAUR:
1423     legal = legal_centaur_move(gs, fFile, fRank, tFile, tRank);
1424     break;
1425   case HORSE:
1426     legal = legal_horse_move(gs, fFile, fRank, tFile, tRank);
1427     break;
1428   case FERZ:
1429   case FERZ2:
1430     legal = legal_ferz_move(gs, fFile, fRank, tFile, tRank);
1431     break;
1432   case MANDARIN:
1433     legal = legal_mandarin_move(gs, fFile, fRank, tFile, tRank);
1434     break;
1435   case WAZIR:
1436     legal = legal_wazir_move(gs, fFile, fRank, tFile, tRank);
1437     break;
1438   case ALFIL:
1439   case ALFIL2:
1440     legal = legal_alfil_move(gs, fFile, fRank, tFile, tRank);
1441     break;
1442   case MODERNELEPHANT:
1443     legal = legal_modernelephant_move(gs, fFile, fRank, tFile, tRank);
1444     break;
1445   case PRIESTESS:
1446     legal = legal_priestess_move(gs, fFile, fRank, tFile, tRank);
1447     break;
1448   case MINISTER:
1449     legal = legal_minister_move(gs, fFile, fRank, tFile, tRank);
1450     break;
1451   case SILVER:
1452     legal = legal_silver_move(gs, fFile, fRank, tFile, tRank);
1453     break;
1454   case GOLD:
1455     legal = legal_gold_move(gs, fFile, fRank, tFile, tRank);
1456     break;
1457   case LANCE:
1458     legal = legal_lance_move(gs, fFile, fRank, tFile, tRank);
1459     break;
1460   case CANNON:
1461     legal = legal_cannon_move(gs, fFile, fRank, tFile, tRank);
1462     break;
1463   case DRAGONHORSE:
1464     legal = legal_dragonhorse_move(gs, fFile, fRank, tFile, tRank);
1465     break;
1466   case DRAGONKING:
1467     legal = legal_dragonking_move(gs, fFile, fRank, tFile, tRank);
1468     break;
1469   case HONORABLEHORSE:
1470     legal = legal_honorablehorse_move(gs, fFile, fRank, tFile, tRank);
1471     break;
1472   case KING:
1473     legal = legal_king_move(gs, fFile, fRank, tFile, tRank);
1474     break;
1475   default:
1476     return 0;
1477     break;
1478   }
1479   return legal;
1480 }
1481
1482 #define DROP_CHAR '@'
1483
1484 /* This fills in the rest of the mt structure once it is determined that
1485  * the move is legal. Returns MOVE_ILLEGAL if move leaves you in check */
1486 static int move_calculate(struct game_state_t * gs, struct move_t * mt, int promote)
1487 {
1488   struct game_state_t fakeMove;
1489   int gating = 0, stm;
1490
1491   mt->pieceCaptured = gs->board[mt->toFile][mt->toRank];
1492   mt->enPassant = 0;            /* Don't know yet, let execute move take care
1493                                    of it */
1494   if (mt->fromFile == ALG_DROP) {
1495     mt->piecePromotionTo = NOPIECE;
1496     sprintf(mt->moveString, "%s/%c%c-%c%d",
1497             wpstring[mt->fromRank],
1498                 DROP_CHAR, DROP_CHAR,
1499             mt->toFile + 'a', mt->toRank + 1 - (gs->ranks>9));
1500   } else if(mt->fromFile == ALG_CASTLE) { 
1501         // [HGM] castle: generalized castling, fr and tr give from and to file of Rook.
1502             sprintf(mt->moveString, mt->toRank > mt->toFile ? "o-o-o" : "o-o");
1503         if(gs->drops == 2 && promote && gs->holding[gs->onMove == BLACK][promote-1])
1504             mt->piecePromotionTo = promote, gating = 1;
1505   } else {
1506   stm = colorval(gs->board[mt->fromFile][mt->fromRank]);
1507   if(gs->promoType == 3) { // Shogi-style promotions: not just Pawns, but many pieces can promote
1508     int piece = gs->board[mt->fromFile][mt->fromRank];
1509     mt->piecePromotionTo = NOPIECE;
1510     if(colorval(piece) == WHITE && mt->fromRank < gs->ranks - gs->ranks/3
1511                                 && mt->toRank   < gs->ranks - gs->ranks/3 ||
1512        colorval(piece) == BLACK && mt->fromRank >= gs->ranks/3
1513                                 && mt->toRank   >= gs->ranks/3 )
1514         promote = NOPIECE; // suppress promotion outside zone
1515     if(promote) { // promotion piece determined by original, no matter what was requested
1516       switch(piecetype(piece)) {
1517         case PAWN:
1518         case LANCE:
1519         case HONORABLEHORSE:
1520         case SILVER:
1521           promote = GOLD; break;
1522         case BISHOP:
1523           promote = DRAGONHORSE; break;
1524         case ROOK:
1525           promote = DRAGONKING; break;
1526         default: promote = NOPIECE; // not a promotion
1527       }
1528     } else
1529       switch(piecetype(piece)) { // force mandatory promotions
1530         case HONORABLEHORSE:
1531           if(mt->toRank == 1 || mt->toRank == gs->files-2) promote = GOLD;
1532         case PAWN:
1533         case LANCE:
1534           if(mt->toRank == 0 || mt->toRank == gs->files-1) promote = GOLD;
1535         default: break;
1536       }
1537     if(promote) mt->piecePromotionTo = promote | (colorval(gs->board[mt->fromFile][mt->fromRank]));
1538   } else
1539   if ((piecetype(gs->board[mt->fromFile][mt->fromRank]) == PAWN) && 
1540         !gs->palace && // [HGM] XQ: no promotions in xiangqi
1541       ((mt->toRank < gs->promoZone) || (mt->toRank >= gs->ranks - gs->promoZone))) {
1542     if(!promote && (mt->toRank == 0 || mt->toRank == gs->ranks-1)) { // promotion obligatory, but not specified
1543         if(gs->promoType != 2) promote = QUEEN; else { // choose a default
1544             for(promote=PIECES-1; promote>PAWN; promote--) if(gs->holding[stm == BLACK][promote-1]) break;
1545             if(promote == PAWN) return MOVE_ILLEGAL; // nothing available
1546         }
1547     } // if not obligatory, we defer unless promotion was explicitly specified!
1548     if(!gs->pawnDblStep && promote == PRINCESS) promote = MAN2;
1549     if(!gs->pawnDblStep && promote != FERZ2 && promote != MAN2) promote = FERZ; // [HGM] kludge to recognize shatranj
1550     // non-promotion can still be an option for deeper promotion zones
1551     mt->piecePromotionTo = promote ? (promote | stm) : NOPIECE;
1552     if(promote && gs->promoType == 2 && !gs->holding[stm == BLACK][promote-1]) return MOVE_ILLEGAL; // unavailable piece specified
1553   } else if(gs->drops == 2 && promote && mt->fromRank == (stm == WHITE ? 0 : gs->ranks-1)) { // [HGM] Seirawan-style gating
1554     int i; struct game *g = &game_globals.garray[gs->gameNum];
1555     if(!gs->holding[stm == BLACK][promote-1]) return MOVE_ILLEGAL; // unavailable piece specified
1556     // now we must test virginity of the moved piece. Yegh!
1557     for (i = g->numHalfMoves-1; i >= 0; i--) {
1558       if (g->moveList[i].fromFile == mt->fromFile && g->moveList[i].fromRank == mt->fromRank ||
1559           g->moveList[i].toFile   == mt->fromFile && g->moveList[i].toRank   == mt->fromRank) return MOVE_ILLEGAL;
1560     }
1561     mt->piecePromotionTo = promote; // gating OK
1562     gating = 1; // remember we did it for check test
1563   } else {
1564     mt->piecePromotionTo = NOPIECE;
1565   }
1566   if ((piecetype(gs->board[mt->fromFile][mt->fromRank]) == PAWN) &&
1567    ((mt->fromRank - mt->toRank == 2) || (mt->toRank - mt->fromRank == 2))) {
1568     mt->doublePawn = mt->fromFile;
1569   } else {
1570     mt->doublePawn = -1;
1571   }
1572 #if 0
1573   if ((piecetype(gs->board[mt->fromFile][mt->fromRank]) == KING) &&
1574       (mt->fromFile == gs->files/2) && (mt->toFile == 2) &&
1575        mt->fromRank == mt->toRank) {
1576     sprintf(mt->moveString, "o-o-o");
1577   } else if ((piecetype(gs->board[mt->fromFile][mt->fromRank]) == KING) &&
1578              (mt->fromFile == gs->files/2) && (mt->toFile == gs->files-2) &&
1579                 mt->fromRank == mt->toRank) {
1580     sprintf(mt->moveString, "o-o");
1581   } else {
1582 #else
1583   {
1584 #endif
1585     sprintf(mt->moveString, "%s/%c%d-%c%d",
1586             wpstring[piecetype(gs->board[mt->fromFile][mt->fromRank])],
1587             mt->fromFile + 'a', mt->fromRank + 1 - (gs->ranks>9),
1588             mt->toFile + 'a', mt->toRank + 1 - (gs->ranks>9));
1589   }
1590   }
1591   /* Replace this with an algabraic de-parser */
1592
1593   sprintf(mt->algString, alg_unparse(gs, mt));
1594   fakeMove = *gs;
1595   /* Calculates enPassant also */
1596   execute_move(&fakeMove, mt, 0);
1597   if(gating) fakeMove.board[mt->fromFile][mt->fromRank] = NOPIECE; // [HGM] gating is only legal if non-gating move was (weird, but true)
1598
1599   /* Does making this move leave ME in check? */
1600   if (in_check(&fakeMove))
1601     return MOVE_ILLEGAL;
1602   /* IanO: bughouse variants: drop cannot be check/checkmate */
1603
1604   return MOVE_OK;
1605 }
1606
1607 int legal_andcheck_move(struct game_state_t * gs,
1608                         int fFile, int fRank,
1609                         int tFile, int tRank)
1610 {
1611   struct move_t mt;
1612
1613   if (!legal_move(gs, fFile, fRank, tFile, tRank))
1614     return 0;
1615   mt.color = gs->onMove;
1616   mt.fromFile = fFile;
1617   mt.fromRank = fRank;
1618   mt.toFile = tFile;
1619   mt.toRank = tRank;
1620   /* This should take into account a pawn promoting to another piece */
1621   if (move_calculate(gs, &mt, NOPIECE) == MOVE_OK) 
1622     return 1;
1623   else
1624     return 0;
1625 }
1626
1627 /* in_check: checks if the side that is NOT about to move is in check 
1628  */
1629 int in_check(struct game_state_t * gs)
1630 {
1631   int f, r;
1632   int kf = -1, kr = -1;
1633
1634   /* Find the king */
1635   if (gs->onMove == WHITE) {
1636     for (f = 0; f < gs->files && kf < 0; f++)
1637       for (r = 0; r < gs->ranks && kf < 0; r++)
1638         if (gs->board[f][r] == B_KING) {
1639           kf = f;
1640           kr = r;
1641         }
1642   } else {
1643     for (f = 0; f < gs->files && kf < 0; f++)
1644       for (r = 0; r < gs->ranks && kf < 0; r++)
1645         if (gs->board[f][r] == W_KING) {
1646           kf = f;
1647           kr = r;
1648         }
1649   }
1650   if (kf < 0) {
1651     d_printf( "CHESSD: Error game with no king!\n");
1652     return 0;
1653   }
1654   for (InitPieceLoop(gs->board, &f, &r, gs->onMove);
1655        NextPieceLoop(gs->board, &f, &r, gs->onMove, gs->files, gs->ranks);) {
1656     if (legal_move(gs, f, r, kf, kr)) { /* In Check? */
1657       return 1;
1658     }
1659   }
1660   return 0;
1661 }
1662
1663 int has_legal_move(struct game_state_t * gs)
1664 {
1665   int i;
1666   int f, r;
1667   int kf = 0, kr = 0;
1668   int possiblef[500], possibler[500];
1669   int numpossible = 0;
1670
1671   for (InitPieceLoop(gs->board, &f, &r, gs->onMove);
1672        NextPieceLoop(gs->board, &f, &r, gs->onMove, gs->files, gs->ranks);) {
1673     switch (piecetype(gs->board[f][r])) {
1674     case PAWN:
1675       possible_pawn_moves(gs, f, r, possiblef, possibler, &numpossible);
1676       break;
1677     case KNIGHT:
1678       possible_knight_moves(gs, f, r, possiblef, possibler, &numpossible);
1679       break;
1680     case BISHOP:
1681       possible_bishop_moves(gs, f, r, possiblef, possibler, &numpossible);
1682       break;
1683     case ROOK:
1684       possible_rook_moves(gs, f, r, possiblef, possibler, &numpossible);
1685       break;
1686     case HAWK:
1687     case CARDINAL:
1688     case PRINCESS:
1689       possible_cardinal_moves(gs, f, r, possiblef, possibler, &numpossible);
1690       break;
1691     case SELEPHANT:
1692     case MARSHALL:
1693     case EMPRESS:
1694       possible_marshall_moves(gs, f, r, possiblef, possibler, &numpossible);
1695       break;
1696     case MAN:
1697     case MAN2:
1698       possible_man_moves(gs, f, r, possiblef, possibler, &numpossible);
1699       break;
1700     case QUEEN:
1701       possible_queen_moves(gs, f, r, possiblef, possibler, &numpossible);
1702       break;
1703     case ELEPHANT:
1704       possible_elephant_moves(gs, f, r, possiblef, possibler, &numpossible);
1705       break;
1706     case AMAZON:
1707       possible_amazon_moves(gs, f, r, possiblef, possibler, &numpossible);
1708       break;
1709     case WOODY:
1710       possible_woody_moves(gs, f, r, possiblef, possibler, &numpossible);
1711       break;
1712     case SQUIRREL:
1713       possible_squirrel_moves(gs, f, r, possiblef, possibler, &numpossible);
1714       break;
1715     case MASTODON:
1716       possible_mastodon_moves(gs, f, r, possiblef, possibler, &numpossible);
1717       break;
1718     case CENTAUR:
1719       possible_centaur_moves(gs, f, r, possiblef, possibler, &numpossible);
1720       break;
1721     case HORSE:
1722       possible_horse_moves(gs, f, r, possiblef, possibler, &numpossible);
1723       break;
1724     case FERZ:
1725     case FERZ2:
1726       possible_ferz_moves(gs, f, r, possiblef, possibler, &numpossible);
1727       break;
1728     case MANDARIN:
1729       possible_mandarin_moves(gs, f, r, possiblef, possibler, &numpossible);
1730       break;
1731     case WAZIR:
1732       possible_wazir_moves(gs, f, r, possiblef, possibler, &numpossible);
1733       break;
1734     case ALFIL:
1735     case ALFIL2:
1736       possible_alfil_moves(gs, f, r, possiblef, possibler, &numpossible);
1737       break;
1738     case MODERNELEPHANT:
1739       possible_modernelephant_moves(gs, f, r, possiblef, possibler, &numpossible);
1740       break;
1741     case PRIESTESS:
1742       possible_priestess_moves(gs, f, r, possiblef, possibler, &numpossible);
1743       break;
1744     case MINISTER:
1745       possible_minister_moves(gs, f, r, possiblef, possibler, &numpossible);
1746       break;
1747     case SILVER:
1748       possible_silver_moves(gs, f, r, possiblef, possibler, &numpossible);
1749       break;
1750     case GOLD:
1751       possible_gold_moves(gs, f, r, possiblef, possibler, &numpossible);
1752       break;
1753     case CANNON:
1754       possible_cannon_moves(gs, f, r, possiblef, possibler, &numpossible);
1755       break;
1756     case LANCE:
1757       possible_lance_moves(gs, f, r, possiblef, possibler, &numpossible);
1758       break;
1759     case DRAGONHORSE:
1760       possible_dragonhorse_moves(gs, f, r, possiblef, possibler, &numpossible);
1761       break;
1762     case DRAGONKING:
1763       possible_dragonking_moves(gs, f, r, possiblef, possibler, &numpossible);
1764       break;
1765     case HONORABLEHORSE:
1766       possible_honorablehorse_moves(gs, f, r, possiblef, possibler, &numpossible);
1767       break;
1768     case KING:
1769       kf = f;
1770       kr = r;
1771       possible_king_moves(gs, f, r, possiblef, possibler, &numpossible);
1772       break;
1773     }
1774     if (numpossible >= 500) {
1775       d_printf( "CHESSD: Possible move overrun\n");
1776     }
1777     for (i = 0; i < numpossible; i++)
1778       if (legal_andcheck_move(gs, f, r, possiblef[i], possibler[i])) {
1779         return 1;
1780       }
1781   }
1782
1783   /* IanO:  if we got here, then kf and kr must be set */
1784   if (gs->gameNum >=0 && game_globals.garray[gs->gameNum].link >= 0
1785         || gs->holdings) { // [HGM] zh: also in 2-player games with drops
1786     /* bughouse: potential drops as check interpositions */
1787     gs->holding[gs->onMove==WHITE ? 0 : 1][QUEEN - 1]++;
1788     for (f=kf-1; f<=kf+1; f++) for (r=kr-1; r<=kr+1; r++) {
1789       if (f>=0 && f<gs->files && r>=0 && r<gs->ranks && gs->board[f][r] == NOPIECE) {
1790         /* try a drop next to the king */
1791         if (legal_andcheck_move(gs, ALG_DROP, QUEEN, f, r)) {
1792           gs->holding[gs->onMove==WHITE ? 0 : 1][QUEEN - 1]--;
1793           // OK, so we have an interposing drop. But do we have something to drop?
1794           if(game_globals.garray[gs->gameNum].link < 0) {
1795                 // we have no partner, so we must have something to drop now
1796                 for(i=QUEEN; i>=PAWN; i--)
1797                         if (legal_andcheck_move(gs, ALG_DROP, i, f, r)) return 1;
1798                 return 0;
1799           }
1800           return 1;
1801         }
1802       }
1803     }
1804     gs->holding[gs->onMove==WHITE ? 0 : 1][QUEEN - 1]--;
1805   }
1806
1807   return 0;
1808 }
1809
1810 /* This will end up being a very complicated function */
1811 int parse_move(char *mstr, struct game_state_t * gs, struct move_t * mt, int promote)
1812 {
1813   int type = is_move(mstr);
1814   int result;
1815
1816   mt->piecePromotionTo = NOPIECE;
1817   mt->color = gs->onMove;
1818   switch (type) {
1819   case MS_NOTMOVE:
1820     return MOVE_ILLEGAL;
1821     break;
1822   case MS_COMP:
1823     mt->fromFile = mstr[0] - 'a';
1824     mt->fromRank = mstr[1] - '1' + (gs->ranks>9);
1825     mt->toFile = mstr[2] - 'a';
1826     mt->toRank = mstr[3] - '1' + (gs->ranks>9);
1827     break;
1828   case MS_COMPDASH:
1829     mt->fromFile = mstr[0] - 'a';
1830     mt->fromRank = mstr[1] - '1' + (gs->ranks>9);
1831     mt->toFile = mstr[3] - 'a';
1832     mt->toRank = mstr[4] - '1' + (gs->ranks>9);
1833     break;
1834   case MS_KCASTLE:
1835 #if 0
1836     mt->fromFile = gs->files/2;
1837     mt->toFile = gs->files-2;
1838     if (gs->onMove == WHITE) {
1839       mt->fromRank = 0;
1840       mt->toRank = 0;
1841     } else {
1842       mt->fromRank = gs->ranks-1;
1843       mt->toRank = gs->ranks-1;
1844     }
1845     break;
1846 #endif
1847     // [HGM] castle: for now always assume Fischer-type castling (of which normal castling is a special case).
1848     mt->fromFile = ALG_CASTLE;
1849     mt->toFile = gs->files-2;
1850     mt->fromRank = gs->onMove == WHITE ? gs->wkrmoved : gs->bkrmoved;
1851     mt->toRank = mt->toFile-1; // R next to K
1852     break;    
1853   case MS_QCASTLE:
1854 #if 0
1855     mt->fromFile = gs->files/2;
1856     mt->toFile = 2;
1857     if (gs->onMove == WHITE) {
1858       mt->fromRank = 0;
1859       mt->toRank = 0;
1860     } else {
1861       mt->fromRank = gs->ranks-1;
1862       mt->toRank = gs->ranks-1;
1863     }
1864     break;
1865 #endif
1866     mt->fromFile = ALG_CASTLE;
1867     mt->toFile = 2;
1868     mt->fromRank = gs->onMove == WHITE ? gs->wqrmoved : gs->bqrmoved;
1869     mt->toRank = mt->toFile+1;
1870     break;
1871   case MS_ALG:
1872     /* Fills in the mt structure */
1873     if ((result = alg_parse_move(mstr, gs, mt)) != MOVE_OK)
1874       return result;
1875     break;
1876   default:
1877     return MOVE_ILLEGAL;
1878     break;
1879   }
1880   if((mt->fromRank >= gs->ranks || mt->fromRank < 0 || mt->fromFile >= gs->files) &&
1881      mt->fromFile != ALG_DROP && mt->fromFile != ALG_CASTLE
1882      || mt->toRank < 0 || mt->toRank >= gs->ranks || mt->toFile >= gs->files)
1883     return MOVE_ILLEGAL; // [HGM] make sure move stays on board
1884
1885   if (!(result = legal_move(gs, mt->fromFile, mt->fromRank, mt->toFile, mt->toRank)))
1886     return MOVE_ILLEGAL;
1887
1888   if(result == 2) { // [HGM] castle: orthodox castling was given as King move; convert it to new format
1889         if(mt->fromFile - mt->toFile > 1) { // Q-side
1890                 mt->fromRank = 0; 
1891                 mt->toRank   = mt->toFile+1;
1892         } else if(mt->toFile - mt->fromFile > 1) { // K-side
1893                 mt->fromRank = gs->files-1;
1894                 mt->toRank   = mt->toFile-1;
1895         }
1896         mt->fromFile = ALG_CASTLE;
1897     }
1898
1899   if (mt->piecePromotionTo != NOPIECE) {
1900           promote = piecetype(mt->piecePromotionTo);
1901   } else if (promote != NOPIECE) { // [HGM] promotion on long algebraic move; correct ambiguous types for variant
1902     if(gs->promoType == 3 && promote == MASTODON) promote = GOLD;
1903     if(gs->drops == 2 && promote == EMPRESS) promote = SELEPHANT;
1904     if(gs->drops == 2 && promote == DRAGONHORSE) promote = HAWK;
1905   }
1906
1907   return move_calculate(gs, mt, promote);
1908 }
1909
1910 /* Returns MOVE_OK, MOVE_NOMATERIAL, MOVE_CHECKMATE, or MOVE_STALEMATE */
1911 /* check_game_status prevents recursion */
1912 int execute_move(struct game_state_t * gs, struct move_t * mt, int check_game_status)
1913 {
1914   int movedPiece;
1915   int tookPiece;
1916   int i, j, foobar, wCnt, bCnt, king, rook;
1917
1918   if (mt->fromFile == ALG_DROP) {
1919     movedPiece = mt->fromRank;
1920     tookPiece = NOPIECE;
1921     gs->holding[gs->onMove==WHITE ? 0 : 1][movedPiece-1]--;
1922     gs->board[mt->toFile][mt->toRank] = movedPiece | gs->onMove;
1923     if (gs->gameNum >= 0)
1924       gs->lastIrreversable = game_globals.garray[gs->gameNum].numHalfMoves;
1925   } else if(mt->fromFile == ALG_CASTLE) {
1926     int backRank, fKing;
1927     // [HGM] castle: perform castling
1928     if(gs->onMove == WHITE) {
1929         backRank = 0;
1930         fKing = gs->wkmoved;
1931         gs->wkmoved = -gs->wkmoved-2;
1932     } else {
1933         backRank = gs->ranks-1;
1934         fKing = gs->bkmoved;
1935         gs->bkmoved = -gs->bkmoved-2;
1936     }
1937     // move Rook & King, in a way that is resistant to ending where they started (for FRC!)
1938     rook = gs->board[mt->fromRank][backRank];    // first remember
1939     king = gs->board[fKing][backRank];
1940     gs->board[mt->fromRank][backRank] = NOPIECE; // then erase
1941     gs->board[fKing][backRank] = NOPIECE;
1942     gs->board[mt->toRank][backRank] = rook;      // then put back
1943     gs->board[mt->toFile][backRank] = king;
1944     if(gs->drops == 2 && mt->piecePromotionTo != NOPIECE) { // [HGM] Seirawan-style gating
1945       gs->board[fKing][backRank] = mt->piecePromotionTo | gs->onMove;   // for now always on King square
1946       gs->holding[gs->onMove==WHITE ? 0 : 1][mt->piecePromotionTo-1]--; // remove gated piece from holdings
1947     }
1948   } else {
1949   movedPiece = gs->board[mt->fromFile][mt->fromRank];
1950   tookPiece = gs->board[mt->toFile][mt->toRank];
1951   if(gs->drops == 2 && mt->piecePromotionTo != NOPIECE && piecetype(movedPiece) != PAWN) { // [HGM] Seirawan-style gating
1952     gs->board[mt->toFile][mt->toRank] = gs->board[mt->fromFile][mt->fromRank];
1953     gs->board[mt->fromFile][mt->fromRank] = mt->piecePromotionTo | gs->onMove;;
1954     gs->holding[gs->onMove==WHITE ? 0 : 1][mt->piecePromotionTo-1]--; // remove gated piece from holdings
1955   } else {
1956     if (mt->piecePromotionTo == NOPIECE) {
1957       gs->board[mt->toFile][mt->toRank] = gs->board[mt->fromFile][mt->fromRank];
1958     } else {
1959       gs->board[mt->toFile][mt->toRank] = mt->piecePromotionTo | gs->onMove;
1960       if(gs->promoType == 2) gs->holding[gs->onMove][mt->piecePromotionTo-1]--;
1961     }
1962     gs->board[mt->fromFile][mt->fromRank] = NOPIECE;
1963   }
1964   /* Check if irreversable */
1965   if ((piecetype(movedPiece) == PAWN) && (mt->fromRank != mt->toRank) // [HGM] XQ: sideway Pawn move reversible!
1966                         || (tookPiece != NOPIECE)) {
1967     if (gs->gameNum >= 0)
1968       gs->lastIrreversable = game_globals.garray[gs->gameNum].numHalfMoves;
1969   }
1970   /* Check if this move is en-passant */
1971   if ((piecetype(movedPiece) == PAWN) && (mt->fromFile != mt->toFile) &&
1972       (tookPiece == NOPIECE) && !gs->palace) { // [HGM] XQ: no e.p. in sideway xiangqi moves
1973     if (gs->onMove == WHITE) {
1974       mt->pieceCaptured = B_PAWN;
1975     } else {
1976       mt->pieceCaptured = W_PAWN;
1977     }
1978     if (mt->fromFile > mt->toFile) {
1979       mt->enPassant = -1;
1980     } else {
1981       mt->enPassant = 1;
1982     }
1983     gs->board[mt->toFile][mt->fromRank] = NOPIECE;
1984   }
1985   /* Check en-passant flags for next moves */
1986   for (i = 0; i < gs->files; i++) {
1987     gs->ep_possible[0][i] = 0;
1988     gs->ep_possible[1][i] = 0;
1989   }
1990 /* Added by Sparky 3/16/95
1991
1992    From soso@Viktoria.drp.fmph.uniba.sk Thu Mar 16 13:08:51 1995
1993    Subject: Re: To DAV: enpassant prob. again
1994    To: chess@caissa.onenet.net (ICS)
1995    Date: Thu, 16 Mar 1995 20:06:20 +0100 (MET)
1996
1997    Yeah !
1998    There was bug in other part of code:
1999
2000    movecheck.c , line about 800:
2001
2002      if (gs->onMove == WHITE) {
2003         if ((mt->toFile+1 < 7 ) ....  should be : (mt->toFile < 7 ) }
2004 */
2005
2006   if ((piecetype(movedPiece) == PAWN) &&
2007    ((mt->fromRank == mt->toRank + 2) || (mt->fromRank + 2 == mt->toRank))) {
2008     /* Should turn on enpassent flag if possible */
2009     if (gs->onMove == WHITE) {
2010       if ((mt->toFile < gs->files-1) && gs->board[mt->toFile + 1][mt->toRank] == B_PAWN) {
2011         gs->ep_possible[1][mt->toFile + 1] = -1;
2012       }
2013       if ((mt->toFile - 1 >= 0) && gs->board[mt->toFile - 1][mt->toRank] == B_PAWN) {
2014         gs->ep_possible[1][mt->toFile - 1] = 1;
2015       }
2016     } else {
2017       if ((mt->toFile < gs->files-1) && gs->board[mt->toFile + 1][mt->toRank] == W_PAWN) {
2018         gs->ep_possible[0][mt->toFile + 1] = -1;
2019       }
2020       if ((mt->toFile - 1 >= 0) && gs->board[mt->toFile - 1][mt->toRank] == W_PAWN) {
2021         gs->ep_possible[0][mt->toFile - 1] = 1;
2022       }
2023     }
2024   }
2025   if ((piecetype(movedPiece) == ROOK) && (mt->fromRank == 0) && (gs->onMove == WHITE)) {
2026     if (mt->fromFile == gs->wqrmoved) // [HGM] castle: flip w.r.t. -1 to remember original
2027       gs->wqrmoved = -gs->wqrmoved-2;
2028     if (mt->fromFile == gs->wkrmoved)
2029       gs->wkrmoved = -gs->wkrmoved-2;
2030   }
2031   if ((piecetype(movedPiece) == ROOK) && (mt->fromRank == gs->ranks-1) && (gs->onMove == BLACK)) {
2032     if (mt->fromFile == gs->bqrmoved)
2033       gs->bqrmoved = -gs->bqrmoved-2;
2034     if (mt->fromFile == gs->bkrmoved)
2035       gs->bkrmoved = -gs->bkrmoved-2;
2036   }
2037   if (piecetype(movedPiece) == KING) {
2038     if ((gs->onMove == WHITE) && (mt->fromFile == gs->wkmoved))
2039       gs->wkmoved = -gs->wkmoved-2;
2040     if ((gs->onMove == BLACK) && (mt->fromFile == gs->bkmoved))
2041       gs->bkmoved = -gs->bkmoved-2;
2042   }
2043 #if 0
2044   if ((piecetype(movedPiece) == KING) &&
2045       ((mt->fromFile == gs->files/2) && (mt->toFile == gs->files-2)) &&
2046         mt->fromRank == mt->toRank) {   /* Check for KS castling */
2047     gs->board[gs->files-3][mt->toRank] = gs->board[gs->files-1][mt->toRank];
2048     gs->board[gs->files-1][mt->toRank] = NOPIECE;
2049   }
2050   if ((piecetype(movedPiece) == KING) &&
2051       ((mt->fromFile == gs->files/2) && (mt->toFile == 2)) &&
2052         mt->fromRank == mt->toRank) {   /* Check for QS castling */
2053     gs->board[3][mt->toRank] = gs->board[0][mt->toRank];
2054     gs->board[0][mt->toRank] = NOPIECE;
2055   }
2056 #endif
2057   }
2058   if (gs->onMove == BLACK)
2059     gs->moveNum++;
2060
2061   if (check_game_status) {
2062     /* Does this move result in check? */
2063     if (in_check(gs)) {
2064       /* Check for checkmate */
2065       gs->onMove = CToggle(gs->onMove);
2066       if (!has_legal_move(gs))
2067         return MOVE_CHECKMATE;
2068     } else {
2069       /* Check for stalemate */
2070       gs->onMove = CToggle(gs->onMove);
2071       if (!has_legal_move(gs))
2072         return gs->stalemate ? MOVE_STALEMATE : MOVE_CHECKMATE; // [HGM] in XQ and shatranj stalemate loses
2073     }
2074 /* loon: check for insufficient mating material, first try */
2075       foobar = wCnt = bCnt = 0;
2076       for (i=0; i<gs->files; i++) {
2077         for (j=0; j<gs->ranks; j++) {
2078           int p = gs->board[i][j];
2079           switch(piecetype(p)) {
2080             case KNIGHT:
2081             case BISHOP:
2082               foobar++;
2083               break;
2084             case KING:
2085             case NOPIECE:
2086               break;
2087             default:
2088               foobar = 2;
2089               break;
2090           }
2091           if(p != NOPIECE && iscolor(p, WHITE)) wCnt++;
2092           if(iscolor(p, BLACK)) bCnt++;
2093         }
2094       }
2095       if(gs->bareKingLoses) { // [HGM] with bare-King-loses rule only KK is insuff. material
2096         if(gs->onMove == BLACK && wCnt == 1 && bCnt > 1) return MOVE_BARE;
2097         if(gs->onMove == WHITE && bCnt == 1 && wCnt > 1) return MOVE_BARE;
2098         if(bCnt == 1 && wCnt == 1) return MOVE_NOMATERIAL;
2099       } else if (foobar < 2)
2100         return MOVE_NOMATERIAL;
2101   } else {
2102     gs->onMove = CToggle(gs->onMove);
2103   }
2104
2105   return MOVE_OK;
2106 }
2107
2108 int backup_move(int g, int mode)
2109 {
2110   struct game_state_t *gs;
2111   struct move_t *m, *m1;
2112   int now, i;
2113
2114   if (game_globals.garray[g].link >= 0) /*IanO: not implemented for bughouse yet */
2115     return MOVE_ILLEGAL;
2116   if (game_globals.garray[g].numHalfMoves < 1)
2117     return MOVE_ILLEGAL;
2118   gs = &game_globals.garray[g].game_state;
2119   m = (mode==REL_GAME) ? &game_globals.garray[g].moveList[game_globals.garray[g].numHalfMoves - 1] : 
2120                          &game_globals.garray[g].examMoveList[game_globals.garray[g].numHalfMoves - 1];
2121   if (m->toFile < 0) {
2122     return MOVE_ILLEGAL;
2123   }
2124   if(m->fromFile == ALG_CASTLE) {
2125     // [HGM] castling in new format. Derive K and R moves
2126     int rank, kingFromFile;
2127     if(m->color == WHITE) {
2128       rank = 0;
2129       kingFromFile = -gs->wkmoved-2;
2130       if(kingFromFile<0) kingFromFile = -kingFromFile-2; // safety catch; should never happen?
2131       gs->wkmoved = kingFromFile;
2132       if(m->toRank > m->toFile) gs->wqrmoved = m->fromRank;
2133       else gs->wkrmoved = m->fromRank;
2134     } else {
2135       rank = gs->ranks-1;
2136       kingFromFile = -gs->bkmoved-2;
2137       if(kingFromFile<0) kingFromFile = -kingFromFile-2; // safety catch; should never happen?
2138       gs->bkmoved = kingFromFile;
2139       if(m->toRank > m->toFile) gs->bqrmoved = m->fromRank;
2140       else gs->bkrmoved = m->fromRank;
2141     }
2142     // remove first, as one might come back to a square the other left
2143     gs->board[m->toFile  ][rank] = NOPIECE; // King toSqr
2144     gs->board[m->toRank  ][rank] = NOPIECE; // Rook toSqr
2145     gs->board[m->fromRank][rank] = ROOK | m->color; // Rook fromSqr
2146     gs->board[kingFromFile][rank] = KING | m->color; // King fromSquare
2147     goto cleanupMove;
2148   }
2149   gs->board[m->fromFile][m->fromRank] = gs->board[m->toFile][m->toRank];
2150   if (m->piecePromotionTo != NOPIECE) {
2151     gs->board[m->fromFile][m->fromRank] = PAWN |
2152       colorval(gs->board[m->fromFile][m->fromRank]);
2153   }
2154   /******************
2155      When takeback a _first_ move of rook, the ??rmoved variable
2156      must be cleared . To check, if the move is first, we should
2157      scan moveList.
2158   *******************/
2159   if (piecetype(gs->board[m->fromFile][m->fromRank]) == ROOK) {
2160     if (m->color == WHITE) {
2161       if ((m->fromFile == -gs->wqrmoved-2) && (m->fromRank == 0)) {
2162         for (i = 2; i < game_globals.garray[g].numHalfMoves - 1; i += 2) {
2163           m1 = (mode==REL_GAME) ? &game_globals.garray[g].moveList[i] : &game_globals.garray[g].examMoveList[i];
2164           if ((m1->fromFile == -gs->wqrmoved-2) && (m1->fromRank == 0))
2165             break;
2166         }
2167         if (i == game_globals.garray[g].numHalfMoves - 1)
2168           gs->wqrmoved = m->fromFile;
2169       }
2170       if ((m->fromFile == -gs->wkrmoved-2) && (m->fromRank == 0)) {
2171         for (i = 2; i < game_globals.garray[g].numHalfMoves - 1; i += 2) {
2172           m1 = (mode==REL_GAME) ? &game_globals.garray[g].moveList[i] : &game_globals.garray[g].examMoveList[i];
2173           if ((m1->fromFile == -gs->wkrmoved-2) && (m1->fromRank == 0))
2174             break;
2175         }
2176         if (i == game_globals.garray[g].numHalfMoves - 1)
2177           gs->wkrmoved = m->fromFile;
2178       }
2179     } else {
2180       if ((m->fromFile == -gs->bqrmoved-2) && (m->fromRank == gs->ranks-1)) {
2181         for (i = 3; i < game_globals.garray[g].numHalfMoves - 1; i += 2) {
2182           m1 = (mode==REL_GAME) ? &game_globals.garray[g].moveList[i] : &game_globals.garray[g].examMoveList[i];
2183           if ((m1->fromFile == -gs->bkrmoved-2) && (m1->fromRank == gs->ranks-1))
2184             break;
2185         }
2186         if (i == game_globals.garray[g].numHalfMoves - 1)
2187           gs->bqrmoved = m->fromFile;
2188       }
2189       if ((m->fromFile == -gs->bkrmoved-2) && (m->fromRank == gs->ranks-1)) {
2190         for (i = 3; i < game_globals.garray[g].numHalfMoves - 1; i += 2) {
2191           m1 = (mode==REL_GAME) ? &game_globals.garray[g].moveList[i] : &game_globals.garray[g].examMoveList[i];
2192           if ((m1->fromFile == -gs->wkrmoved-2) && (m1->fromRank == gs->ranks-1))
2193             break;
2194         }
2195         if (i == game_globals.garray[g].numHalfMoves - 1)
2196           gs->bkrmoved = m->fromFile;
2197       }
2198     }
2199   }
2200   if (piecetype(gs->board[m->fromFile][m->fromRank]) == KING) {
2201     gs->board[m->toFile][m->toRank] = m->pieceCaptured;
2202 #if 0
2203     /* [HGM] castlings are already intercepted due to new format; this code wrecks knightmate! */
2204     if (m->toFile - m->fromFile == 2) {
2205       gs->board[7][m->fromRank] = ROOK |
2206         colorval(gs->board[m->fromFile][m->fromRank]);
2207       gs->board[5][m->fromRank] = NOPIECE;
2208
2209       /********
2210          If takeback a castling, the appropriates ??moved variables
2211          must be cleared
2212       ********/
2213       if (m->color == WHITE) {
2214         gs->wkmoved = 0;
2215         gs->wkrmoved = 0;
2216       } else {
2217         gs->bkmoved = 0;
2218         gs->bkrmoved = 0;
2219       }
2220       goto cleanupMove;
2221     }
2222     if (m->fromFile - m->toFile == 2) {
2223       gs->board[0][m->fromRank] = ROOK |
2224         colorval(gs->board[m->fromFile][m->fromRank]);
2225       gs->board[3][m->fromRank] = NOPIECE;
2226
2227       /**********
2228          If takeback a castling, the appropriate ??moved variables
2229          must be cleared
2230       ***********/
2231       if (m->color == WHITE) {
2232         gs->wkmoved = 0;
2233         gs->wqrmoved = 0;
2234       } else {
2235         gs->bkmoved = 0;
2236         gs->bqrmoved = 0;
2237       }
2238       goto cleanupMove;
2239     }
2240 #endif
2241     /******************
2242        When takeback a _first_ move of king (not the castling),
2243        the ?kmoved variable must be cleared . To check, if the move is first,
2244        we should scan moveList.
2245     *******************/
2246
2247     if (m->color == WHITE) {
2248
2249       if ((m->fromFile == -gs->wkmoved-2) && (m->fromRank == 0)) {
2250         for (i = 2; i < game_globals.garray[g].numHalfMoves - 1; i += 2) {
2251           m1 = (mode==REL_GAME) ? &game_globals.garray[g].moveList[i] : &game_globals.garray[g].examMoveList[i];
2252           if ((m1->fromFile == gs->wkmoved-2) && (m1->fromRank == 0))
2253             break;
2254         }
2255         if (i == game_globals.garray[g].numHalfMoves - 1)
2256           gs->wkmoved = m->fromFile;
2257       }
2258     } else {
2259       if ((m->fromFile == -gs->bkmoved-2) && (m->fromRank == gs->ranks-1)) {
2260         for (i = 3; i < game_globals.garray[g].numHalfMoves - 1; i += 2) {
2261           m1 = (mode==REL_GAME) ? &game_globals.garray[g].moveList[i] : &game_globals.garray[g].examMoveList[i];
2262           if ((m1->fromFile == -gs->bkmoved-2) && (m1->fromRank == gs->ranks-1))
2263             break;
2264         }
2265         if (i == game_globals.garray[g].numHalfMoves - 1)
2266           gs->bkmoved = m->fromFile;
2267       }
2268     }
2269   }
2270   if (m->enPassant) {           /* Do enPassant */
2271     gs->board[m->toFile][m->fromRank] = PAWN |
2272       (colorval(gs->board[m->fromFile][m->fromRank]) == WHITE ? BLACK : WHITE);
2273     gs->board[m->toFile][m->toRank] = NOPIECE;
2274     /* Should set the enpassant array, but I don't care right now */
2275     goto cleanupMove;
2276   }
2277   gs->board[m->toFile][m->toRank] = m->pieceCaptured;
2278   if(gs->board[m->fromFile][m->fromRank] != NOPIECE) { // [HGM] from-square occupied, must have been Seirawan-style gating
2279     gs->holding[gs->onMove==WHITE ? 1 : 0][piecetype(gs->board[m->fromFile][m->fromRank])-1]++; // put back in holdings (onMove not flipped yet!)
2280   }
2281 cleanupMove:
2282   if (game_globals.garray[g].status != GAME_EXAMINE) {
2283     game_update_time(g);
2284   }
2285   game_globals.garray[g].numHalfMoves--;
2286   if (game_globals.garray[g].status != GAME_EXAMINE) {
2287     if (game_globals.garray[g].wInitTime) {     /* Don't update times in untimed games */
2288       now = tenth_secs();
2289
2290       if (m->color == WHITE) {
2291         if (net_globals.con[player_globals.parray[game_globals.garray[g].white].socket]->timeseal) {  /* white uses timeseal? */      
2292           game_globals.garray[g].wRealTime += (m->tookTime * 100); 
2293           game_globals.garray[g].wRealTime -= (game_globals.garray[g].wIncrement * 100);
2294           game_globals.garray[g].wTime = game_globals.garray[g].wRealTime / 100;
2295           if (net_globals.con[player_globals.parray[game_globals.garray[g].black].socket]->timeseal) { /* opp uses timeseal? */
2296             game_globals.garray[g].bTime = game_globals.garray[g].bRealTime / 100;
2297           } else {    /* opp has no timeseal */
2298             game_globals.garray[g].bTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
2299           }
2300         } else {  /* white has no timeseal */
2301           game_globals.garray[g].wTime += m->tookTime;
2302           game_globals.garray[g].wTime -= game_globals.garray[g].wIncrement;
2303           if (net_globals.con[player_globals.parray[game_globals.garray[g].black].socket]->timeseal) { /* opp uses timeseal? */
2304             game_globals.garray[g].bTime = game_globals.garray[g].bRealTime / 100;
2305           } else {    /* opp has no timeseal */
2306             game_globals.garray[g].bTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
2307           }
2308         }
2309       } else {
2310         if (net_globals.con[player_globals.parray[game_globals.garray[g].black].socket]->timeseal) {  /* black uses timeseal? */
2311           game_globals.garray[g].bRealTime += (m->tookTime * 100);
2312           game_globals.garray[g].bRealTime -= (game_globals.garray[g].wIncrement * 100);
2313           game_globals.garray[g].bTime = game_globals.garray[g].bRealTime / 100;
2314           if (net_globals.con[player_globals.parray[game_globals.garray[g].white].socket]->timeseal) { /* opp uses timeseal? */
2315             game_globals.garray[g].wTime = game_globals.garray[g].wRealTime / 100;
2316           } else {    /* opp has no timeseal */
2317             game_globals.garray[g].wTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
2318           }
2319         } else {  /* black has no timeseal */
2320           game_globals.garray[g].bTime += m->tookTime;
2321           if (!game_globals.garray[g].bIncrement)
2322             game_globals.garray[g].bTime -= game_globals.garray[g].wIncrement;
2323           else
2324             game_globals.garray[g].bTime -= game_globals.garray[g].bIncrement;
2325           if (net_globals.con[player_globals.parray[game_globals.garray[g].white].socket]->timeseal) { /* opp uses timeseal? */
2326             game_globals.garray[g].wTime = game_globals.garray[g].wRealTime / 100;
2327           } else {    /* opp has no timeseal */
2328             game_globals.garray[g].wTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
2329           }
2330         }
2331       }
2332
2333       if (game_globals.garray[g].numHalfMoves == 0)
2334         game_globals.garray[g].timeOfStart = now;
2335       game_globals.garray[g].lastMoveTime = now;
2336       game_globals.garray[g].lastDecTime = now;
2337     }
2338   }
2339   if (gs->onMove == BLACK)
2340     gs->onMove = WHITE;
2341   else {
2342     gs->onMove = BLACK;
2343     gs->moveNum--;
2344   }
2345
2346   /******* Here begins the patch : ********************************
2347      Takeback of last move is done already, it's time to update enpassant
2348      array.  (patch from Soso, added by Sparky 3/17/95)
2349   ********/
2350
2351   if (game_globals.garray[g].numHalfMoves > 0) {
2352     m1 = (mode==REL_GAME) ? &game_globals.garray[g].moveList[game_globals.garray[g].numHalfMoves - 1] : 
2353                             &game_globals.garray[g].examMoveList[game_globals.garray[g].numHalfMoves - 1];
2354     if (piecetype(gs->board[m1->toFile][m1->toRank]) == PAWN) {
2355       if ((m1->toRank - m1->fromRank) == 2) {
2356         if ((m1->toFile < gs->files-1) && gs->board[m1->toFile + 1][m1->toRank] == B_PAWN) {
2357           gs->ep_possible[1][m1->toFile + 1] = -1;
2358         }
2359         if ((m1->toFile - 1 >= 0) && gs->board[m1->toFile - 1][m1->toRank] == B_PAWN) {
2360           gs->ep_possible[1][m1->toFile - 1] = 1;
2361         }
2362       }
2363       if ((m1->toRank - m1->fromRank) == -2) {
2364         if ((m1->toFile < gs->files-1) && gs->board[m1->toFile + 1][m1->toRank] == W_PAWN) {
2365           gs->ep_possible[0][m1->toFile + 1] = -1;
2366         }
2367         if ((m1->toFile - 1 >= 0) && gs->board[m1->toFile - 1][m1->toRank] == W_PAWN) {
2368           gs->ep_possible[0][m1->toFile - 1] = 1;
2369         }
2370       }
2371     }
2372   }
2373   /************** and here's the end **************/
2374   return MOVE_OK;
2375 }
2376
2377
2378
2379