fixes ambiguity resolver in PGN output not resolving ambiguities for diagonally attacking Knights

Wed, 29 Aug 2018 15:12:24 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 29 Aug 2018 15:12:24 +0200
changeset 66
f5cc75565f7c
parent 65
dcc5bd2c56c0
child 67
c76e46970a59

fixes ambiguity resolver in PGN output not resolving ambiguities for diagonally attacking Knights

src/chess/rules.c file | annotate | diff | comparison | revisions
src/terminal-chess.h file | annotate | diff | comparison | revisions
     1.1 --- a/src/chess/rules.c	Wed Aug 29 14:01:41 2018 +0200
     1.2 +++ b/src/chess/rules.c	Wed Aug 29 15:12:24 2018 +0200
     1.3 @@ -35,6 +35,7 @@
     1.4  
     1.5  static GameState gamestate_copy_sim(GameState *gamestate) {
     1.6      GameState simulation = *gamestate;
     1.7 +    simulation.movecount = 0; /* simulations do not count moves */
     1.8      if (simulation.lastmove) {
     1.9          MoveList *lastmovecopy = malloc(sizeof(MoveList));
    1.10          *lastmovecopy = *(simulation.lastmove);
    1.11 @@ -70,9 +71,9 @@
    1.12      };
    1.13  }
    1.14  
    1.15 -/* MUST be called IMMEDIATLY after applying a move to work correctly */
    1.16 +/* MUST be called BEFORE applying a move to work correctly */
    1.17  static void format_move(GameState *gamestate, Move *move) {
    1.18 -    char *string = move->string;
    1.19 +    char *string = &(move->string[0]);
    1.20      
    1.21      /* at least 8 characters should be available, wipe them out */
    1.22      memset(string, 0, 8);
    1.23 @@ -103,25 +104,34 @@
    1.24              /* resolve ambiguities, if any */
    1.25              Move threats[16];
    1.26              uint8_t threatcount;
    1.27 -            get_real_threats(gamestate, move->torow, move->tofile,
    1.28 -                move->piece&COLOR_MASK, threats, &threatcount);
    1.29 -            if (threatcount > 1) {
    1.30 -                int ambrows = 0, ambfiles = 0;
    1.31 +            if (get_threats(gamestate, move->torow, move->tofile,
    1.32 +                    move->piece&COLOR_MASK, threats, &threatcount)) {
    1.33 +                unsigned int ambrows = 0, ambfiles = 0, ambpiece = 0;
    1.34                  for (uint8_t i = 0 ; i < threatcount ; i++) {
    1.35 -                    if (threats[i].fromrow == move->fromrow) {
    1.36 -                        ambrows++;
    1.37 -                    }
    1.38 -                    if (threats[i].fromfile == move->fromfile) {
    1.39 -                        ambfiles++;
    1.40 +                    if (threats[i].piece == move->piece) {
    1.41 +                        ambpiece++;
    1.42 +                        if (threats[i].fromrow == move->fromrow) {
    1.43 +                            ambrows++;
    1.44 +                        }
    1.45 +                        if (threats[i].fromfile == move->fromfile) {
    1.46 +                            ambfiles++;
    1.47 +                        }
    1.48                      }
    1.49                  }
    1.50 -                /* ambiguous row, name file */
    1.51 -                if (ambrows > 1) {
    1.52 +                /* neither file, nor row are ambiguous, name file */
    1.53 +                if (ambpiece > 1 && ambrows == 1 && ambfiles == 1) {
    1.54 +                    /* this is most likely the case with Knights
    1.55 +                     * in diagonal opposition */
    1.56                      string[idx++] = filechr(move->fromfile);
    1.57 -                }
    1.58 -                /* ambiguous file, name row */
    1.59 -                if (ambfiles > 1) {
    1.60 -                    string[idx++] = filechr(move->fromrow);
    1.61 +                } else {
    1.62 +                    /* ambiguous row, name file */
    1.63 +                    if (ambrows > 1) {
    1.64 +                        string[idx++] = filechr(move->fromfile);
    1.65 +                    }
    1.66 +                    /* ambiguous file, name row */
    1.67 +                    if (ambfiles > 1) {
    1.68 +                        string[idx++] = filechr(move->fromrow);
    1.69 +                    }
    1.70                  }
    1.71              }
    1.72          }
    1.73 @@ -144,7 +154,7 @@
    1.74      
    1.75      /* check? */
    1.76      if (move->check) {
    1.77 -        /* works only, if this function is called when applying the move */
    1.78 +        // TODO: does not work, because checkmate is not set when format_move is called
    1.79          string[idx++] = gamestate->checkmate?'#':'+';
    1.80      }
    1.81  }
    1.82 @@ -207,6 +217,13 @@
    1.83  }
    1.84  
    1.85  static void apply_move_impl(GameState *gamestate, Move *move, _Bool simulate) {
    1.86 +    /* format move before moving (s.t. ambiguities can be resolved) */
    1.87 +    if (!simulate) {
    1.88 +        if (!move->string[0]) {
    1.89 +            format_move(gamestate, move);
    1.90 +        }
    1.91 +    }
    1.92 +    
    1.93      uint8_t piece = move->piece & PIECE_MASK;
    1.94      uint8_t color = move->piece & COLOR_MASK;
    1.95      
    1.96 @@ -239,7 +256,6 @@
    1.97      
    1.98      /* castling */
    1.99      if (piece == KING && move->fromfile == fileidx('e')) {
   1.100 -        
   1.101          if (move->tofile == fileidx('g')) {
   1.102              gamestate->board[move->torow][fileidx('h')] = 0;
   1.103              gamestate->board[move->torow][fileidx('f')] = color|ROOK;
   1.104 @@ -248,12 +264,7 @@
   1.105              gamestate->board[move->torow][fileidx('d')] = color|ROOK;
   1.106          }
   1.107      }
   1.108 -
   1.109 -    if (!simulate) {
   1.110 -        if (!move->string[0]) {
   1.111 -            format_move(gamestate, move);
   1.112 -        }
   1.113 -    }
   1.114 +    
   1.115      /* add move, even in simulation (checkmate test needs it) */
   1.116      addmove(gamestate, move);
   1.117  }
     2.1 --- a/src/terminal-chess.h	Wed Aug 29 14:01:41 2018 +0200
     2.2 +++ b/src/terminal-chess.h	Wed Aug 29 15:12:24 2018 +0200
     2.3 @@ -36,7 +36,7 @@
     2.4  #ifndef TERMINAL_CHESS_H
     2.5  #define	TERMINAL_CHESS_H
     2.6  
     2.7 -#define PROGRAM_VERSION "0.9-r60"
     2.8 +#define PROGRAM_VERSION "0.9-r66"
     2.9  
    2.10  #ifdef	__cplusplus
    2.11  extern "C" {

mercurial