X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=book.c;h=e42b9c6e5099e0a3560bcd28b384e2d26dca88fc;hb=86473f8c47a82552a7766df71bdd7e99a4830181;hp=e5a6030bb67b533e2ef3247b075dc42e0456d5f6;hpb=9b980cf4a3a0e555c16f50b7b198773a3fd2d8f2;p=xboard.git diff --git a/book.c b/book.c index e5a6030..e42b9c6 100644 --- a/book.c +++ b/book.c @@ -36,6 +36,7 @@ #include "common.h" #include "backend.h" +#include "moves.h" #ifdef _MSC_VER typedef unsigned __int64 uint64; @@ -276,17 +277,21 @@ uint64 *RandomTurn =Random64+780; uint64 hash(int moveNr) { int r, f, p_enc, squareNr, pieceGroup; - uint64 key=0, Zobrist; + uint64 key=0, holdingsKey=0, Zobrist; - for(f=BOARD_LEFT; f= (int)BlackPawn) ? (int)BlackPawn :(int)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); + // holdings squares get nmbers immediately after board; first left, then right holdings + if(f == BOARD_LEFT-2) squareNr = (BOARD_RGHT - BOARD_LEFT)*BOARD_HEIGHT + r; else + if(f == BOARD_RGHT+1) squareNr = (BOARD_RGHT - BOARD_LEFT + 1)*BOARD_HEIGHT + r; else squareNr = (BOARD_RGHT - BOARD_LEFT)*r + (f - BOARD_LEFT); // note that in normal Chess squareNr < 64 and p_enc < 12. The following code // maps other pieces and squares in this range, and then modify the corresponding @@ -306,11 +311,13 @@ uint64 hash(int moveNr) break; } if(squareNr >= 64) Zobrist = (Zobrist << 8) ^ (Zobrist >> 56); + // holdings have separate (additive) key, to encode presence of multiple pieces on same square + if(f == BOARD_LEFT-2) holdingsKey += Zobrist * boards[moveNr][r][f+1]; else + if(f == BOARD_RGHT+1) holdingsKey += Zobrist * boards[moveNr][r][f-1]; else key ^= Zobrist; } } } - // Holdings not implemented yet! if(boards[moveNr][CASTLING][2] != NoRights) { if(boards[moveNr][CASTLING][0] != NoRights) key^=RandomCastle[0]; @@ -341,7 +348,7 @@ uint64 hash(int moveNr) if(WhiteOnMove(moveNr)){ key^=RandomTurn[0]; } - return key; + return key + holdingsKey; } #define MOVE_BUF 100 @@ -461,7 +468,8 @@ void move_to_string(char move_s[6], uint16 move) int GetBookMoves(int moveNr, char *book, entry_t entries[]) { // retrieve all entries for given position from book in 'entries', return number. - FILE *f; + static FILE *f = NULL; + static char curBook[MSG_SIZ]; entry_t entry; int offset; uint64 key; @@ -470,7 +478,11 @@ int GetBookMoves(int moveNr, char *book, entry_t entries[]) if(book == NULL || moveNr >= 2*appData.bookDepth) return -1; // if(gameInfo.variant != VariantNormal) return -1; // Zobrist scheme only works for normal Chess, so far - f=fopen(book,"rb"); + if(!f || strcmp(book, curBook)){ // keep book file open until book changed + strncpy(curBook, book, MSG_SIZ); + if(f) fclose(f); + f = fopen(book,"rb"); + } if(!f){ DisplayError("Polyglot book not valid", 0); appData.usePolyglotBook = FALSE; @@ -482,7 +494,6 @@ int GetBookMoves(int moveNr, char *book, entry_t entries[]) offset=find_key(f, key, &entry); if(entry.key != key) { - fclose(f); return FALSE; } entries[0] = entry; @@ -499,7 +510,6 @@ int GetBookMoves(int moveNr, char *book, entry_t entries[]) if(count == MOVE_BUF) break; entries[count++] = entry; } - fclose(f); return count; } @@ -527,6 +537,7 @@ char *ProbeBook(int moveNr, char *book) for(i=0; i> 12; // create random < total_weight total_weight = 0; for(i=0; i