void BOEKIN(void) /*update BORD and HASHKEY*/ { int sc,z,y0; char kntab[] = { 0,0,2,0,0,0,0,1,3,0,0,0,0,1 }; sc=_BORD[XD]; y0=_BORD[XE]; z=sc<<6; z=z+(sc<<4); HK1=HK1 ^ RANDOM1[z+XD]; HK1=HK1 ^ RANDOM1[z+XE]; HK2=HK2 ^ RANDOM2[z+XD]; HK2=HK2 ^ RANDOM2[z+XE]; _BORD[XE]=sc; _BORD[XD]=1; if (y0!=1) /*remove captured piece from hashkey*/ { z=y0<<6; z=z+(y0<<4); HK1=HK1 ^ RANDOM1[z+XE]; HK2=HK2 ^ RANDOM2[z+XE]; } if (kntab[sc]==1) /*handle castling*/ { if (XE-XD==20) { _BORD[XE+10]=1; _BORD[XE-10]=sc-2; MAKE_HASHKEY(); } if (XE-XD==-20) { _BORD[XE-20]=1; _BORD[XE+10]=sc-2; MAKE_HASHKEY(); } } if (sc==2 && y0==1 && XE-XD!=1 && XE-XD!=2) { _BORD[XE-1]=1; MAKE_HASHKEY(); } /*ep (white)*/ if (sc==8 && y0==1 && XD-XE!=1 && XD-XE!=2) { _BORD[XE+1]=1; MAKE_HASHKEY(); } /*ep (black)*/ }
int FIND_OPENING(const char *EPD,char *From,char *To, char *From1,char *From2,char *To1,char *To2,int *Bookmvs) /*input : BOOKFILE / EPD*/ /*output: FROM and TO as E2 and E4 and ERROR*/ { int xl,px,x,y,rnd,bhk1,bhk2,xd,xe; int ch,by,v,kleur,boekfase; char db[100],eb[100]; unsigned char zcd[200],zce[200],vb[100]; char fentab[] = "??PNBRQKpnbrqk??"; char fenkleur[] = "wb??"; Error=0; From[0]=0; To[0]=0; db[0]=0; eb[0]=0; vb[0]=255; rnd = rand() % 9999; AZ=0; /*number of found book moves*/ boekfase=0; /*search tournament book first*/ if (_GB==NULL) boekfase=1; /*no tournament book selected -> search main book*/ /* retrieve EPD*/ /* ============*/ for (x=1; x<=78; x++) if (_BORD[x] > 0) _BORD[x]=1; /*empty board*/ x=-1; y=0; next: x++; ch=EPD[x]; if (ch=='/') goto next; if (ch >= '1' && ch <= '8') { v=ch-48; y=y+v; goto test; } for (v=2; v<=15; v++) if (fentab[v]==ch) { _BORD[BORDPOS[y]]=v; y++; break; } if (v>13) goto error; test: if (y < 64) goto next; x++; if (EPD[x] != ' ') goto error; x++; if (EPD[x]==fenkleur[0]) { kleur=0; goto done; } /*white to move*/ if (EPD[x]==fenkleur[1]) { kleur=1; goto done; } /*black to move*/ error: Error=3; return Error; /*error in EPD string*/ done: MAKE_HASHKEY(); bhk1=HK1; bhk2=HK2; /*make hashkey of retrieved EPD*/ again: xl=0; px=-1; boek10: px++; boek12: if (boekfase){ /*read main book*/ if( _MNB[xl] ==255 && _MNB[xl+1]==255) goto boek90; zce[px]=_MNB[xl+1]; zcd[px]=_MNB[xl]; } else { if (_GB[xl]==255 && _GB[xl+1]==255) goto boek90; /*read tournament book*/ if (_GB[xl+1]==255 && _GB[xl+2]==255) goto boek90; zce[px]=_GB[xl+1]; zcd[px]=_GB[xl];} xl+=2; if (zcd[px] < 128) goto boek10; /*collect moves till end of variation*/ /* Search the filled stack for possible bookmove*/ /* =============================================*/ x=0; BOEK_BEGIN(); HK1=0x05035c45; HK2=0xf1b92b1f; /*hashkey values start position*/ boek14: if (bhk1==HK1 && bhk2==HK2) goto boek16; /*hashkeys match, bookmove found*/ XD=BVELD[zcd[x] & 0x3f]; XE=BVELD[zce[x] & 0x3f]; BOEKIN(); /*update board and hashkeys*/ x++; if (x > px) goto boek20; /*end of variantion, check for next*/ goto boek14; /* Bookmove found, insert in table*/ /* ===============================*/ boek16: if (zce[x]<64) goto boek20; /*Bad book move, no insert*/ if ((x&1) != (kleur&1)) goto boek20; /*colours don't match, no insert*/ xd=BVELD[zcd[x]&0x3f]; xe=BVELD[zce[x]&0x3f]; /*Bookmove found*/ for (v=0; v<AZ; v++) /*skip possible doubles*/ { if (db[v]==xd && eb[v]==xe && vb[v]<64) vb[v]=zce[x]; if (db[v]==xd && eb[v]==xe) goto boek20; } db[AZ]=xd; eb[AZ]=xe; vb[AZ]=zce[x]; /*store move in internal format*/ From1[AZ]=TA[xd]; From2[AZ]=TN[xd]; /*store move in ascii format*/ To1[AZ]=TA[xe]; To2[AZ]=TN[xe]; AZ++; /*number of found book moves*/ /* Spool back till bit-6 becomes zero*/ /* ==================================*/ boek20: if (zcd[px] < 192) goto boek12; /*there is another one !*/ boek22: px--; if (px < 0) goto boek10; if (zcd[px] >= 64) goto boek22; goto boek12; /* End of File, decide move to play*/ /* ================================*/ boek90: if (boekfase==0 && AZ==0) { boekfase=1; goto again; } /*nothing found in tournament book*/ /*search now the main book*/ if (AZ==0) goto einde; /*no book move(s) found*/ boek92: rnd=rnd & 63; by=-1; boek94: by++; if (by >= AZ) goto boek92; rnd=rnd-BOEKRND[by]; if (rnd > 0) goto boek94; x=db[by]; From[0]=TA[x]; From[1]=TN[x]; From[2]=0; x=eb[by]; To[0]=TA[x]; To[1]=TN[x]; To[2]=0; einde: *Bookmvs=AZ; return Error; }
void Book::FIND_OPENING() // input : BOOKFILE / EPD // output: FROM and TO as E2 and E4 and ERROR { int xl,px,xx,x,y,rnd,bhk1,bhk2,xd,xe; int ch,by,v,kleur,boekfase; char db[100],eb[100]; unsigned char zcd[200],zce[200],vb[100]; char fentab[] = "??PNBRQKpnbrqk??"; char fenkleur[] = "wb??"; ERROR=0; if ((FPB = fopen(BOOKFILE,"rb")) == NULL) { ERROR=2; return; } FROM[0]=0; TO[0]=0; db[0]=0; eb[0]=0; vb[0]=255; RND=clock(); // initialize randomizer x=rand()%9999; rnd=RND+x; RND=rnd; // debug info AZ=0; // number of found book moves boekfase=1; // no tournament book selected -> search main book // retrieve EPD // ============ for (x=1; x<=78; x++) if (_BORD[x] > 0) _BORD[x]=1; // empty board x=-1; y=0; next: x++; ch=EPD[x]; if (ch=='/') goto next; if (ch >= '1' && ch <= '8') { v=ch-48; y=y+v; goto test; } for (v=2; v<=15; v++) if (fentab[v]==ch) { _BORD[(int)BORDPOS[y]]=v; y++; break; } if (v>13) goto error; test: if (y < 64) goto next; x++; if (EPD[x] != ' ') goto error; x++; if (EPD[x]==fenkleur[0]) { kleur=0; goto done; } // white to move if (EPD[x]==fenkleur[1]) { kleur=1; goto done; } // black to move error: ERROR=3; fclose(FPB); return; done: MAKE_HASHKEY(); bhk1=HK1; bhk2=HK2; // make hashkey of retrieved EPD again: xl=-2; px=-1; boek10: px++; boek12: if ((xx = fgetc(FPB)) == EOF) goto boek90; zcd[px]=xx; xl=xl+2; if ((xx = fgetc(FPB)) == EOF) goto boek90; zce[px]=xx; if (zcd[px] < 128) goto boek10; // collect moves till end of varition // Search the filled stack for possible bookmove // ============================================= x=0; BOEK_BEGIN(); HK1=0x05035c45; HK2=0xf1b92b1f; // hashkey values start position boek14: if (bhk1==HK1 && bhk2==HK2) goto boek16; // hashkeys match, bookmove found XD=BVELD[zcd[x] & 0x3f]; XE=BVELD[zce[x] & 0x3f]; BOEKIN(); // update board and hashkeys x++; if (x > px) goto boek20; // end of variantion, check for next goto boek14; // Bookmove found, insert in table // =============================== boek16: if (zce[x]<64) goto boek20; // Bad book move, no insert if ((x&1) != (kleur&1)) goto boek20; // colours don't match, no insert xd=BVELD[zcd[x]&0x3f]; xe=BVELD[zce[x]&0x3f]; // Bookmove found for (v=0; v<AZ; v++) // skip possible doubles { if (db[v]==xd && eb[v]==xe && vb[v]<64) vb[v]=zce[x]; if (db[v]==xd && eb[v]==xe) goto boek20; } db[AZ]=xd; eb[AZ]=xe; vb[AZ]=zce[x]; // store move in internal format FROM1[AZ]=TA[xd]; FROM2[AZ]=TN[xd]; // store move in ascii format TO1[AZ]=TA[xe]; TO2[AZ]=TN[xe]; BOEKSTAT[AZ]=boekfase; AZ++; // number of found book moves // Spool back till bit-6 becomes zero // ================================== boek20: if (zcd[px] < 192) goto boek12; // there is another one ! boek22: px--; if (px < 0) goto boek10; if (zcd[px] >= 64) goto boek22; goto boek12; // End of File, decide move to play // ================================ boek90: if (boekfase==0 && AZ==0) { boekfase=1; goto again; } // nothing found in tournament book // search now the main book if (AZ==0) goto einde; // no book move(s) found boek92: rnd=rnd & 63; by=-1; boek94: by++; if (by >= AZ) goto boek92; rnd=rnd-BOEKRND[by]; if (rnd > 0) goto boek94; x=db[by]; FROM[0]=TA[x]; FROM[1]=TN[x]; FROM[2]=0; x=eb[by]; TO[0]=TA[x]; TO[1]=TN[x]; TO[2]=0; einde: fclose(FPB); }