src/chess/rules.c

changeset 216
d0c3d3016650
parent 201
b61c2b90f19c
equal deleted inserted replaced
215:d20887f0e8b3 216:d0c3d3016650
344 (move->fromrank == 6 && move->torank == 4))) { 344 (move->fromrank == 6 && move->torank == 4))) {
345 enpassant_threat_add(gamestate, move->tofile, move->torank); 345 enpassant_threat_add(gamestate, move->tofile, move->torank);
346 } 346 }
347 347
348 /* castling */ 348 /* castling */
349 bool castling_happend = false;
349 if (piece_type(move->piece) == KING && move->fromfile == fileidx('e')) { 350 if (piece_type(move->piece) == KING && move->fromfile == fileidx('e')) {
350 const Color color = piece_color(move->piece); 351 const Color color = piece_color(move->piece);
351 if (move->tofile == fileidx('g')) { 352 if (move->tofile == fileidx('g')) {
352 gamestate->board[move->torank][fileidx('h')] = 0; 353 gamestate->board[move->torank][fileidx('h')] = 0;
353 gamestate->board[move->torank][fileidx('f')] = mkpiece(ROOK, color); 354 gamestate->board[move->torank][fileidx('f')] = mkpiece(ROOK, color);
355 castling_happend = true;
354 } else if (move->tofile == fileidx('c')) { 356 } else if (move->tofile == fileidx('c')) {
355 gamestate->board[move->torank][fileidx('a')] = 0; 357 gamestate->board[move->torank][fileidx('a')] = 0;
356 gamestate->board[move->torank][fileidx('d')] = mkpiece(ROOK, color); 358 gamestate->board[move->torank][fileidx('d')] = mkpiece(ROOK, color);
359 castling_happend = true;
360 }
361 }
362 if (castling_happend) {
363 if (piece_color(move->piece) == WHITE) {
364 gamestate->castling.K = true;
365 gamestate->castling.Q = true;
366 } else {
367 gamestate->castling.k = true;
368 gamestate->castling.q = true;
369 }
370 } else {
371 if (piece_color(move->piece) == WHITE) {
372 if (move->fromrank == rankidx('1')) {
373 if (move->fromfile == fileidx('e')) {
374 gamestate->castling.K = gamestate->castling.Q = true;
375 } else if (move->fromfile == fileidx('h')) {
376 gamestate->castling.K = true;
377 } else if (move->fromfile == fileidx('a')) {
378 gamestate->castling.Q = true;
379 }
380 }
381 } else {
382 if (move->fromrank == rankidx('8')) {
383 if (move->fromfile == fileidx('e')) {
384 gamestate->castling.k = gamestate->castling.q = true;
385 } else if (move->fromfile == fileidx('h')) {
386 gamestate->castling.k = true;
387 } else if (move->fromfile == fileidx('a')) {
388 gamestate->castling.q = true;
389 }
390 }
357 } 391 }
358 } 392 }
359 393
360 /* add move to the moves array and the new position to the FEN array */ 394 /* add move to the moves array and the new position to the FEN array */
361 if (gamestate->movecount == gamestate->movecapacity) { 395 if (gamestate->movecount == gamestate->movecapacity) {
939 } 973 }
940 974
941 static int getlocation(const GameState *gamestate, Move *move) { 975 static int getlocation(const GameState *gamestate, Move *move) {
942 976
943 Color color = piece_color(move->piece); 977 Color color = piece_color(move->piece);
944 bool incheck = false; 978
945 if (gamestate->movecount > 0) {
946 incheck = is_check_position(gamestate);
947 }
948
949 Move candidates[16], *candidate = NULL; 979 Move candidates[16], *candidate = NULL;
950 size_t candidatecount; 980 size_t candidatecount;
951 981
952 /* determine all candidate moves and sort out the invalid ones */ 982 /* determine all candidate moves and sort out the invalid ones */
953 if (get_candidates(gamestate, move->tofile, move->torank, color, 983 if (get_candidates(gamestate, move->tofile, move->torank, color,
981 /* no valid candidate left */ 1011 /* no valid candidate left */
982 if (!candidate) { 1012 if (!candidate) {
983 if (found) { 1013 if (found) {
984 if (piece_type(move->piece) == KING) { 1014 if (piece_type(move->piece) == KING) {
985 return KING_MOVES_INTO_CHECK; 1015 return KING_MOVES_INTO_CHECK;
986 } else if (incheck) { 1016 } else if (is_check_position(gamestate)) {
987 return KING_IN_CHECK; 1017 return KING_IN_CHECK;
988 } else { 1018 } else {
989 return PIECE_PINNED; 1019 return PIECE_PINNED;
990 } 1020 }
991 } else { 1021 } else {

mercurial