src/chess/king.c

changeset 47
d726e4b46c33
parent 25
3ab0c2e1a4e2
child 55
54ea19938d57
equal deleted inserted replaced
46:4dcfb4c58b6d 47:d726e4b46c33
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 _Bool blocked = is_covered(gamestate, move->torow, move->tofile,
73 opponent_color);
74 72
73 // being in check does not "block" the king, so don't test it here
74 _Bool blocked = 0;
75
76 // just test, if castling move is blocked
75 if (abs(move->tofile - move->fromfile) == 2) { 77 if (abs(move->tofile - move->fromfile) == 2) {
76 if (move->tofile == fileidx('c')) { 78 if (move->tofile == fileidx('c')) {
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;
82 is_covered(gamestate, move->torow, midfile, opponent_color); 84 is_covered(gamestate, move->torow, midfile, opponent_color);
83 } 85 }
84 86
85 return blocked; 87 return blocked;
86 } 88 }
87
88 int king_getlocation(GameState *gamestate, Move *move) {
89
90 uint8_t file, row;
91
92 for (int f = -1 ; f <= 1 ; f++) {
93 for (int r = -1 ; r <= 1 ; r++) {
94 if (f == 0 && r == 0) {
95 continue;
96 }
97 file = move->tofile + f;
98 row = move->torow + r;
99 if (isidx(file) && isidx(row)) {
100 if (gamestate->board[row][file] == move->piece) {
101 if ((move->fromfile != POS_UNSPECIFIED
102 && move->fromfile != file) ||
103 (move->fromrow != POS_UNSPECIFIED
104 && move->fromrow != row)) {
105 return INVALID_POSITION;
106 }
107 move->fromfile = file;
108 move->fromrow = row;
109 return VALID_MOVE_SYNTAX;
110 }
111 }
112 }
113 }
114
115 return INVALID_POSITION;
116 }

mercurial