int validmove(move m) { move_and_score *restore_sp = move_sp; genmoves(); while(move_sp>restore_sp) { move vm = popmove(); if (m == vm) { int r = wouldbeincheckfullp(m); if (r == 0) { move_sp = restore_sp; return 1; /* valid */ } else { move_sp = restore_sp; return -r; /* invalid move */ } } } return 0; /* move was not found */ }
int castling_test(struct castling_struct *info) { // bit test for castling right if ((board->flags & info->bittest) == 0) { return 0; } square ks = info->king_start; square rs = info->rook_start; int i; int increment = abs(B1-A1); // test for empty squres in between rook and king for (i=min(ks,rs)+increment;i<max(rs,ks);i+=increment) { if (getpiece__(i)) return 0; } // end square for rook must be empty or be the king chesspiece p = getpiece__(info->rook_end); if (p && VALUE(p) != king) return 0; square ke = info->king_end; // test for attacks on king from start to end inclusive // genmoves_nocastles so that we do not recurse forever switch_sides(); move_and_score *restore_sp = move_sp; genmoves_nocastles(); while (move_sp>restore_sp) { move m = popmove(); int i_min = min(ke,ks); int i_max = max(ke,ks); for (i=i_min;i<=i_max;i+=increment) { if (TO(m) == i) { move_sp = restore_sp; switch_sides(); return 0; } } } switch_sides(); return 1; }
void showmoves(c) { move_and_score *restore_sp = move_sp; genmoves(c); while (move_sp>restore_sp) { move m; m = popmove(); domove(m); printboard(); undomove(); } }
void cmd_attacks(char *s) { move_and_score *restore_sp = move_sp; genattacks(); if (move_sp>restore_sp) { while (move_sp>restore_sp) { move m = popmove(); printf("%8.8lx - %8lu - %s\n", (unsigned long)m, (unsigned long)m,movestring(m)); } } else printf("\tno available attacks\n"); }
square findstart(chesspiece p, int f, int r, int gf, int gr) { square s1 = 0; int count = 0; move_and_score *restore_sp = move_sp; genmoves(); while(move_sp>restore_sp) { square start, end; int ef,er,sf,sr; move m = popmove(); start = FR(m); end = TO(m); ef = F(end); er = R(end); sf = F(start); sr = R(start); if (p==getpiece(sf, sr)) if ((ef==f)&&((gf==sf)||(gf==8))) if ((er==r)&&((gr==sr)||(gr==8))) { count++; s1 = SQ(sf,sr); } } if (count>1) { printf("Illegal move (ambiguous)\n"); return dummysq; } else if (count == 1) { return s1; } else return dummysq; }
void cmd_threats(char *s) { int count=0; move_and_score *restore_sp = move_sp; board->flags ^= turn_flag; genmoves(); board->flags ^= turn_flag; while (move_sp>restore_sp) { square end; int ef, er; move m = popmove(); end = TO(m); ef = F(end); er = R(end); if (getpiece__(end)) { square start; int sf, sr; chesspiece p = getpiece(ef, er); start = FR(m); sf = F(start); sr = R(start); printf("\t%c threatened on %c%c from %c%c\n", rep(p), ef+'a',er+'1', sf+'a',sr+'1'); count++; } } if (count==0) printf("\t no pieces currently threatened\n"); }