src/chess/rules.c

changeset 25
3ab0c2e1a4e2
parent 23
824c9522ce66
child 27
efeb98bc69c9
equal deleted inserted replaced
24:4d030da07c88 25:3ab0c2e1a4e2
88 case KING: return king_getlocation(gamestate, move); 88 case KING: return king_getlocation(gamestate, move);
89 default: return INVALID_MOVE_SYNTAX; 89 default: return INVALID_MOVE_SYNTAX;
90 } 90 }
91 } 91 }
92 92
93 _Bool is_covered(GameState *gamestate,uint8_t row,uint8_t file,uint8_t color) {
94 Move threats[16];
95 int threatcount = 0;
96 for (uint8_t r = 0 ; r < 8 ; r++) {
97 for (uint8_t f = 0 ; f < 8 ; f++) {
98 if ((gamestate->board[r][f] & COLOR_MASK) == color) {
99 threats[threatcount].piece = gamestate->board[r][f];
100 threats[threatcount].fromrow = r;
101 threats[threatcount].fromfile = f;
102 threats[threatcount].torow = row;
103 threats[threatcount].tofile = file;
104 threatcount++;
105 }
106 }
107 }
108
109 for (int i = 0 ; i < threatcount ; i++) {
110 if (validate_move(gamestate, &(threats[i]))) {
111 return 1;
112 }
113 }
114
115 return 0;
116 }
93 117
94 void apply_move(GameState *gamestate, Move *move) { 118 void apply_move(GameState *gamestate, Move *move) {
95 uint8_t piece = move->piece & PIECE_MASK; 119 uint8_t piece = move->piece & PIECE_MASK;
96 uint8_t color = move->piece & COLOR_MASK; 120 uint8_t color = move->piece & COLOR_MASK;
97 121
188 break; 212 break;
189 default: 213 default:
190 result = 0; 214 result = 0;
191 } 215 }
192 216
217 /* cancel processing to avoid recursion overflow with is_covered() */
218 if (!result) {
219 return 0;
220 }
221
193 /* is piece pinned */ 222 /* is piece pinned */
194 // TODO: make it so 223 // TODO: make it so
195 224
196 /* correct check and checkmate flags */ 225 /* correct check and checkmate flags (move is still valid) */
197 // TODO: make it so 226 // TODO: make it so
198 227
199 return result; 228 return result;
200 } 229 }
201 230

mercurial