src/chess/king.c

changeset 163
2a6d83f4677e
parent 161
3ff96fec144a
equal deleted inserted replaced
162:f0fc70b6f8f9 163:2a6d83f4677e
30 #include "rules.h" 30 #include "rules.h"
31 #include "king.h" 31 #include "king.h"
32 32
33 #include <string.h> 33 #include <string.h>
34 34
35 static bool king_castling_chkmoved(GameState *gamestate, Row row, File file) { 35 static bool king_castling_chkmoved(
36 const GameState *gamestate, Row row, File file) {
37
36 for (unsigned i = 0; i < gamestate->movecount; i++) { 38 for (unsigned i = 0; i < gamestate->movecount; i++) {
37 if (gamestate->moves[i].fromfile == file 39 if (gamestate->moves[i].fromfile == file
38 && gamestate->moves[i].fromrow == row) { 40 && gamestate->moves[i].fromrow == row) {
39 return true; 41 return true;
40 } 42 }
41 } 43 }
42 44
43 return false; 45 return false;
44 } 46 }
45 47
46 bool king_chkrules(GameState *gamestate, Move* move) { 48 bool king_chkrules(const GameState *gamestate, const Move* move) {
47 if (abs(move->torow - move->fromrow) <= 1 && 49 if (abs(move->torow - move->fromrow) <= 1 &&
48 abs(move->tofile - move->fromfile) <= 1) { 50 abs(move->tofile - move->fromfile) <= 1) {
49 return true; 51 return true;
50 } else { 52 } else {
51 /* castling */ 53 /* castling */
62 return false; 64 return false;
63 } 65 }
64 } 66 }
65 } 67 }
66 68
67 bool king_isblocked(GameState *gamestate, Move *move) { 69 bool king_isblocked(const GameState *gamestate, const Move *move) {
68 70
69 uint8_t opponent_color = opponent_color(piece_color(move->piece)); 71 uint8_t opponent_color = opponent_color(piece_color(move->piece));
70 72
71 /* 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 */
72 bool blocked = false; 74 bool blocked = false;
77 blocked |= gamestate->board[move->torow][fileidx('b')]; 79 blocked |= gamestate->board[move->torow][fileidx('b')];
78 } 80 }
79 uint8_t midfile = (move->tofile+move->fromfile)/2; 81 uint8_t midfile = (move->tofile+move->fromfile)/2;
80 bool incheck = false; 82 bool incheck = false;
81 if (gamestate->movecount > 0) { 83 if (gamestate->movecount > 0) {
82 incheck = gamestate->moves[gamestate->movecount-1].check; 84 incheck = is_check_position(gamestate);
83 } 85 }
84 blocked |= incheck || gamestate->board[move->torow][midfile] || 86 blocked |= incheck || gamestate->board[move->torow][midfile] ||
85 is_covered(gamestate, move->torow, midfile, opponent_color); 87 is_covered(gamestate, move->torow, midfile, opponent_color);
86 } 88 }
87 89

mercurial