fixes move validation working on old king's position, when the king moves

Tue, 28 Aug 2018 15:56:33 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Aug 2018 15:56:33 +0200
changeset 62
564af8a16828
parent 61
e3a1a794351e
child 63
611332453da0

fixes move validation working on old king's position, when the king moves

Fischer-Spassky-1992.pgn file | annotate | diff | comparison | revisions
src/chess/rules.c file | annotate | diff | comparison | revisions
     1.1 --- a/Fischer-Spassky-1992.pgn	Tue Aug 28 15:45:44 2018 +0200
     1.2 +++ b/Fischer-Spassky-1992.pgn	Tue Aug 28 15:56:33 2018 +0200
     1.3 @@ -13,5 +13,5 @@
     1.4  23. Ne5 Rae8 24. Bxf7+ Rxf7 25. Nxf7 Rxe1+ 26. Qxe1 Kxf7 27. Qe3 Qg5 28. Qxg5
     1.5  hxg5 29. b3 Ke6 30. a3 Kd6 31. axb4 cxb4 32. Ra5 Nd5 33. f3 Bc8 34. Kf2 Bf5
     1.6  35. Ra7 g6 36. Ra6+ Kc5 37. Ke1 Nf4 38. g3 Nxh3 39. Kd2 Kb5 40. Rd6 Kc5 41. Ra6
     1.7 -Nf2 42. g4 Bd3 43. Re6 1/2-1/2
     1.8 +Nf2 42. g4 Bd3 43. Re6 
     1.9  
     2.1 --- a/src/chess/rules.c	Tue Aug 28 15:45:44 2018 +0200
     2.2 +++ b/src/chess/rules.c	Tue Aug 28 15:56:33 2018 +0200
     2.3 @@ -327,17 +327,22 @@
     2.4          return result;
     2.5      }
     2.6      
     2.7 +    /* simulate move for check validation */
     2.8 +    GameState simulation = gamestate_copy_sim(gamestate);
     2.9 +    Move simmove = *move;
    2.10 +    apply_move_impl(&simulation, &simmove, 1);
    2.11 +    
    2.12      /* find kings for check validation */
    2.13      uint8_t piececolor = (move->piece & COLOR_MASK);
    2.14      
    2.15      uint8_t mykingfile = 0, mykingrow = 0, opkingfile = 0, opkingrow = 0;
    2.16      for (uint8_t row = 0 ; row < 8 ; row++) {
    2.17          for (uint8_t file = 0 ; file < 8 ; file++) {
    2.18 -            if (gamestate->board[row][file] ==
    2.19 +            if (simulation.board[row][file] ==
    2.20                      (piececolor == WHITE?WKING:BKING)) {
    2.21                  mykingfile = file;
    2.22                  mykingrow = row;
    2.23 -            } else if (gamestate->board[row][file] ==
    2.24 +            } else if (simulation.board[row][file] ==
    2.25                      (piececolor == WHITE?BKING:WKING)) {
    2.26                  opkingfile = file;
    2.27                  opkingrow = row;
    2.28 @@ -345,11 +350,6 @@
    2.29          }
    2.30      }
    2.31      
    2.32 -    /* simulate move for check validation */
    2.33 -    GameState simulation = gamestate_copy_sim(gamestate);
    2.34 -    Move simmove = *move;
    2.35 -    apply_move_impl(&simulation, &simmove, 1);
    2.36 -    
    2.37      /* don't move into or stay in check position */
    2.38      if (is_covered(&simulation, mykingrow, mykingfile,
    2.39          opponent_color(piececolor))) {
    2.40 @@ -495,19 +495,20 @@
    2.41  _Bool is_pinned(GameState *gamestate, Move *move) {
    2.42      uint8_t color = move->piece & COLOR_MASK;
    2.43  
    2.44 +    GameState simulation = gamestate_copy_sim(gamestate);
    2.45 +    Move simmove = *move;
    2.46 +    apply_move(&simulation, &simmove);
    2.47 +    
    2.48      uint8_t kingfile = 0, kingrow = 0;
    2.49      for (uint8_t row = 0 ; row < 8 ; row++) {
    2.50          for (uint8_t file = 0 ; file < 8 ; file++) {
    2.51 -            if (gamestate->board[row][file] == (color|KING)) {
    2.52 +            if (simulation.board[row][file] == (color|KING)) {
    2.53                  kingfile = file;
    2.54                  kingrow = row;
    2.55              }
    2.56          }
    2.57      }
    2.58 -
    2.59 -    GameState simulation = gamestate_copy_sim(gamestate);
    2.60 -    Move simmove = *move;
    2.61 -    apply_move(&simulation, &simmove);
    2.62 +    
    2.63      _Bool covered = is_covered(&simulation,
    2.64          kingrow, kingfile, opponent_color(color));
    2.65      gamestate_cleanup(&simulation);
    2.66 @@ -570,7 +571,7 @@
    2.67          int reason = INVALID_POSITION;
    2.68          
    2.69          // find threats for the specified position
    2.70 -        for (uint8_t i = 0 ; i < threatcount ; i++) {
    2.71 +        for (uint8_t i = 0 ; i < threatcount ; i++) {            
    2.72              if ((threats[i].piece & (PIECE_MASK | COLOR_MASK))
    2.73                      == move->piece &&
    2.74                      (move->fromrow == POS_UNSPECIFIED ||

mercurial