15 #define DELTA_INC(delta) (DeltaInc[128+(delta)])
16 #define DELTA_MASK(delta) (DeltaMask[128+(delta)])
20 const sint8 KnightInc[8+1] = {
21 -33, -31, -18, -14, +14, +18, +31, +33, 0
24 const sint8 BishopInc[4+1] = {
28 const sint8 RookInc[4+1] = {
32 const sint8 QueenInc[8+1] = {
33 -17, -16, -15, -1, +1, +15, +16, +17, 0
36 const sint8 KingInc[8+1] = {
37 -17, -16, -15, -1, +1, +15, +16, +17, 0
42 static sint8 DeltaInc[256];
43 static uint8 DeltaMask[256];
47 static bool delta_is_ok (int delta);
48 static bool inc_is_ok (int inc);
59 for (delta = -128; delta < +128; delta++) {
60 DeltaInc[128+delta] = IncNone;
61 DeltaMask[128+delta] = 0;
64 DeltaMask[128-17] |= BlackPawnFlag;
65 DeltaMask[128-15] |= BlackPawnFlag;
67 DeltaMask[128+15] |= WhitePawnFlag;
68 DeltaMask[128+17] |= WhitePawnFlag;
70 for (dir = 0; dir < 8; dir++) {
71 delta = KnightInc[dir];
72 ASSERT(delta_is_ok(delta));
73 DeltaMask[128+delta] |= KnightFlag;
76 for (dir = 0; dir < 4; dir++) {
79 for (dist = 1; dist < 8; dist++) {
81 ASSERT(delta_is_ok(delta));
82 ASSERT(DeltaInc[128+delta]==IncNone);
83 DeltaInc[128+delta] = inc;
84 DeltaMask[128+delta] |= BishopFlag;
88 for (dir = 0; dir < 4; dir++) {
91 for (dist = 1; dist < 8; dist++) {
93 ASSERT(delta_is_ok(delta));
94 ASSERT(DeltaInc[128+delta]==IncNone);
95 DeltaInc[128+delta] = inc;
96 DeltaMask[128+delta] |= RookFlag;
100 for (dir = 0; dir < 8; dir++) {
101 delta = KingInc[dir];
102 ASSERT(delta_is_ok(delta));
103 DeltaMask[128+delta] |= KingFlag;
109 static bool delta_is_ok(int delta) {
111 if (delta < -119 || delta > +119) return FALSE;
118 static bool inc_is_ok(int inc) {
122 for (dir = 0; dir < 8; dir++) {
123 if (KingInc[dir] == inc) return TRUE;
131 bool is_in_check(const board_t * board, int colour) {
133 ASSERT(board_is_ok(board));
134 ASSERT(colour_is_ok(colour));
136 return is_attacked(board,king_pos(board,colour),colour_opp(colour));
141 bool is_attacked(const board_t * board, int to, int colour) {
146 ASSERT(board_is_ok(board));
147 ASSERT(square_is_ok(to));
148 ASSERT(colour_is_ok(colour));
150 for (ptr = board->list[colour]; (from=*ptr) != SquareNone; ptr++) {
152 piece = board->square[from];
153 ASSERT(colour_equal(piece,colour));
155 if (piece_attack(board,piece,from,to)) return TRUE;
163 bool piece_attack(const board_t * board, int piece, int from, int to) {
168 ASSERT(board_is_ok(board));
169 ASSERT(piece_is_ok(piece));
170 ASSERT(square_is_ok(from));
171 ASSERT(square_is_ok(to));
174 ASSERT(delta_is_ok(delta));
176 if ((piece & DELTA_MASK(delta)) == 0) return FALSE; // no pseudo-attack
178 if (!piece_is_slider(piece)) return TRUE;
180 inc = DELTA_INC(delta);
181 ASSERT(inc_is_ok(inc));
183 for (sq = from+inc; sq != to; sq += inc) {
184 ASSERT(square_is_ok(sq));
185 if (board->square[sq] != Empty) return FALSE; // blocker
193 bool is_pinned(const board_t * board, int from, int to, int colour) {
200 ASSERT(square_is_ok(from));
201 ASSERT(square_is_ok(to));
202 ASSERT(colour_is_ok(colour));
204 king = king_pos(board,colour);
206 inc = DELTA_INC(king-from);
207 if (inc == IncNone) return FALSE; // not a line
210 do sq += inc; while (board->square[sq] == Empty);
212 if (sq != king) return FALSE; // blocker
215 do sq -= inc; while ((piece=board->square[sq]) == Empty);
217 return square_is_ok(sq)
218 && (piece & DELTA_MASK(king-sq)) != 0
219 && piece_colour(piece) == colour_opp(colour)
220 && DELTA_INC(king-to) != inc;