| 29 |
29 |
| 30 #include "rules.h" |
30 #include "rules.h" |
| 31 #include "king.h" |
31 #include "king.h" |
| 32 |
32 |
| 33 static bool king_castling_chkmoved( |
33 static bool king_castling_chkmoved( |
| 34 const GameState *gamestate, Rank rank, File file) { |
34 const GameState *gamestate, File file, Rank rank) { |
| 35 |
35 |
| 36 for (unsigned i = 0; i < gamestate->movecount; i++) { |
36 for (unsigned i = 0; i < gamestate->movecount; i++) { |
| 37 if (gamestate->moves[i].fromfile == file |
37 if (gamestate->moves[i].fromfile == file |
| 38 && gamestate->moves[i].fromrank == rank) { |
38 && gamestate->moves[i].fromrank == rank) { |
| 39 return true; |
39 return true; |
| 53 move->fromrank == (piece_color(move->piece) == WHITE ? 0 : 7) && |
53 move->fromrank == (piece_color(move->piece) == WHITE ? 0 : 7) && |
| 54 move->fromfile == fileidx('e') && |
54 move->fromfile == fileidx('e') && |
| 55 (move->tofile == fileidx('c') || move->tofile == fileidx('g'))) { |
55 (move->tofile == fileidx('c') || move->tofile == fileidx('g'))) { |
| 56 |
56 |
| 57 return !king_castling_chkmoved(gamestate, |
57 return !king_castling_chkmoved(gamestate, |
| 58 move->fromrank, move->fromfile) && |
58 move->fromfile, move->fromrank) && |
| 59 !king_castling_chkmoved(gamestate, move->fromrank, |
59 !king_castling_chkmoved(gamestate, |
| 60 move->tofile == fileidx('c') ? 0 : 7); |
60 move->tofile == fileidx('c') ? 0 : 7, move->fromrank); |
| 61 } else { |
61 } else { |
| 62 return false; |
62 return false; |
| 63 } |
63 } |
| 64 } |
64 } |
| 65 } |
65 } |
| 80 bool incheck = false; |
80 bool incheck = false; |
| 81 if (gamestate->movecount > 0) { |
81 if (gamestate->movecount > 0) { |
| 82 incheck = is_check_position(gamestate); |
82 incheck = is_check_position(gamestate); |
| 83 } |
83 } |
| 84 blocked |= incheck || gamestate->board[move->torank][midfile] || |
84 blocked |= incheck || gamestate->board[move->torank][midfile] || |
| 85 is_covered(gamestate, move->torank, midfile, op_color); |
85 is_covered(gamestate, midfile, move->torank, op_color); |
| 86 } |
86 } |
| 87 |
87 |
| 88 return blocked; |
88 return blocked; |
| 89 } |
89 } |
| 90 |
90 |
| 91 size_t king_moves(const GameState *gamestate, |
91 size_t king_moves(const GameState *gamestate, |
| 92 Color c, Rank r, File f, Move *moves) { |
92 Color c, File f, Rank r, Move *moves) { |
| 93 |
93 |
| 94 size_t count = 0; |
94 size_t count = 0; |
| 95 Piece king = mkpiece(KING, c); |
95 Piece king = mkpiece(KING, c); |
| 96 |
96 |
| 97 for (int dr = -1 ; dr <= 1 ; dr++) { |
97 for (int dr = -1 ; dr <= 1 ; dr++) { |
| 111 moves[count].piece = king; |
111 moves[count].piece = king; |
| 112 moves[count].fromrank = r; |
112 moves[count].fromrank = r; |
| 113 moves[count].fromfile = f; |
113 moves[count].fromfile = f; |
| 114 moves[count].torank = torank; |
114 moves[count].torank = torank; |
| 115 moves[count].tofile = tofile; |
115 moves[count].tofile = tofile; |
| 116 moves[count].capture = piece_at(gamestate, torank, tofile) != 0; |
116 moves[count].capture = piece_at(gamestate, tofile, torank) != 0; |
| 117 count++; |
117 count++; |
| 118 } |
118 } |
| 119 } |
119 } |
| 120 |
120 |
| 121 Rank homerank = c == WHITE ? 0 : 7; |
121 Rank homerank = c == WHITE ? 0 : 7; |