1.1 --- a/src/chess/rules.c Wed Aug 29 13:45:13 2018 +0200 1.2 +++ b/src/chess/rules.c Wed Aug 29 13:55:18 2018 +0200 1.3 @@ -77,65 +77,69 @@ 1.4 /* at least 8 characters should be available, wipe them out */ 1.5 memset(string, 0, 8); 1.6 1.7 - /* special formats for castling */ 1.8 + unsigned int idx; 1.9 if ((move->piece&PIECE_MASK) == KING && 1.10 abs(move->tofile-move->fromfile) == 2) { 1.11 + /* special formats for castling */ 1.12 if (move->tofile==fileidx('c')) { 1.13 memcpy(string, "O-O-O", 5); 1.14 + idx = 5; 1.15 } else { 1.16 memcpy(string, "O-O", 3); 1.17 + idx = 3; 1.18 } 1.19 - } 1.20 + } else { 1.21 + /* start by notating the piece character */ 1.22 + string[0] = getpiecechr(move->piece); 1.23 + idx = string[0] ? 1 : 0; 1.24 1.25 - /* start by notating the piece character */ 1.26 - string[0] = getpiecechr(move->piece); 1.27 - int idx = string[0] ? 1 : 0; 1.28 - 1.29 - /* find out how many source information we do need */ 1.30 - uint8_t piece = move->piece & PIECE_MASK; 1.31 - if (piece == PAWN) { 1.32 - if (move->capture) { 1.33 - string[idx++] = filechr(move->fromfile); 1.34 - } 1.35 - } else if (piece != KING) { 1.36 - Move threats[16]; 1.37 - uint8_t threatcount; 1.38 - get_real_threats(gamestate, move->torow, move->tofile, 1.39 - move->piece&COLOR_MASK, threats, &threatcount); 1.40 - if (threatcount > 1) { 1.41 - int ambrows = 0, ambfiles = 0; 1.42 - for (uint8_t i = 0 ; i < threatcount ; i++) { 1.43 - if (threats[i].fromrow == move->fromrow) { 1.44 - ambrows++; 1.45 + /* find out how many source information we do need */ 1.46 + uint8_t piece = move->piece & PIECE_MASK; 1.47 + if (piece == PAWN) { 1.48 + if (move->capture) { 1.49 + string[idx++] = filechr(move->fromfile); 1.50 + } 1.51 + } else if (piece != KING) { 1.52 + /* resolve ambiguities, if any */ 1.53 + Move threats[16]; 1.54 + uint8_t threatcount; 1.55 + get_real_threats(gamestate, move->torow, move->tofile, 1.56 + move->piece&COLOR_MASK, threats, &threatcount); 1.57 + if (threatcount > 1) { 1.58 + int ambrows = 0, ambfiles = 0; 1.59 + for (uint8_t i = 0 ; i < threatcount ; i++) { 1.60 + if (threats[i].fromrow == move->fromrow) { 1.61 + ambrows++; 1.62 + } 1.63 + if (threats[i].fromfile == move->fromfile) { 1.64 + ambfiles++; 1.65 + } 1.66 } 1.67 - if (threats[i].fromfile == move->fromfile) { 1.68 - ambfiles++; 1.69 + /* ambiguous row, name file */ 1.70 + if (ambrows > 1) { 1.71 + string[idx++] = filechr(move->fromfile); 1.72 + } 1.73 + /* ambiguous file, name row */ 1.74 + if (ambfiles > 1) { 1.75 + string[idx++] = filechr(move->fromrow); 1.76 } 1.77 } 1.78 - /* ambiguous row, name file */ 1.79 - if (ambrows > 1) { 1.80 - string[idx++] = filechr(move->fromfile); 1.81 - } 1.82 - /* ambiguous file, name row */ 1.83 - if (ambfiles > 1) { 1.84 - string[idx++] = filechr(move->fromrow); 1.85 - } 1.86 } 1.87 - } 1.88 - 1.89 - /* capturing? */ 1.90 - if (move->capture) { 1.91 - string[idx++] = 'x'; 1.92 - } 1.93 - 1.94 - /* destination */ 1.95 - string[idx++] = filechr(move->tofile); 1.96 - string[idx++] = rowchr(move->torow); 1.97 - 1.98 - /* promotion? */ 1.99 - if (move->promotion) { 1.100 - string[idx++] = '='; 1.101 - string[idx++] = getpiecechr(move->promotion); 1.102 + 1.103 + /* capturing? */ 1.104 + if (move->capture) { 1.105 + string[idx++] = 'x'; 1.106 + } 1.107 + 1.108 + /* destination */ 1.109 + string[idx++] = filechr(move->tofile); 1.110 + string[idx++] = rowchr(move->torow); 1.111 + 1.112 + /* promotion? */ 1.113 + if (move->promotion) { 1.114 + string[idx++] = '='; 1.115 + string[idx++] = getpiecechr(move->promotion); 1.116 + } 1.117 } 1.118 1.119 /* check? */ 1.120 @@ -456,7 +460,7 @@ 1.121 for (uint8_t r = 0 ; r < 8 ; r++) { 1.122 for (uint8_t f = 0 ; f < 8 ; f++) { 1.123 if ((gamestate->board[r][f] & COLOR_MASK) == color) { 1.124 - // non-capturing move 1.125 + /* non-capturing move */ 1.126 memset(&(candidates[candidatecount]), 0, sizeof(Move)); 1.127 candidates[candidatecount].piece = gamestate->board[r][f]; 1.128 candidates[candidatecount].fromrow = r; 1.129 @@ -465,7 +469,7 @@ 1.130 candidates[candidatecount].tofile = file; 1.131 candidatecount++; 1.132 1.133 - // capturing move 1.134 + /* capturing move */ 1.135 memcpy(&(candidates[candidatecount]), 1.136 &(candidates[candidatecount-1]), sizeof(Move)); 1.137 candidates[candidatecount].capture = 1; 1.138 @@ -572,7 +576,7 @@ 1.139 1.140 int reason = INVALID_POSITION; 1.141 1.142 - // find threats for the specified position 1.143 + /* find threats for the specified position */ 1.144 for (uint8_t i = 0 ; i < threatcount ; i++) { 1.145 if ((threats[i].piece & (PIECE_MASK | COLOR_MASK)) 1.146 == move->piece && 1.147 @@ -584,7 +588,7 @@ 1.148 if (threat) { 1.149 return AMBIGUOUS_MOVE; 1.150 } else { 1.151 - // found threat is no real threat 1.152 + /* found threat is no real threat */ 1.153 if (is_pinned(gamestate, &(threats[i]))) { 1.154 reason = incheck?KING_IN_CHECK:PIECE_PINNED; 1.155 } else { 1.156 @@ -594,7 +598,7 @@ 1.157 } 1.158 } 1.159 1.160 - // can't threaten specified position 1.161 + /* can't threaten specified position */ 1.162 if (!threat) { 1.163 return reason; 1.164 }