src/chess/rules.c

changeset 64
4eda5df55f86
parent 63
611332453da0
child 66
f5cc75565f7c
--- a/src/chess/rules.c	Wed Aug 29 13:45:13 2018 +0200
+++ b/src/chess/rules.c	Wed Aug 29 13:55:18 2018 +0200
@@ -77,65 +77,69 @@
     /* at least 8 characters should be available, wipe them out */
     memset(string, 0, 8);
     
-    /* special formats for castling */
+    unsigned int idx;
     if ((move->piece&PIECE_MASK) == KING &&
             abs(move->tofile-move->fromfile) == 2) {
+        /* special formats for castling */
         if (move->tofile==fileidx('c')) {
             memcpy(string, "O-O-O", 5);
+            idx = 5;
         } else {
             memcpy(string, "O-O", 3);
-        }
-    }
-
-    /* start by notating the piece character */
-    string[0] = getpiecechr(move->piece);
-    int idx = string[0] ? 1 : 0;
-    
-    /* find out how many source information we do need */
-    uint8_t piece = move->piece & PIECE_MASK;
-    if (piece == PAWN) {
-        if (move->capture) {
-            string[idx++] = filechr(move->fromfile);
+            idx = 3;
         }
-    } else if (piece != KING) {
-        Move threats[16];
-        uint8_t threatcount;
-        get_real_threats(gamestate, move->torow, move->tofile,
-            move->piece&COLOR_MASK, threats, &threatcount);
-        if (threatcount > 1) {
-            int ambrows = 0, ambfiles = 0;
-            for (uint8_t i = 0 ; i < threatcount ; i++) {
-                if (threats[i].fromrow == move->fromrow) {
-                    ambrows++;
+    } else {
+        /* start by notating the piece character */
+        string[0] = getpiecechr(move->piece);
+        idx = string[0] ? 1 : 0;
+
+        /* find out how many source information we do need */
+        uint8_t piece = move->piece & PIECE_MASK;
+        if (piece == PAWN) {
+            if (move->capture) {
+                string[idx++] = filechr(move->fromfile);
+            }
+        } else if (piece != KING) {
+            /* resolve ambiguities, if any */
+            Move threats[16];
+            uint8_t threatcount;
+            get_real_threats(gamestate, move->torow, move->tofile,
+                move->piece&COLOR_MASK, threats, &threatcount);
+            if (threatcount > 1) {
+                int ambrows = 0, ambfiles = 0;
+                for (uint8_t i = 0 ; i < threatcount ; i++) {
+                    if (threats[i].fromrow == move->fromrow) {
+                        ambrows++;
+                    }
+                    if (threats[i].fromfile == move->fromfile) {
+                        ambfiles++;
+                    }
                 }
-                if (threats[i].fromfile == move->fromfile) {
-                    ambfiles++;
+                /* ambiguous row, name file */
+                if (ambrows > 1) {
+                    string[idx++] = filechr(move->fromfile);
+                }
+                /* ambiguous file, name row */
+                if (ambfiles > 1) {
+                    string[idx++] = filechr(move->fromrow);
                 }
             }
-            /* ambiguous row, name file */
-            if (ambrows > 1) {
-                string[idx++] = filechr(move->fromfile);
-            }
-            /* ambiguous file, name row */
-            if (ambfiles > 1) {
-                string[idx++] = filechr(move->fromrow);
-            }
+        }
+
+        /* capturing? */
+        if (move->capture) {
+            string[idx++] = 'x';
         }
-    }
-    
-    /* capturing? */
-    if (move->capture) {
-        string[idx++] = 'x';
-    }
-    
-    /* destination */
-    string[idx++] = filechr(move->tofile);
-    string[idx++] = rowchr(move->torow);
-    
-    /* promotion? */
-    if (move->promotion) {
-        string[idx++] = '=';
-        string[idx++] = getpiecechr(move->promotion);
+
+        /* destination */
+        string[idx++] = filechr(move->tofile);
+        string[idx++] = rowchr(move->torow);
+
+        /* promotion? */
+        if (move->promotion) {
+            string[idx++] = '=';
+            string[idx++] = getpiecechr(move->promotion);
+        }
     }
     
     /* check? */
@@ -456,7 +460,7 @@
     for (uint8_t r = 0 ; r < 8 ; r++) {
         for (uint8_t f = 0 ; f < 8 ; f++) {
             if ((gamestate->board[r][f] & COLOR_MASK) == color) {
-                // non-capturing move
+                /* non-capturing move */
                 memset(&(candidates[candidatecount]), 0, sizeof(Move));
                 candidates[candidatecount].piece = gamestate->board[r][f];
                 candidates[candidatecount].fromrow = r;
@@ -465,7 +469,7 @@
                 candidates[candidatecount].tofile = file;
                 candidatecount++;
 
-                // capturing move
+                /* capturing move */
                 memcpy(&(candidates[candidatecount]),
                     &(candidates[candidatecount-1]), sizeof(Move));
                 candidates[candidatecount].capture = 1;
@@ -572,7 +576,7 @@
         
         int reason = INVALID_POSITION;
         
-        // find threats for the specified position
+        /* find threats for the specified position */
         for (uint8_t i = 0 ; i < threatcount ; i++) {            
             if ((threats[i].piece & (PIECE_MASK | COLOR_MASK))
                     == move->piece &&
@@ -584,7 +588,7 @@
                 if (threat) {
                     return AMBIGUOUS_MOVE;
                 } else {
-                    // found threat is no real threat
+                    /* found threat is no real threat */
                     if (is_pinned(gamestate, &(threats[i]))) {
                         reason = incheck?KING_IN_CHECK:PIECE_PINNED;
                     } else {
@@ -594,7 +598,7 @@
             }
         }
         
-        // can't threaten specified position
+        /* can't threaten specified position */
         if (!threat) {
             return reason;
         }

mercurial