29 |
29 |
30 #include "rules.h" |
30 #include "rules.h" |
31 #include "king.h" |
31 #include "king.h" |
32 #include <stdlib.h> |
32 #include <stdlib.h> |
33 |
33 |
34 static _Bool king_castling_chkmoved(GameState *gamestate, |
34 static bool king_castling_chkmoved(GameState *gamestate, |
35 uint8_t row, uint8_t file) { |
35 uint8_t row, uint8_t file) { |
36 |
36 |
37 MoveList *ml = gamestate->movelist; |
37 MoveList *ml = gamestate->movelist; |
38 while (ml) { |
38 while (ml) { |
39 if (ml->move.fromfile == file && ml->move.fromrow == row) { |
39 if (ml->move.fromfile == file && ml->move.fromrow == row) { |
40 return 1; |
40 return true; |
41 } |
41 } |
42 ml = ml->next; |
42 ml = ml->next; |
43 } |
43 } |
44 |
44 |
45 return 0; |
45 return false; |
46 } |
46 } |
47 |
47 |
48 _Bool king_chkrules(GameState *gamestate, Move* move) { |
48 bool king_chkrules(GameState *gamestate, Move* move) { |
49 if (abs(move->torow - move->fromrow) <= 1 && |
49 if (abs(move->torow - move->fromrow) <= 1 && |
50 abs(move->tofile - move->fromfile) <= 1) { |
50 abs(move->tofile - move->fromfile) <= 1) { |
51 return 1; |
51 return true; |
52 } else { |
52 } else { |
53 /* castling */ |
53 /* castling */ |
54 if (move->fromrow == move->torow && |
54 if (move->fromrow == move->torow && |
55 move->fromrow == ((move->piece&COLOR_MASK) == WHITE ? 0 : 7) && |
55 move->fromrow == ((move->piece&COLOR_MASK) == WHITE ? 0 : 7) && |
56 move->fromfile == fileidx('e') && |
56 move->fromfile == fileidx('e') && |
59 return !king_castling_chkmoved(gamestate, |
59 return !king_castling_chkmoved(gamestate, |
60 move->fromrow, move->fromfile) && |
60 move->fromrow, move->fromfile) && |
61 !king_castling_chkmoved(gamestate, move->fromrow, |
61 !king_castling_chkmoved(gamestate, move->fromrow, |
62 move->tofile == fileidx('c') ? 0 : 7); |
62 move->tofile == fileidx('c') ? 0 : 7); |
63 } else { |
63 } else { |
64 return 0; |
64 return false; |
65 } |
65 } |
66 } |
66 } |
67 } |
67 } |
68 |
68 |
69 _Bool king_isblocked(GameState *gamestate, Move *move) { |
69 bool king_isblocked(GameState *gamestate, Move *move) { |
70 |
70 |
71 uint8_t opponent_color = opponent_color(move->piece&COLOR_MASK); |
71 uint8_t opponent_color = opponent_color(move->piece&COLOR_MASK); |
72 |
72 |
73 /* being in check does not "block" the king, so don't test it here */ |
73 /* being in check does not "block" the king, so don't test it here */ |
74 _Bool blocked = 0; |
74 bool blocked = false; |
75 |
75 |
76 /* just test, if castling move is blocked */ |
76 /* just test, if castling move is blocked */ |
77 if (abs(move->tofile - move->fromfile) == 2) { |
77 if (abs(move->tofile - move->fromfile) == 2) { |
78 if (move->tofile == fileidx('c')) { |
78 if (move->tofile == fileidx('c')) { |
79 blocked |= gamestate->board[move->torow][fileidx('b')]; |
79 blocked |= gamestate->board[move->torow][fileidx('b')]; |