2 Copyright (c) 1993 Richard V. Nash.
3 Copyright (c) 2000 Dan Papasian
4 Copyright (C) Andrew Tridgell 2002
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.
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.
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.
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() ).
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 */
41 /* MS_COMPDASH e2-e4 */
42 /* MS_CASTLE o-o, o-o-o */
44 /* MS_ALG e4, Nd5 Ncd5 */
45 int is_move(const char *mstr)
47 int len = strlen(mstr);
48 if ((len > 3) && (mstr[len - 2] == '='))
51 /* remove the 'mates' marker */
52 if (mstr[len - 1] == '#')
55 if (len == 4) { /* Test for e2e4 */
56 if (isfile(mstr[0]) && isrank(mstr[1]) &&
57 isfile(mstr[2]) && isrank(mstr[3])) {
61 if (len == 5) { /* Test for e2-e4 */
62 if (isfile(mstr[0]) && isrank(mstr[1]) &&
64 isfile(mstr[3]) && isrank(mstr[4])) {
68 if (len == 3) { /* Test for o-o */
69 if ((mstr[0] == 'o') && (mstr[1] == '-') && (mstr[2] == 'o')) {
72 if ((mstr[0] == 'O') && (mstr[1] == '-') && (mstr[2] == 'O')) {
75 if ((mstr[0] == '0') && (mstr[1] == '-') && (mstr[2] == '0')) {
79 if (len == 2) { /* Test for oo */
80 if ((mstr[0] == 'o') && (mstr[1] == 'o')) {
83 if ((mstr[0] == 'O') && (mstr[1] == 'O')) {
86 if ((mstr[0] == '0') && (mstr[1] == '0')) {
90 if (len == 5) { /* Test for o-o-o */
91 if ((mstr[0] == 'o') && (mstr[1] == '-') && (mstr[2] == 'o') && (mstr[3] == '-') && (mstr[4] == 'o')) {
94 if ((mstr[0] == 'O') && (mstr[1] == '-') && (mstr[2] == 'O') && (mstr[3] == '-') && (mstr[4] == 'O')) {
97 if ((mstr[0] == '0') && (mstr[1] == '-') && (mstr[2] == '0') && (mstr[3] == '-') && (mstr[4] == '0')) {
101 if (len == 3) { /* Test for ooo */
102 if ((mstr[0] == 'o') && (mstr[1] == 'o') && (mstr[2] == 'o')) {
105 if ((mstr[0] == 'O') && (mstr[1] == 'O') && (mstr[2] == 'O')) {
108 if ((mstr[0] == '0') && (mstr[1] == '0') && (mstr[2] == '0')) {
112 return alg_is_move(mstr);
116 int NextPieceLoop(board_t b, int *f, int *r, int color, int w, int h)
126 if ((b[*f][*r] != NOPIECE) && iscolor(b[*f][*r], color))
132 int InitPieceLoop(board_t b, int *f, int *r, int color)
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 )
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;
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;
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))
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;
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))
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;
181 static int legal_knight_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
198 static int legal_horse_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
205 if (abs(dy) == 1 && gs->board[(ff+tf)/2][fr] == NOPIECE)
209 if (abs(dx) == 1 && gs->board[ff][(fr+tr)/2] == NOPIECE)
215 static int legal_honorablehorse_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
221 if (dy == (gs->onMove == WHITE ? -2 : 2)) {
228 static int legal_bishop_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
252 return 0; /* Not diagonal */
254 return 1; /* One square, ok */
256 for (x = startx, y = starty; count; x += incx, y += incy, count--) {
257 if (gs->board[x][y] != NOPIECE)
263 static int legal_rook_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
269 if (abs(fr - tr) == 1)
278 for (i = start; i <= stop; i++) {
279 if (gs->board[ff][i] != NOPIECE)
283 } else if (fr == tr) {
284 if (abs(ff - tf) == 1)
293 for (i = start; i <= stop; i++) {
294 if (gs->board[i][fr] != NOPIECE)
303 static int legal_cannon_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
316 for (i = start; i <= stop; i++) {
317 if (gs->board[ff][i] != NOPIECE) cnt++;
319 return (cnt == 0 && gs->board[tf][tr] == NOPIECE) ||
320 (cnt == 1 && gs->board[tf][tr] != NOPIECE);
321 } else if (fr == tr) {
329 for (i = start; i <= stop; i++) {
330 if (gs->board[i][fr] != NOPIECE) cnt++;
332 return (cnt == 0 && gs->board[tf][tr] == NOPIECE) ||
333 (cnt == 1 && gs->board[tf][tr] != NOPIECE);
339 static int legal_lance_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
345 if (abs(fr - tr) == 1)
348 if(gs->onMove != WHITE) return 0;
352 if(gs->onMove == WHITE) return 0;
356 for (i = start; i <= stop; i++) {
357 if (gs->board[ff][i] != NOPIECE)
364 static int legal_queen_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
366 return legal_rook_move(gs, ff, fr, tf, tr) || legal_bishop_move(gs, ff, fr, tf, tr);
369 static int legal_cardinal_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
371 return legal_knight_move(gs, ff, fr, tf, tr) || legal_bishop_move(gs, ff, fr, tf, tr);
374 static int legal_marshall_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
376 return legal_rook_move(gs, ff, fr, tf, tr) || legal_knight_move(gs, ff, fr, tf, tr);
379 /* Ckeck, if square (kf,kr) is attacked by enemy piece.
380 * Used in castling from/through check testing.
383 /* new one from soso: */
384 static int is_square_attacked (struct game_state_t *gs, int kf, int kr)
386 struct game_state_t fakeMove;
387 int oldk = gs->onMove == WHITE ? gs->wkmoved : gs->bkmoved;
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;
398 static int is_square_attacked(struct game_state_t * gs, int kf, int kr)
401 gs->onMove = CToggle(gs->onMove);
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);
410 gs->onMove = CToggle(gs->onMove);
415 static int legal_man_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
417 if (abs(ff - tf) > 1)
419 if (abs(fr - tr) > 1)
424 static int legal_wazir_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
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))
429 if (abs(ff - tf) == 1 && fr == tr)
431 if (abs(fr - tr) == 1 && ff == tf)
436 static int legal_dababba_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
438 if (abs(ff - tf) == 2 && fr == tr)
440 if (abs(fr - tr) == 2 && ff == tf)
445 static int legal_ferz_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
447 if (abs(ff - tf) != 1)
449 if (abs(fr - tr) != 1)
454 static int legal_mandarin_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
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))
459 if (abs(ff - tf) != 1)
461 if (abs(fr - tr) != 1)
466 static int legal_alfil_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
468 if (abs(ff - tf) != 2)
470 if (abs(fr - tr) != 2)
475 static int legal_elephant_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
477 if (abs(ff - tf) != 2)
479 if (abs(fr - tr) != 2)
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
486 static int legal_gold_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
488 return legal_wazir_move(gs, ff, fr, tf, tr) || (abs(ff-tf) == 1 && tr == fr + (gs->onMove==WHITE ? 1 : -1));
491 static int legal_silver_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
493 return legal_ferz_move(gs, ff, fr, tf, tr) || (tf == ff && tr == fr + (gs->onMove==WHITE ? 1 : -1) );
496 static int legal_woody_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
498 return legal_wazir_move(gs, ff, fr, tf, tr) || legal_dababba_move(gs, ff, fr, tf, tr);
501 static int legal_squirrel_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
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);
507 static int legal_mastodon_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
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);
513 static int legal_centaur_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
515 return legal_man_move(gs, ff, fr, tf, tr) || legal_knight_move(gs, ff, fr, tf, tr);
518 static int legal_amazon_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
520 return legal_queen_move(gs, ff, fr, tf, tr) || legal_knight_move(gs, ff, fr, tf, tr);
523 static int legal_dragonking_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
525 return legal_rook_move(gs, ff, fr, tf, tr) || legal_ferz_move(gs, ff, fr, tf, tr);
528 static int legal_dragonhorse_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
530 return legal_bishop_move(gs, ff, fr, tf, tr) || legal_wazir_move(gs, ff, fr, tf, tr);
533 static int legal_modernelephant_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
535 return legal_ferz_move(gs, ff, fr, tf, tr) || legal_alfil_move(gs, ff, fr, tf, tr);
538 static int legal_priestess_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
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);
544 static int legal_minister_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
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);
550 static int legal_king_move(struct game_state_t * gs, int ff, int fr, int tf, int tr)
554 // [HGM] castle: test first if valid as regular King move; result = 1 or 0
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
566 result = legal_man_move(gs, ff, fr, tf, tr);
569 // [HGM] castle: orthodox legal castlings given as King move return 2
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))) {
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))) {
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))) {
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))) {
609 return 0; // neither regular King move nor castling
612 static void add_pos(int tof, int tor, int *posf, int *posr, int *numpos)
619 static void possible_pawn_moves(struct game_state_t * gs,
621 int *posf, int *posr, int *numpos)
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);
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
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
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);
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);
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
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
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);
678 static void possible_knight_moves(struct game_state_t * gs,
680 int *posf, int *posr, int *numpos)
682 static int knightJumps[8][2] = {{-1, 2}, {1, 2}, {2, -1}, {2, 1},
683 {-1, -2}, {1, -2}, {-2, 1}, {-2, -1}};
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))
692 if ((r < 0) || (r >= gs->ranks))
694 if ((gs->board[f][r] == NOPIECE) ||
695 (iscolor(gs->board[f][r], CToggle(gs->onMove))))
696 add_pos(f, r, posf, posr, numpos);
700 static void possible_horse_moves(struct game_state_t * gs,
702 int *posf, int *posr, int *numpos)
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}};
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))
714 if ((r < 0) || (r >= gs->ranks))
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);
722 static void possible_bishop_moves(struct game_state_t * gs,
724 int *posf, int *posr, int *numpos)
734 if ((f < 0) || (f >= gs->files))
736 if ((r < 0) || (r >= gs->ranks))
738 if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
740 add_pos(f, r, posf, posr, numpos);
741 if (gs->board[f][r] != NOPIECE)
750 if ((f < 0) || (f >= gs->files))
752 if ((r < 0) || (r >= gs->ranks))
754 if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
756 add_pos(f, r, posf, posr, numpos);
757 if (gs->board[f][r] != NOPIECE)
766 if ((f < 0) || (f >= gs->files))
768 if ((r < 0) || (r >= gs->ranks))
770 if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
772 add_pos(f, r, posf, posr, numpos);
773 if (gs->board[f][r] != NOPIECE)
782 if ((f < 0) || (f >= gs->files))
784 if ((r < 0) || (r >= gs->ranks))
786 if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
788 add_pos(f, r, posf, posr, numpos);
789 if (gs->board[f][r] != NOPIECE)
794 static void possible_rook_moves(struct game_state_t * gs,
796 int *posf, int *posr, int *numpos)
805 if ((f < 0) || (f >= gs->files))
807 if ((r < 0) || (r >= gs->ranks))
809 if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
811 add_pos(f, r, posf, posr, numpos);
812 if (gs->board[f][r] != NOPIECE)
820 if ((f < 0) || (f >= gs->files))
822 if ((r < 0) || (r >= gs->ranks))
824 if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
826 add_pos(f, r, posf, posr, numpos);
827 if (gs->board[f][r] != NOPIECE)
835 if ((f < 0) || (f >= gs->files))
837 if ((r < 0) || (r >= gs->ranks))
839 if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
841 add_pos(f, r, posf, posr, numpos);
842 if (gs->board[f][r] != NOPIECE)
850 if ((f < 0) || (f >= gs->files))
852 if ((r < 0) || (r >= gs->ranks))
854 if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
856 add_pos(f, r, posf, posr, numpos);
857 if (gs->board[f][r] != NOPIECE)
862 static void possible_cannon_moves(struct game_state_t * gs,
864 int *posf, int *posr, int *numpos)
873 if ((f < 0) || (f >= gs->files))
875 if ((r < 0) || (r >= gs->ranks))
877 if ((gs->board[f][r] != NOPIECE) && i++ == 0) continue;
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)
890 if ((f < 0) || (f >= gs->files))
892 if ((r < 0) || (r >= gs->ranks))
894 if ((gs->board[f][r] != NOPIECE) && i++ == 0) continue;
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)
907 if ((f < 0) || (f >= gs->files))
909 if ((r < 0) || (r >= gs->ranks))
911 if ((gs->board[f][r] != NOPIECE) && i++ == 0) continue;
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)
924 if ((f < 0) || (f >= gs->files))
926 if ((r < 0) || (r >= gs->ranks))
928 if ((gs->board[f][r] != NOPIECE) && i++ == 0) continue;
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)
938 static void possible_lance_moves(struct game_state_t * gs,
940 int *posf, int *posr, int *numpos)
947 for (;gs->onMove == WHITE;) {
949 if ((f < 0) || (f >= gs->files))
951 if ((r < 0) || (r >= gs->ranks))
953 if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
955 add_pos(f, r, posf, posr, numpos);
956 if (gs->board[f][r] != NOPIECE)
962 for (;gs->onMove == BLACK;) {
964 if ((f < 0) || (f >= gs->files))
966 if ((r < 0) || (r >= gs->ranks))
968 if ((gs->board[f][r] != NOPIECE) && (iscolor(gs->board[f][r], gs->onMove)))
970 add_pos(f, r, posf, posr, numpos);
971 if (gs->board[f][r] != NOPIECE)
976 static void possible_cardinal_moves(struct game_state_t * gs,
978 int *posf, int *posr, int *numpos)
980 possible_knight_moves(gs, onf, onr, posf, posr, numpos);
981 possible_bishop_moves(gs, onf, onr, posf, posr, numpos);
984 static void possible_marshall_moves(struct game_state_t * gs,
986 int *posf, int *posr, int *numpos)
988 possible_rook_moves(gs, onf, onr, posf, posr, numpos);
989 possible_knight_moves(gs, onf, onr, posf, posr, numpos);
992 static void possible_queen_moves(struct game_state_t * gs,
994 int *posf, int *posr, int *numpos)
996 possible_rook_moves(gs, onf, onr, posf, posr, numpos);
997 possible_bishop_moves(gs, onf, onr, posf, posr, numpos);
1000 static void possible_alfil_moves(struct game_state_t * gs,
1002 int *posf, int *posr, int *numpos)
1004 static int kingJumps[4][2] = {{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
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))
1013 if ((r < 0) || (r >= gs->ranks))
1015 if ((gs->board[f][r] == NOPIECE) ||
1016 (iscolor(gs->board[f][r], CToggle(gs->onMove))))
1017 add_pos(f, r, posf, posr, numpos);
1021 static void possible_ferz_moves(struct game_state_t * gs,
1023 int *posf, int *posr, int *numpos)
1025 static int kingJumps[4][2] = {{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
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))
1034 if ((r < 0) || (r >= gs->ranks))
1036 if ((gs->board[f][r] == NOPIECE) ||
1037 (iscolor(gs->board[f][r], CToggle(gs->onMove))))
1038 add_pos(f, r, posf, posr, numpos);
1042 static void possible_mandarin_moves(struct game_state_t * gs,
1044 int *posf, int *posr, int *numpos)
1046 static int kingJumps[4][2] = {{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
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))
1055 if ((r < 0) || (r >= gs->ranks))
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))
1060 if ((gs->board[f][r] == NOPIECE) ||
1061 (iscolor(gs->board[f][r], CToggle(gs->onMove))))
1062 add_pos(f, r, posf, posr, numpos);
1066 static void possible_wazir_moves(struct game_state_t * gs,
1068 int *posf, int *posr, int *numpos)
1070 static int kingJumps[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
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))
1079 if ((r < 0) || (r >= gs->ranks))
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))
1084 if ((gs->board[f][r] == NOPIECE) ||
1085 (iscolor(gs->board[f][r], CToggle(gs->onMove))))
1086 add_pos(f, r, posf, posr, numpos);
1090 static void possible_dababba_moves(struct game_state_t * gs,
1092 int *posf, int *posr, int *numpos)
1094 static int kingJumps[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
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))
1103 if ((r < 0) || (r >= gs->ranks))
1105 if ((gs->board[f][r] == NOPIECE) ||
1106 (iscolor(gs->board[f][r], CToggle(gs->onMove))))
1107 add_pos(f, r, posf, posr, numpos);
1111 static void possible_man_moves(struct game_state_t * gs,
1113 int *posf, int *posr, int *numpos)
1115 possible_wazir_moves(gs, onf, onr, posf, posr, numpos);
1116 possible_ferz_moves(gs, onf, onr, posf, posr, numpos);
1119 static void possible_dragonking_moves(struct game_state_t * gs,
1121 int *posf, int *posr, int *numpos)
1123 possible_rook_moves(gs, onf, onr, posf, posr, numpos);
1124 possible_ferz_moves(gs, onf, onr, posf, posr, numpos);
1127 static void possible_dragonhorse_moves(struct game_state_t * gs,
1129 int *posf, int *posr, int *numpos)
1131 possible_wazir_moves(gs, onf, onr, posf, posr, numpos);
1132 possible_bishop_moves(gs, onf, onr, posf, posr, numpos);
1135 static void possible_centaur_moves(struct game_state_t * gs,
1137 int *posf, int *posr, int *numpos)
1139 possible_man_moves(gs, onf, onr, posf, posr, numpos);
1140 possible_knight_moves(gs, onf, onr, posf, posr, numpos);
1143 static void possible_woody_moves(struct game_state_t * gs,
1145 int *posf, int *posr, int *numpos)
1147 possible_wazir_moves(gs, onf, onr, posf, posr, numpos);
1148 possible_dababba_moves(gs, onf, onr, posf, posr, numpos);
1151 static void possible_squirrel_moves(struct game_state_t * gs,
1153 int *posf, int *posr, int *numpos)
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);
1160 static void possible_mastodon_moves(struct game_state_t * gs,
1162 int *posf, int *posr, int *numpos)
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);
1169 static void possible_amazon_moves(struct game_state_t * gs,
1171 int *posf, int *posr, int *numpos)
1173 possible_queen_moves(gs, onf, onr, posf, posr, numpos);
1174 possible_knight_moves(gs, onf, onr, posf, posr, numpos);
1177 static void possible_modernelephant_moves(struct game_state_t * gs,
1179 int *posf, int *posr, int *numpos)
1181 possible_ferz_moves(gs, onf, onr, posf, posr, numpos);
1182 possible_alfil_moves(gs, onf, onr, posf, posr, numpos);
1185 static void possible_priestess_moves(struct game_state_t * gs,
1187 int *posf, int *posr, int *numpos)
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);
1194 static void possible_minister_moves(struct game_state_t * gs,
1196 int *posf, int *posr, int *numpos)
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);
1203 static void possible_gold_moves(struct game_state_t * gs,
1205 int *posf, int *posr, int *numpos)
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);
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);
1223 static void possible_silver_moves(struct game_state_t * gs,
1225 int *posf, int *posr, int *numpos)
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);
1232 if(onr > 0 && !iscolor(gs->board[onf][onr-1], BLACK))
1233 add_pos(onf, onr-1, posf, posr, numpos);
1237 static void possible_honorablehorse_moves(struct game_state_t * gs,
1239 int *posf, int *posr, int *numpos)
1241 int f, r = onr + (gs->onMove == WHITE ? 2 : -2);
1243 if(r < 0 || r >= gs->ranks) return;
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);
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);
1256 static void possible_king_moves(struct game_state_t * gs,
1258 int *posf, int *posr, int *numpos)
1261 possible_knight_moves(gs, onf, onr, posf, posr, numpos);
1263 possible_wazir_moves(gs, onf, onr, posf, posr, numpos);
1265 possible_man_moves(gs, onf, onr, posf, posr, numpos);
1268 static void possible_elephant_moves(struct game_state_t * gs,
1270 int *posf, int *posr, int *numpos)
1272 static int kingJumps[4][2] = {{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
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))
1281 if ((r < 0) || (r >= gs->ranks))
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);
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)
1297 if (fFile == ALG_DROP) {
1299 if(gs->drops != 1) return 0; // [HGM] variants: no drops in this variant
1300 if (move_piece == KING)
1302 if (gs->holding[gs->onMove==WHITE ? 0 : 1][move_piece-1] == 0)
1304 if (gs->board[tFile][tRank] != NOPIECE)
1306 if (gs->promoType == 3) { // Shogi
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;
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;
1321 if (move_piece == PAWN && (tRank == 0 || tRank == gs->ranks-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;
1334 kRook = gs->wkrmoved;
1335 qRook = gs->wqrmoved;
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;
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
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;
1353 leftEmpty = tRank < fKing ? tRank+1 : fKing+1;
1354 rightEmpty = fRank < tFile ? fRank-1 : tFile-1;
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;
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;
1364 return 1; // passed all tests
1366 move_piece = piecetype(gs->board[fFile][fRank]);
1368 if (gs->board[fFile][fRank] == NOPIECE)
1370 if (!iscolor(gs->board[fFile][fRank], gs->onMove)) /* Wrong color */
1372 if ((gs->board[tFile][tRank] != NOPIECE) &&
1373 iscolor(gs->board[tFile][tRank], gs->onMove)) /* Can't capture own */
1375 if ((fFile == tFile) && (fRank == tRank)) /* Same square */
1377 switch (move_piece) {
1379 legal = legal_pawn_move(gs, fFile, fRank, tFile, tRank);
1382 legal = legal_knight_move(gs, fFile, fRank, tFile, tRank);
1385 legal = legal_bishop_move(gs, fFile, fRank, tFile, tRank);
1388 legal = legal_rook_move(gs, fFile, fRank, tFile, tRank);
1393 legal = legal_cardinal_move(gs, fFile, fRank, tFile, tRank);
1398 legal = legal_marshall_move(gs, fFile, fRank, tFile, tRank);
1402 legal = legal_man_move(gs, fFile, fRank, tFile, tRank);
1405 legal = legal_queen_move(gs, fFile, fRank, tFile, tRank);
1408 legal = legal_elephant_move(gs, fFile, fRank, tFile, tRank);
1411 legal = legal_amazon_move(gs, fFile, fRank, tFile, tRank);
1414 legal = legal_woody_move(gs, fFile, fRank, tFile, tRank);
1417 legal = legal_squirrel_move(gs, fFile, fRank, tFile, tRank);
1420 legal = legal_mastodon_move(gs, fFile, fRank, tFile, tRank);
1423 legal = legal_centaur_move(gs, fFile, fRank, tFile, tRank);
1426 legal = legal_horse_move(gs, fFile, fRank, tFile, tRank);
1430 legal = legal_ferz_move(gs, fFile, fRank, tFile, tRank);
1433 legal = legal_mandarin_move(gs, fFile, fRank, tFile, tRank);
1436 legal = legal_wazir_move(gs, fFile, fRank, tFile, tRank);
1440 legal = legal_alfil_move(gs, fFile, fRank, tFile, tRank);
1442 case MODERNELEPHANT:
1443 legal = legal_modernelephant_move(gs, fFile, fRank, tFile, tRank);
1446 legal = legal_priestess_move(gs, fFile, fRank, tFile, tRank);
1449 legal = legal_minister_move(gs, fFile, fRank, tFile, tRank);
1452 legal = legal_silver_move(gs, fFile, fRank, tFile, tRank);
1455 legal = legal_gold_move(gs, fFile, fRank, tFile, tRank);
1458 legal = legal_lance_move(gs, fFile, fRank, tFile, tRank);
1461 legal = legal_cannon_move(gs, fFile, fRank, tFile, tRank);
1464 legal = legal_dragonhorse_move(gs, fFile, fRank, tFile, tRank);
1467 legal = legal_dragonking_move(gs, fFile, fRank, tFile, tRank);
1469 case HONORABLEHORSE:
1470 legal = legal_honorablehorse_move(gs, fFile, fRank, tFile, tRank);
1473 legal = legal_king_move(gs, fFile, fRank, tFile, tRank);
1482 #define DROP_CHAR '@'
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)
1488 struct game_state_t fakeMove;
1491 mt->pieceCaptured = gs->board[mt->toFile][mt->toRank];
1492 mt->enPassant = 0; /* Don't know yet, let execute move take care
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");
1504 if(gs->promoType == 3) { // Shogi-style promotions: not just Pawns, but many pieces can promote
1505 int piece = gs->board[mt->fromFile][mt->fromRank];
1506 mt->piecePromotionTo = NOPIECE;
1507 if(colorval(piece) == WHITE && mt->fromRank < gs->ranks - gs->ranks/3
1508 && mt->toRank < gs->ranks - gs->ranks/3 ||
1509 colorval(piece) == BLACK && mt->fromRank >= gs->ranks/3
1510 && mt->toRank >= gs->ranks/3 )
1511 promote = NOPIECE; // suppress promotion outside zone
1512 if(promote) { // promotion piece determined by original, no matter what was requested
1513 switch(piecetype(piece)) {
1516 case HONORABLEHORSE:
1518 promote = GOLD; break;
1520 promote = DRAGONHORSE; break;
1522 promote = DRAGONKING; break;
1523 default: promote = NOPIECE; // not a promotion
1526 switch(piecetype(piece)) { // force mandatory promotions
1527 case HONORABLEHORSE:
1528 if(mt->toRank == 1 || mt->toRank == gs->files-2) promote = GOLD;
1531 if(mt->toRank == 0 || mt->toRank == gs->files-1) promote = GOLD;
1534 if(promote) mt->piecePromotionTo = promote | (colorval(gs->board[mt->fromFile][mt->fromRank]));
1536 if ((piecetype(gs->board[mt->fromFile][mt->fromRank]) == PAWN) &&
1537 !gs->palace && // [HGM] XQ: no promotions in xiangqi
1538 ((mt->toRank < gs->promoZone) || (mt->toRank >= gs->ranks - gs->promoZone))) {
1539 int stm = colorval(gs->board[mt->fromFile][mt->fromRank]);
1540 if(!promote && (mt->toRank == 0 || mt->toRank == gs->ranks-1)) { // promotion obligatory, but not specified
1541 if(gs->promoType != 2) promote = QUEEN; else { // choose a default
1542 for(promote=PIECES-1; promote>PAWN; promote--) if(gs->holding[stm == BLACK][promote-1]) break;
1543 if(promote == PAWN) return MOVE_ILLEGAL; // nothing available
1545 } // if not obligatory, we defer unless promotion was explicitly specified!
1546 if(!gs->pawnDblStep && promote == PRINCESS) promote = MAN2;
1547 if(!gs->pawnDblStep && promote != FERZ2 && promote != MAN2) promote = FERZ; // [HGM] kludge to recognize shatranj
1548 // non-promotion can still be an option for deeper promotion zones
1549 mt->piecePromotionTo = promote ? (promote | stm) : NOPIECE;
1550 if(promote && gs->promoType == 2 && !gs->holding[stm == BLACK][promote-1]) return MOVE_ILLEGAL; // unavailable piece specified
1551 } else if(gs->drops == 2 && promote && mt->fromRank == (stm == WHITE ? 0 : gs->ranks-1)) { // [HGM] Seirawan-style gating
1552 int i; struct game *g = &game_globals.garray[gs->gameNum];
1553 if(!gs->holding[stm == BLACK][promote-1]) return MOVE_ILLEGAL; // unavailable piece specified
1554 // now we must test virginity of the moved piece. Yegh!
1555 for (i = g->numHalfMoves-2; i > 0; i -= 2) {
1556 if (g->moveList[i].toFile == mt->fromFile && g->moveList[i].toRank == mt->fromRank) return MOVE_ILLEGAL;
1558 gating = 1; // gating OK; remember we did it for check test
1560 mt->piecePromotionTo = NOPIECE;
1562 if ((piecetype(gs->board[mt->fromFile][mt->fromRank]) == PAWN) &&
1563 ((mt->fromRank - mt->toRank == 2) || (mt->toRank - mt->fromRank == 2))) {
1564 mt->doublePawn = mt->fromFile;
1566 mt->doublePawn = -1;
1569 if ((piecetype(gs->board[mt->fromFile][mt->fromRank]) == KING) &&
1570 (mt->fromFile == gs->files/2) && (mt->toFile == 2) &&
1571 mt->fromRank == mt->toRank) {
1572 sprintf(mt->moveString, "o-o-o");
1573 } else if ((piecetype(gs->board[mt->fromFile][mt->fromRank]) == KING) &&
1574 (mt->fromFile == gs->files/2) && (mt->toFile == gs->files-2) &&
1575 mt->fromRank == mt->toRank) {
1576 sprintf(mt->moveString, "o-o");
1581 sprintf(mt->moveString, "%s/%c%d-%c%d",
1582 wpstring[piecetype(gs->board[mt->fromFile][mt->fromRank])],
1583 mt->fromFile + 'a', mt->fromRank + 1 - (gs->ranks>9),
1584 mt->toFile + 'a', mt->toRank + 1 - (gs->ranks>9));
1587 /* Replace this with an algabraic de-parser */
1589 sprintf(mt->algString, alg_unparse(gs, mt));
1591 /* Calculates enPassant also */
1592 execute_move(&fakeMove, mt, 0);
1593 if(gating) fakeMove.board[mt-fromFile][mt->fromRank] = NOPIECE; // [HGM] gating is only legal if non-gating move was (weird, but true)
1595 /* Does making this move leave ME in check? */
1596 if (in_check(&fakeMove))
1597 return MOVE_ILLEGAL;
1598 /* IanO: bughouse variants: drop cannot be check/checkmate */
1603 int legal_andcheck_move(struct game_state_t * gs,
1604 int fFile, int fRank,
1605 int tFile, int tRank)
1609 if (!legal_move(gs, fFile, fRank, tFile, tRank))
1611 mt.color = gs->onMove;
1612 mt.fromFile = fFile;
1613 mt.fromRank = fRank;
1616 /* This should take into account a pawn promoting to another piece */
1617 if (move_calculate(gs, &mt, NOPIECE) == MOVE_OK)
1623 /* in_check: checks if the side that is NOT about to move is in check
1625 int in_check(struct game_state_t * gs)
1628 int kf = -1, kr = -1;
1631 if (gs->onMove == WHITE) {
1632 for (f = 0; f < gs->files && kf < 0; f++)
1633 for (r = 0; r < gs->ranks && kf < 0; r++)
1634 if (gs->board[f][r] == B_KING) {
1639 for (f = 0; f < gs->files && kf < 0; f++)
1640 for (r = 0; r < gs->ranks && kf < 0; r++)
1641 if (gs->board[f][r] == W_KING) {
1647 d_printf( "CHESSD: Error game with no king!\n");
1650 for (InitPieceLoop(gs->board, &f, &r, gs->onMove);
1651 NextPieceLoop(gs->board, &f, &r, gs->onMove, gs->files, gs->ranks);) {
1652 if (legal_move(gs, f, r, kf, kr)) { /* In Check? */
1659 int has_legal_move(struct game_state_t * gs)
1664 int possiblef[500], possibler[500];
1665 int numpossible = 0;
1667 for (InitPieceLoop(gs->board, &f, &r, gs->onMove);
1668 NextPieceLoop(gs->board, &f, &r, gs->onMove, gs->files, gs->ranks);) {
1669 switch (piecetype(gs->board[f][r])) {
1671 possible_pawn_moves(gs, f, r, possiblef, possibler, &numpossible);
1674 possible_knight_moves(gs, f, r, possiblef, possibler, &numpossible);
1677 possible_bishop_moves(gs, f, r, possiblef, possibler, &numpossible);
1680 possible_rook_moves(gs, f, r, possiblef, possibler, &numpossible);
1685 possible_cardinal_moves(gs, f, r, possiblef, possibler, &numpossible);
1690 possible_marshall_moves(gs, f, r, possiblef, possibler, &numpossible);
1694 possible_man_moves(gs, f, r, possiblef, possibler, &numpossible);
1697 possible_queen_moves(gs, f, r, possiblef, possibler, &numpossible);
1700 possible_elephant_moves(gs, f, r, possiblef, possibler, &numpossible);
1703 possible_amazon_moves(gs, f, r, possiblef, possibler, &numpossible);
1706 possible_woody_moves(gs, f, r, possiblef, possibler, &numpossible);
1709 possible_squirrel_moves(gs, f, r, possiblef, possibler, &numpossible);
1712 possible_mastodon_moves(gs, f, r, possiblef, possibler, &numpossible);
1715 possible_centaur_moves(gs, f, r, possiblef, possibler, &numpossible);
1718 possible_horse_moves(gs, f, r, possiblef, possibler, &numpossible);
1722 possible_ferz_moves(gs, f, r, possiblef, possibler, &numpossible);
1725 possible_mandarin_moves(gs, f, r, possiblef, possibler, &numpossible);
1728 possible_wazir_moves(gs, f, r, possiblef, possibler, &numpossible);
1732 possible_alfil_moves(gs, f, r, possiblef, possibler, &numpossible);
1734 case MODERNELEPHANT:
1735 possible_modernelephant_moves(gs, f, r, possiblef, possibler, &numpossible);
1738 possible_priestess_moves(gs, f, r, possiblef, possibler, &numpossible);
1741 possible_minister_moves(gs, f, r, possiblef, possibler, &numpossible);
1744 possible_silver_moves(gs, f, r, possiblef, possibler, &numpossible);
1747 possible_gold_moves(gs, f, r, possiblef, possibler, &numpossible);
1750 possible_cannon_moves(gs, f, r, possiblef, possibler, &numpossible);
1753 possible_lance_moves(gs, f, r, possiblef, possibler, &numpossible);
1756 possible_dragonhorse_moves(gs, f, r, possiblef, possibler, &numpossible);
1759 possible_dragonking_moves(gs, f, r, possiblef, possibler, &numpossible);
1761 case HONORABLEHORSE:
1762 possible_honorablehorse_moves(gs, f, r, possiblef, possibler, &numpossible);
1767 possible_king_moves(gs, f, r, possiblef, possibler, &numpossible);
1770 if (numpossible >= 500) {
1771 d_printf( "CHESSD: Possible move overrun\n");
1773 for (i = 0; i < numpossible; i++)
1774 if (legal_andcheck_move(gs, f, r, possiblef[i], possibler[i])) {
1779 /* IanO: if we got here, then kf and kr must be set */
1780 if (gs->gameNum >=0 && game_globals.garray[gs->gameNum].link >= 0
1781 || gs->holdings) { // [HGM] zh: also in 2-player games with drops
1782 /* bughouse: potential drops as check interpositions */
1783 gs->holding[gs->onMove==WHITE ? 0 : 1][QUEEN - 1]++;
1784 for (f=kf-1; f<=kf+1; f++) for (r=kr-1; r<=kr+1; r++) {
1785 if (f>=0 && f<gs->files && r>=0 && r<gs->ranks && gs->board[f][r] == NOPIECE) {
1786 /* try a drop next to the king */
1787 if (legal_andcheck_move(gs, ALG_DROP, QUEEN, f, r)) {
1788 gs->holding[gs->onMove==WHITE ? 0 : 1][QUEEN - 1]--;
1789 // OK, so we have an interposing drop. But do we have something to drop?
1790 if(game_globals.garray[gs->gameNum].link < 0) {
1791 // we have no partner, so we must have something to drop now
1792 for(i=QUEEN; i>=PAWN; i--)
1793 if (legal_andcheck_move(gs, ALG_DROP, i, f, r)) return 1;
1800 gs->holding[gs->onMove==WHITE ? 0 : 1][QUEEN - 1]--;
1806 /* This will end up being a very complicated function */
1807 int parse_move(char *mstr, struct game_state_t * gs, struct move_t * mt, int promote)
1809 int type = is_move(mstr);
1812 mt->piecePromotionTo = NOPIECE;
1813 mt->color = gs->onMove;
1816 return MOVE_ILLEGAL;
1819 mt->fromFile = mstr[0] - 'a';
1820 mt->fromRank = mstr[1] - '1' + (gs->ranks>9);
1821 mt->toFile = mstr[2] - 'a';
1822 mt->toRank = mstr[3] - '1' + (gs->ranks>9);
1825 mt->fromFile = mstr[0] - 'a';
1826 mt->fromRank = mstr[1] - '1' + (gs->ranks>9);
1827 mt->toFile = mstr[3] - 'a';
1828 mt->toRank = mstr[4] - '1' + (gs->ranks>9);
1832 mt->fromFile = gs->files/2;
1833 mt->toFile = gs->files-2;
1834 if (gs->onMove == WHITE) {
1838 mt->fromRank = gs->ranks-1;
1839 mt->toRank = gs->ranks-1;
1843 // [HGM] castle: for now always assume Fischer-type castling (of which normal castling is a special case).
1844 mt->fromFile = ALG_CASTLE;
1845 mt->toFile = gs->files-2;
1846 mt->fromRank = gs->onMove == WHITE ? gs->wkrmoved : gs->bkrmoved;
1847 mt->toRank = mt->toFile-1; // R next to K
1851 mt->fromFile = gs->files/2;
1853 if (gs->onMove == WHITE) {
1857 mt->fromRank = gs->ranks-1;
1858 mt->toRank = gs->ranks-1;
1862 mt->fromFile = ALG_CASTLE;
1864 mt->fromRank = gs->onMove == WHITE ? gs->wqrmoved : gs->bqrmoved;
1865 mt->toRank = mt->toFile+1;
1868 /* Fills in the mt structure */
1869 if ((result = alg_parse_move(mstr, gs, mt)) != MOVE_OK)
1873 return MOVE_ILLEGAL;
1876 if((mt->fromRank >= gs->ranks || mt->fromRank < 0 || mt->fromFile >= gs->files) &&
1877 mt->fromFile != ALG_DROP && mt->fromFile != ALG_CASTLE
1878 || mt->toRank < 0 || mt->toRank >= gs->ranks || mt->toFile >= gs->files)
1879 return MOVE_ILLEGAL; // [HGM] make sure move stays on board
1881 if (!(result = legal_move(gs, mt->fromFile, mt->fromRank, mt->toFile, mt->toRank)))
1882 return MOVE_ILLEGAL;
1884 if(result == 2) { // [HGM] castle: orthodox castling was given as King move; convert it to new format
1885 if(mt->fromFile - mt->toFile > 1) { // Q-side
1887 mt->toRank = mt->toFile+1;
1888 } else if(mt->toFile - mt->fromFile > 1) { // K-side
1889 mt->fromRank = gs->files-1;
1890 mt->toRank = mt->toFile-1;
1892 mt->fromFile = ALG_CASTLE;
1895 if (mt->piecePromotionTo != NOPIECE) {
1896 promote = piecetype(mt->piecePromotionTo);
1897 } else if(gs->promoType == 3 && promote == MASTODON) promote = GOLD;
1899 return move_calculate(gs, mt, promote);
1902 /* Returns MOVE_OK, MOVE_NOMATERIAL, MOVE_CHECKMATE, or MOVE_STALEMATE */
1903 /* check_game_status prevents recursion */
1904 int execute_move(struct game_state_t * gs, struct move_t * mt, int check_game_status)
1908 int i, j, foobar, wCnt, bCnt, king, rook;
1910 if (mt->fromFile == ALG_DROP) {
1911 movedPiece = mt->fromRank;
1912 tookPiece = NOPIECE;
1913 gs->holding[gs->onMove==WHITE ? 0 : 1][movedPiece-1]--;
1914 gs->board[mt->toFile][mt->toRank] = movedPiece | gs->onMove;
1915 if (gs->gameNum >= 0)
1916 gs->lastIrreversable = game_globals.garray[gs->gameNum].numHalfMoves;
1917 } else if(mt->fromFile == ALG_CASTLE) {
1918 int backRank, fKing;
1919 // [HGM] castle: perform castling
1920 if(gs->onMove == WHITE) {
1922 fKing = gs->wkmoved;
1923 gs->wkmoved = -gs->wkmoved-2;
1925 backRank = gs->ranks-1;
1926 fKing = gs->bkmoved;
1927 gs->bkmoved = -gs->bkmoved-2;
1929 // move Rook & King, in a way that is resistant to ending where they started (for FRC!)
1930 rook = gs->board[mt->fromRank][backRank]; // first remember
1931 king = gs->board[fKing][backRank];
1932 gs->board[mt->fromRank][backRank] = NOPIECE; // then erase
1933 gs->board[fKing][backRank] = NOPIECE;
1934 gs->board[mt->toRank][backRank] = rook; // then put back
1935 gs->board[mt->toFile][backRank] = king;
1937 movedPiece = gs->board[mt->fromFile][mt->fromRank];
1938 tookPiece = gs->board[mt->toFile][mt->toRank];
1939 if(gs->drops == 2 && mt->piecePromotionTo != NOPIECE && pieceType(movedPiece) != PAWN) { // [HGM] Seirawan-style gating
1940 gs->board[mt->toFile][mt->toRank] = gs->board[mt->fromFile][mt->fromRank];
1941 gs->board[mt->fromFile][mt->fromRank] = mt->piecePromotionTo | gs->onMove;;
1942 gs->holding[gs->onMove==WHITE ? 0 : 1][mt->piecePromotionTo-1]--; // remove gated piece from holdings
1944 if (mt->piecePromotionTo == NOPIECE) {
1945 gs->board[mt->toFile][mt->toRank] = gs->board[mt->fromFile][mt->fromRank];
1947 gs->board[mt->toFile][mt->toRank] = mt->piecePromotionTo | gs->onMove;
1948 if(gs->promoType == 2) gs->holding[gs->onMove][mt->piecePromotionTo-1]--;
1950 gs->board[mt->fromFile][mt->fromRank] = NOPIECE;
1952 /* Check if irreversable */
1953 if ((piecetype(movedPiece) == PAWN) && (mt->fromRank != mt->toRank) // [HGM] XQ: sideway Pawn move reversible!
1954 || (tookPiece != NOPIECE)) {
1955 if (gs->gameNum >= 0)
1956 gs->lastIrreversable = game_globals.garray[gs->gameNum].numHalfMoves;
1958 /* Check if this move is en-passant */
1959 if ((piecetype(movedPiece) == PAWN) && (mt->fromFile != mt->toFile) &&
1960 (tookPiece == NOPIECE) && !gs->palace) { // [HGM] XQ: no e.p. in sideway xiangqi moves
1961 if (gs->onMove == WHITE) {
1962 mt->pieceCaptured = B_PAWN;
1964 mt->pieceCaptured = W_PAWN;
1966 if (mt->fromFile > mt->toFile) {
1971 gs->board[mt->toFile][mt->fromRank] = NOPIECE;
1973 /* Check en-passant flags for next moves */
1974 for (i = 0; i < gs->files; i++) {
1975 gs->ep_possible[0][i] = 0;
1976 gs->ep_possible[1][i] = 0;
1978 /* Added by Sparky 3/16/95
1980 From soso@Viktoria.drp.fmph.uniba.sk Thu Mar 16 13:08:51 1995
1981 Subject: Re: To DAV: enpassant prob. again
1982 To: chess@caissa.onenet.net (ICS)
1983 Date: Thu, 16 Mar 1995 20:06:20 +0100 (MET)
1986 There was bug in other part of code:
1988 movecheck.c , line about 800:
1990 if (gs->onMove == WHITE) {
1991 if ((mt->toFile+1 < 7 ) .... should be : (mt->toFile < 7 ) }
1994 if ((piecetype(movedPiece) == PAWN) &&
1995 ((mt->fromRank == mt->toRank + 2) || (mt->fromRank + 2 == mt->toRank))) {
1996 /* Should turn on enpassent flag if possible */
1997 if (gs->onMove == WHITE) {
1998 if ((mt->toFile < gs->files-1) && gs->board[mt->toFile + 1][mt->toRank] == B_PAWN) {
1999 gs->ep_possible[1][mt->toFile + 1] = -1;
2001 if ((mt->toFile - 1 >= 0) && gs->board[mt->toFile - 1][mt->toRank] == B_PAWN) {
2002 gs->ep_possible[1][mt->toFile - 1] = 1;
2005 if ((mt->toFile < gs->files-1) && gs->board[mt->toFile + 1][mt->toRank] == W_PAWN) {
2006 gs->ep_possible[0][mt->toFile + 1] = -1;
2008 if ((mt->toFile - 1 >= 0) && gs->board[mt->toFile - 1][mt->toRank] == W_PAWN) {
2009 gs->ep_possible[0][mt->toFile - 1] = 1;
2013 if ((piecetype(movedPiece) == ROOK) && (mt->fromRank == 0) && (gs->onMove == WHITE)) {
2014 if (mt->fromFile == gs->wqrmoved) // [HGM] castle: flip w.r.t. -1 to remember original
2015 gs->wqrmoved = -gs->wqrmoved-2;
2016 if (mt->fromFile == gs->wkrmoved)
2017 gs->wkrmoved = -gs->wkrmoved-2;
2019 if ((piecetype(movedPiece) == ROOK) && (mt->fromRank == gs->ranks-1) && (gs->onMove == BLACK)) {
2020 if (mt->fromFile == gs->bqrmoved)
2021 gs->bqrmoved = -gs->bqrmoved-2;
2022 if (mt->fromFile == gs->bkrmoved)
2023 gs->bkrmoved = -gs->bkrmoved-2;
2025 if (piecetype(movedPiece) == KING) {
2026 if ((gs->onMove == WHITE) && (mt->fromFile == gs->wkmoved))
2027 gs->wkmoved = -gs->wkmoved-2;
2028 if ((gs->onMove == BLACK) && (mt->fromFile == gs->bkmoved))
2029 gs->bkmoved = -gs->bkmoved-2;
2032 if ((piecetype(movedPiece) == KING) &&
2033 ((mt->fromFile == gs->files/2) && (mt->toFile == gs->files-2)) &&
2034 mt->fromRank == mt->toRank) { /* Check for KS castling */
2035 gs->board[gs->files-3][mt->toRank] = gs->board[gs->files-1][mt->toRank];
2036 gs->board[gs->files-1][mt->toRank] = NOPIECE;
2038 if ((piecetype(movedPiece) == KING) &&
2039 ((mt->fromFile == gs->files/2) && (mt->toFile == 2)) &&
2040 mt->fromRank == mt->toRank) { /* Check for QS castling */
2041 gs->board[3][mt->toRank] = gs->board[0][mt->toRank];
2042 gs->board[0][mt->toRank] = NOPIECE;
2046 if (gs->onMove == BLACK)
2049 if (check_game_status) {
2050 /* Does this move result in check? */
2052 /* Check for checkmate */
2053 gs->onMove = CToggle(gs->onMove);
2054 if (!has_legal_move(gs))
2055 return MOVE_CHECKMATE;
2057 /* Check for stalemate */
2058 gs->onMove = CToggle(gs->onMove);
2059 if (!has_legal_move(gs))
2060 return gs->stalemate ? MOVE_STALEMATE : MOVE_CHECKMATE; // [HGM] in XQ and shatranj stalemate loses
2062 /* loon: check for insufficient mating material, first try */
2063 foobar = wCnt = bCnt = 0;
2064 for (i=0; i<gs->files; i++) {
2065 for (j=0; j<gs->ranks; j++) {
2066 int p = gs->board[i][j];
2067 switch(piecetype(p)) {
2079 if(p != NOPIECE && iscolor(p, WHITE)) wCnt++;
2080 if(iscolor(p, BLACK)) bCnt++;
2083 if(gs->bareKingLoses) { // [HGM] with bare-King-loses rule only KK is insuff. material
2084 if(gs->onMove == BLACK && wCnt == 1 && bCnt > 1) return MOVE_BARE;
2085 if(gs->onMove == WHITE && bCnt == 1 && wCnt > 1) return MOVE_BARE;
2086 if(bCnt == 1 && wCnt == 1) return MOVE_NOMATERIAL;
2087 } else if (foobar < 2)
2088 return MOVE_NOMATERIAL;
2090 gs->onMove = CToggle(gs->onMove);
2096 int backup_move(int g, int mode)
2098 struct game_state_t *gs;
2099 struct move_t *m, *m1;
2102 if (game_globals.garray[g].link >= 0) /*IanO: not implemented for bughouse yet */
2103 return MOVE_ILLEGAL;
2104 if (game_globals.garray[g].numHalfMoves < 1)
2105 return MOVE_ILLEGAL;
2106 gs = &game_globals.garray[g].game_state;
2107 m = (mode==REL_GAME) ? &game_globals.garray[g].moveList[game_globals.garray[g].numHalfMoves - 1] :
2108 &game_globals.garray[g].examMoveList[game_globals.garray[g].numHalfMoves - 1];
2109 if (m->toFile < 0) {
2110 return MOVE_ILLEGAL;
2112 if(m->fromFile == ALG_CASTLE) {
2113 // [HGM] castling in new format. Derive K and R moves
2114 int rank, kingFromFile;
2115 if(m->color == WHITE) {
2117 kingFromFile = -gs->wkmoved-2;
2118 if(kingFromFile<0) kingFromFile = -kingFromFile-2; // safety catch; should never happen?
2119 gs->wkmoved = kingFromFile;
2120 if(m->toRank > m->toFile) gs->wqrmoved = m->fromRank;
2121 else gs->wkrmoved = m->fromRank;
2124 kingFromFile = -gs->bkmoved-2;
2125 if(kingFromFile<0) kingFromFile = -kingFromFile-2; // safety catch; should never happen?
2126 gs->bkmoved = kingFromFile;
2127 if(m->toRank > m->toFile) gs->bqrmoved = m->fromRank;
2128 else gs->bkrmoved = m->fromRank;
2130 // remove first, as one might come back to a square the other left
2131 gs->board[m->toFile ][rank] = NOPIECE; // King toSqr
2132 gs->board[m->toRank ][rank] = NOPIECE; // Rook toSqr
2133 gs->board[m->fromRank][rank] = ROOK | m->color; // Rook fromSqr
2134 gs->board[kingFromFile][rank] = KING | m->color; // King fromSquare
2137 gs->board[m->fromFile][m->fromRank] = gs->board[m->toFile][m->toRank];
2138 if (m->piecePromotionTo != NOPIECE) {
2139 gs->board[m->fromFile][m->fromRank] = PAWN |
2140 colorval(gs->board[m->fromFile][m->fromRank]);
2143 When takeback a _first_ move of rook, the ??rmoved variable
2144 must be cleared . To check, if the move is first, we should
2146 *******************/
2147 if (piecetype(gs->board[m->fromFile][m->fromRank]) == ROOK) {
2148 if (m->color == WHITE) {
2149 if ((m->fromFile == -gs->wqrmoved-2) && (m->fromRank == 0)) {
2150 for (i = 2; i < game_globals.garray[g].numHalfMoves - 1; i += 2) {
2151 m1 = (mode==REL_GAME) ? &game_globals.garray[g].moveList[i] : &game_globals.garray[g].examMoveList[i];
2152 if ((m1->fromFile == -gs->wqrmoved-2) && (m1->fromRank == 0))
2155 if (i == game_globals.garray[g].numHalfMoves - 1)
2156 gs->wqrmoved = m->fromFile;
2158 if ((m->fromFile == -gs->wkrmoved-2) && (m->fromRank == 0)) {
2159 for (i = 2; i < game_globals.garray[g].numHalfMoves - 1; i += 2) {
2160 m1 = (mode==REL_GAME) ? &game_globals.garray[g].moveList[i] : &game_globals.garray[g].examMoveList[i];
2161 if ((m1->fromFile == -gs->wkrmoved-2) && (m1->fromRank == 0))
2164 if (i == game_globals.garray[g].numHalfMoves - 1)
2165 gs->wkrmoved = m->fromFile;
2168 if ((m->fromFile == -gs->bqrmoved-2) && (m->fromRank == gs->ranks-1)) {
2169 for (i = 3; i < game_globals.garray[g].numHalfMoves - 1; i += 2) {
2170 m1 = (mode==REL_GAME) ? &game_globals.garray[g].moveList[i] : &game_globals.garray[g].examMoveList[i];
2171 if ((m1->fromFile == -gs->bkrmoved-2) && (m1->fromRank == gs->ranks-1))
2174 if (i == game_globals.garray[g].numHalfMoves - 1)
2175 gs->bqrmoved = m->fromFile;
2177 if ((m->fromFile == -gs->bkrmoved-2) && (m->fromRank == gs->ranks-1)) {
2178 for (i = 3; i < game_globals.garray[g].numHalfMoves - 1; i += 2) {
2179 m1 = (mode==REL_GAME) ? &game_globals.garray[g].moveList[i] : &game_globals.garray[g].examMoveList[i];
2180 if ((m1->fromFile == -gs->wkrmoved-2) && (m1->fromRank == gs->ranks-1))
2183 if (i == game_globals.garray[g].numHalfMoves - 1)
2184 gs->bkrmoved = m->fromFile;
2188 if (piecetype(gs->board[m->fromFile][m->fromRank]) == KING) {
2189 gs->board[m->toFile][m->toRank] = m->pieceCaptured;
2191 /* [HGM] castlings are already intercepted due to new format; this code wrecks knightmate! */
2192 if (m->toFile - m->fromFile == 2) {
2193 gs->board[7][m->fromRank] = ROOK |
2194 colorval(gs->board[m->fromFile][m->fromRank]);
2195 gs->board[5][m->fromRank] = NOPIECE;
2198 If takeback a castling, the appropriates ??moved variables
2201 if (m->color == WHITE) {
2210 if (m->fromFile - m->toFile == 2) {
2211 gs->board[0][m->fromRank] = ROOK |
2212 colorval(gs->board[m->fromFile][m->fromRank]);
2213 gs->board[3][m->fromRank] = NOPIECE;
2216 If takeback a castling, the appropriate ??moved variables
2219 if (m->color == WHITE) {
2230 When takeback a _first_ move of king (not the castling),
2231 the ?kmoved variable must be cleared . To check, if the move is first,
2232 we should scan moveList.
2233 *******************/
2235 if (m->color == WHITE) {
2237 if ((m->fromFile == -gs->wkmoved-2) && (m->fromRank == 0)) {
2238 for (i = 2; i < game_globals.garray[g].numHalfMoves - 1; i += 2) {
2239 m1 = (mode==REL_GAME) ? &game_globals.garray[g].moveList[i] : &game_globals.garray[g].examMoveList[i];
2240 if ((m1->fromFile == gs->wkmoved-2) && (m1->fromRank == 0))
2243 if (i == game_globals.garray[g].numHalfMoves - 1)
2244 gs->wkmoved = m->fromFile;
2247 if ((m->fromFile == -gs->bkmoved-2) && (m->fromRank == gs->ranks-1)) {
2248 for (i = 3; i < game_globals.garray[g].numHalfMoves - 1; i += 2) {
2249 m1 = (mode==REL_GAME) ? &game_globals.garray[g].moveList[i] : &game_globals.garray[g].examMoveList[i];
2250 if ((m1->fromFile == -gs->bkmoved-2) && (m1->fromRank == gs->ranks-1))
2253 if (i == game_globals.garray[g].numHalfMoves - 1)
2254 gs->bkmoved = m->fromFile;
2258 if (m->enPassant) { /* Do enPassant */
2259 gs->board[m->toFile][m->fromRank] = PAWN |
2260 (colorval(gs->board[m->fromFile][m->fromRank]) == WHITE ? BLACK : WHITE);
2261 gs->board[m->toFile][m->toRank] = NOPIECE;
2262 /* Should set the enpassant array, but I don't care right now */
2265 gs->board[m->toFile][m->toRank] = m->pieceCaptured;
2266 if(gs->board[m->fromFile][m->fromRank] != NOPIECE) { // [HGM] from-square occupied, must have been Seirawan-style gating
2267 gs->holding[gs->onMove==WHITE ? 1 : 0][piecetype(gs->board[m->fromFile][m->fromRank])-1]++; // put back in holdings (onMove not flipped yet!)
2270 if (game_globals.garray[g].status != GAME_EXAMINE) {
2271 game_update_time(g);
2273 game_globals.garray[g].numHalfMoves--;
2274 if (game_globals.garray[g].status != GAME_EXAMINE) {
2275 if (game_globals.garray[g].wInitTime) { /* Don't update times in untimed games */
2278 if (m->color == WHITE) {
2279 if (net_globals.con[player_globals.parray[game_globals.garray[g].white].socket]->timeseal) { /* white uses timeseal? */
2280 game_globals.garray[g].wRealTime += (m->tookTime * 100);
2281 game_globals.garray[g].wRealTime -= (game_globals.garray[g].wIncrement * 100);
2282 game_globals.garray[g].wTime = game_globals.garray[g].wRealTime / 100;
2283 if (net_globals.con[player_globals.parray[game_globals.garray[g].black].socket]->timeseal) { /* opp uses timeseal? */
2284 game_globals.garray[g].bTime = game_globals.garray[g].bRealTime / 100;
2285 } else { /* opp has no timeseal */
2286 game_globals.garray[g].bTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
2288 } else { /* white has no timeseal */
2289 game_globals.garray[g].wTime += m->tookTime;
2290 game_globals.garray[g].wTime -= game_globals.garray[g].wIncrement;
2291 if (net_globals.con[player_globals.parray[game_globals.garray[g].black].socket]->timeseal) { /* opp uses timeseal? */
2292 game_globals.garray[g].bTime = game_globals.garray[g].bRealTime / 100;
2293 } else { /* opp has no timeseal */
2294 game_globals.garray[g].bTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
2298 if (net_globals.con[player_globals.parray[game_globals.garray[g].black].socket]->timeseal) { /* black uses timeseal? */
2299 game_globals.garray[g].bRealTime += (m->tookTime * 100);
2300 game_globals.garray[g].bRealTime -= (game_globals.garray[g].wIncrement * 100);
2301 game_globals.garray[g].bTime = game_globals.garray[g].bRealTime / 100;
2302 if (net_globals.con[player_globals.parray[game_globals.garray[g].white].socket]->timeseal) { /* opp uses timeseal? */
2303 game_globals.garray[g].wTime = game_globals.garray[g].wRealTime / 100;
2304 } else { /* opp has no timeseal */
2305 game_globals.garray[g].wTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
2307 } else { /* black has no timeseal */
2308 game_globals.garray[g].bTime += m->tookTime;
2309 if (!game_globals.garray[g].bIncrement)
2310 game_globals.garray[g].bTime -= game_globals.garray[g].wIncrement;
2312 game_globals.garray[g].bTime -= game_globals.garray[g].bIncrement;
2313 if (net_globals.con[player_globals.parray[game_globals.garray[g].white].socket]->timeseal) { /* opp uses timeseal? */
2314 game_globals.garray[g].wTime = game_globals.garray[g].wRealTime / 100;
2315 } else { /* opp has no timeseal */
2316 game_globals.garray[g].wTime += (game_globals.garray[g].lastDecTime - game_globals.garray[g].lastMoveTime);
2321 if (game_globals.garray[g].numHalfMoves == 0)
2322 game_globals.garray[g].timeOfStart = now;
2323 game_globals.garray[g].lastMoveTime = now;
2324 game_globals.garray[g].lastDecTime = now;
2327 if (gs->onMove == BLACK)
2334 /******* Here begins the patch : ********************************
2335 Takeback of last move is done already, it's time to update enpassant
2336 array. (patch from Soso, added by Sparky 3/17/95)
2339 if (game_globals.garray[g].numHalfMoves > 0) {
2340 m1 = (mode==REL_GAME) ? &game_globals.garray[g].moveList[game_globals.garray[g].numHalfMoves - 1] :
2341 &game_globals.garray[g].examMoveList[game_globals.garray[g].numHalfMoves - 1];
2342 if (piecetype(gs->board[m1->toFile][m1->toRank]) == PAWN) {
2343 if ((m1->toRank - m1->fromRank) == 2) {
2344 if ((m1->toFile < gs->files-1) && gs->board[m1->toFile + 1][m1->toRank] == B_PAWN) {
2345 gs->ep_possible[1][m1->toFile + 1] = -1;
2347 if ((m1->toFile - 1 >= 0) && gs->board[m1->toFile - 1][m1->toRank] == B_PAWN) {
2348 gs->ep_possible[1][m1->toFile - 1] = 1;
2351 if ((m1->toRank - m1->fromRank) == -2) {
2352 if ((m1->toFile < gs->files-1) && gs->board[m1->toFile + 1][m1->toRank] == W_PAWN) {
2353 gs->ep_possible[0][m1->toFile + 1] = -1;
2355 if ((m1->toFile - 1 >= 0) && gs->board[m1->toFile - 1][m1->toRank] == W_PAWN) {
2356 gs->ep_possible[0][m1->toFile - 1] = 1;
2361 /************** and here's the end **************/