src/chess/rules.c

changeset 62
564af8a16828
parent 55
54ea19938d57
child 63
611332453da0
     1.1 --- a/src/chess/rules.c	Tue Aug 28 15:45:44 2018 +0200
     1.2 +++ b/src/chess/rules.c	Tue Aug 28 15:56:33 2018 +0200
     1.3 @@ -327,17 +327,22 @@
     1.4          return result;
     1.5      }
     1.6      
     1.7 +    /* simulate move for check validation */
     1.8 +    GameState simulation = gamestate_copy_sim(gamestate);
     1.9 +    Move simmove = *move;
    1.10 +    apply_move_impl(&simulation, &simmove, 1);
    1.11 +    
    1.12      /* find kings for check validation */
    1.13      uint8_t piececolor = (move->piece & COLOR_MASK);
    1.14      
    1.15      uint8_t mykingfile = 0, mykingrow = 0, opkingfile = 0, opkingrow = 0;
    1.16      for (uint8_t row = 0 ; row < 8 ; row++) {
    1.17          for (uint8_t file = 0 ; file < 8 ; file++) {
    1.18 -            if (gamestate->board[row][file] ==
    1.19 +            if (simulation.board[row][file] ==
    1.20                      (piececolor == WHITE?WKING:BKING)) {
    1.21                  mykingfile = file;
    1.22                  mykingrow = row;
    1.23 -            } else if (gamestate->board[row][file] ==
    1.24 +            } else if (simulation.board[row][file] ==
    1.25                      (piececolor == WHITE?BKING:WKING)) {
    1.26                  opkingfile = file;
    1.27                  opkingrow = row;
    1.28 @@ -345,11 +350,6 @@
    1.29          }
    1.30      }
    1.31      
    1.32 -    /* simulate move for check validation */
    1.33 -    GameState simulation = gamestate_copy_sim(gamestate);
    1.34 -    Move simmove = *move;
    1.35 -    apply_move_impl(&simulation, &simmove, 1);
    1.36 -    
    1.37      /* don't move into or stay in check position */
    1.38      if (is_covered(&simulation, mykingrow, mykingfile,
    1.39          opponent_color(piececolor))) {
    1.40 @@ -495,19 +495,20 @@
    1.41  _Bool is_pinned(GameState *gamestate, Move *move) {
    1.42      uint8_t color = move->piece & COLOR_MASK;
    1.43  
    1.44 +    GameState simulation = gamestate_copy_sim(gamestate);
    1.45 +    Move simmove = *move;
    1.46 +    apply_move(&simulation, &simmove);
    1.47 +    
    1.48      uint8_t kingfile = 0, kingrow = 0;
    1.49      for (uint8_t row = 0 ; row < 8 ; row++) {
    1.50          for (uint8_t file = 0 ; file < 8 ; file++) {
    1.51 -            if (gamestate->board[row][file] == (color|KING)) {
    1.52 +            if (simulation.board[row][file] == (color|KING)) {
    1.53                  kingfile = file;
    1.54                  kingrow = row;
    1.55              }
    1.56          }
    1.57      }
    1.58 -
    1.59 -    GameState simulation = gamestate_copy_sim(gamestate);
    1.60 -    Move simmove = *move;
    1.61 -    apply_move(&simulation, &simmove);
    1.62 +    
    1.63      _Bool covered = is_covered(&simulation,
    1.64          kingrow, kingfile, opponent_color(color));
    1.65      gamestate_cleanup(&simulation);
    1.66 @@ -570,7 +571,7 @@
    1.67          int reason = INVALID_POSITION;
    1.68          
    1.69          // find threats for the specified position
    1.70 -        for (uint8_t i = 0 ; i < threatcount ; i++) {
    1.71 +        for (uint8_t i = 0 ; i < threatcount ; i++) {            
    1.72              if ((threats[i].piece & (PIECE_MASK | COLOR_MASK))
    1.73                      == move->piece &&
    1.74                      (move->fromrow == POS_UNSPECIFIED ||

mercurial