Fix printing of book moves for double-digit ranks
[xboard.git] / book.c
diff --git a/book.c b/book.c
index 8b4a43c..c956362 100644 (file)
--- a/book.c
+++ b/book.c
@@ -313,8 +313,9 @@ hash (int moveNr)
             ChessSquare p = boards[moveNr][r][f];
            if(f == BOARD_LEFT-1 || f == BOARD_RGHT) continue; // between board and holdings
             if(p != EmptySquare){
-                   int j = (int)p;
+                   int j = (int)p, promoted = 0;
                    j -= (j >= (int)BlackPawn) ? (int)BlackPawn :(int)WhitePawn;
+                   if(j >= CHUPROMOTED WhitePawn) promoted++, j -= CHUPROMOTED WhitePawn;
                    if(j > (int)WhiteQueen) j++;  // make space for King
                    if(j > (int) WhiteKing) j = (int)WhiteQueen + 1;
                    p_enc = 2*j + ((int)p < (int)BlackPawn);
@@ -340,6 +341,7 @@ hash (int moveNr)
                                Zobrist = (Zobrist << 48) ^ (Zobrist >> 16);
                                break;
                    }
+                   if(promoted) Zobrist ^= 123456789*RandomPiece[squareNr & 63];
                    if(squareNr &  64) Zobrist = (Zobrist << 8) ^ (Zobrist >> 56);
                    if(squareNr & 128) Zobrist = (Zobrist << 4) ^ (Zobrist >> 60);
                    // holdings have separate (additive) key, to encode presence of multiple pieces on same square
@@ -483,7 +485,7 @@ find_key (FILE *f, uint64 key, entry_t *entry)
 }
 
 void
-move_to_string (char move_s[6], uint16 move)
+move_to_string (char move_s[10], uint16 move)
 {
     int f,fr,ff,t,tr,tf,p;
     int width = BOARD_RGHT - BOARD_LEFT, size; // allow for alternative board formats
@@ -497,10 +499,7 @@ move_to_string (char move_s[6], uint16 move)
     t  = move % size;
     tr = t / width;
     tf = t % width;
-    move_s[0] = ff + 'a';
-    move_s[1] = fr + '1' - (BOARD_HEIGHT > 9);
-    move_s[2] = tf + 'a';
-    move_s[3] = tr + '1' - (BOARD_HEIGHT > 9);
+    snprintf(move_s, 9, "%c%d%c%d", ff + 'a', fr + 1 - (BOARD_HEIGHT == 10), tf + 'a', tr + 1 - (BOARD_HEIGHT == 10));
 
     if(IS_SHOGI(gameInfo.variant) && p) {
        if(p == 2) p = 10;     // Lion moves, for boards so big that 10 is out of range
@@ -519,10 +518,9 @@ move_to_string (char move_s[6], uint16 move)
 
     // add promotion piece, if any
     if(p){
-        move_s[4] = promote_pieces[p];
-        move_s[5] = '\0';
-    }else{
-        move_s[4] = '\0';
+       int len = strlen(move_s);
+        move_s[len] = promote_pieces[p];
+        move_s[len+1] = '\0';
     }
 
     if(gameInfo.variant != VariantNormal) return;
@@ -573,6 +571,8 @@ GetBookMoves (FILE *f, int moveNr, entry_t entries[], int max)
     return count;
 }
 
+static int dirty;
+
 int
 ReadFromBookFile (int moveNr, char *book, entry_t entries[])
 {   // retrieve all entries for given position from book in 'entries', return number.
@@ -580,6 +580,7 @@ ReadFromBookFile (int moveNr, char *book, entry_t entries[])
     static char curBook[MSG_SIZ];
 
     if(book == NULL) return -1;
+    if(dirty) { if(f) fclose(f); dirty = 0; f = NULL; }
     if(!f || strcmp(book, curBook)){ // keep book file open until book changed
        strncpy(curBook, book, MSG_SIZ);
        if(f) fclose(f);
@@ -871,6 +872,7 @@ SaveToBook (char *text)
            writepos += len2;
        } while(len1);
     }
+    dirty = 1;
     fclose(f);
 }