diff -r e3a1a794351e -r 564af8a16828 src/chess/rules.c --- a/src/chess/rules.c Tue Aug 28 15:45:44 2018 +0200 +++ b/src/chess/rules.c Tue Aug 28 15:56:33 2018 +0200 @@ -327,17 +327,22 @@ return result; } + /* simulate move for check validation */ + GameState simulation = gamestate_copy_sim(gamestate); + Move simmove = *move; + apply_move_impl(&simulation, &simmove, 1); + /* find kings for check validation */ uint8_t piececolor = (move->piece & COLOR_MASK); uint8_t mykingfile = 0, mykingrow = 0, opkingfile = 0, opkingrow = 0; for (uint8_t row = 0 ; row < 8 ; row++) { for (uint8_t file = 0 ; file < 8 ; file++) { - if (gamestate->board[row][file] == + if (simulation.board[row][file] == (piececolor == WHITE?WKING:BKING)) { mykingfile = file; mykingrow = row; - } else if (gamestate->board[row][file] == + } else if (simulation.board[row][file] == (piececolor == WHITE?BKING:WKING)) { opkingfile = file; opkingrow = row; @@ -345,11 +350,6 @@ } } - /* simulate move for check validation */ - GameState simulation = gamestate_copy_sim(gamestate); - Move simmove = *move; - apply_move_impl(&simulation, &simmove, 1); - /* don't move into or stay in check position */ if (is_covered(&simulation, mykingrow, mykingfile, opponent_color(piececolor))) { @@ -495,19 +495,20 @@ _Bool is_pinned(GameState *gamestate, Move *move) { uint8_t color = move->piece & COLOR_MASK; + GameState simulation = gamestate_copy_sim(gamestate); + Move simmove = *move; + apply_move(&simulation, &simmove); + uint8_t kingfile = 0, kingrow = 0; for (uint8_t row = 0 ; row < 8 ; row++) { for (uint8_t file = 0 ; file < 8 ; file++) { - if (gamestate->board[row][file] == (color|KING)) { + if (simulation.board[row][file] == (color|KING)) { kingfile = file; kingrow = row; } } } - - GameState simulation = gamestate_copy_sim(gamestate); - Move simmove = *move; - apply_move(&simulation, &simmove); + _Bool covered = is_covered(&simulation, kingrow, kingfile, opponent_color(color)); gamestate_cleanup(&simulation); @@ -570,7 +571,7 @@ int reason = INVALID_POSITION; // find threats for the specified position - for (uint8_t i = 0 ; i < threatcount ; i++) { + for (uint8_t i = 0 ; i < threatcount ; i++) { if ((threats[i].piece & (PIECE_MASK | COLOR_MASK)) == move->piece && (move->fromrow == POS_UNSPECIFIED ||