/* * bookmoves() returns number of moves found in book, or 0, * if no move is found. moves[0],...,moves[index-1] are indexes * to the bookmoves in m[], n is numer of moves. Moves must * be generated before. */ int bookmoves( int *moves, int *values, tmove *m, int n ) { char s[256], p[256]; int index, i; long booksize; /* filesize in bytes */ FILE *f; if( (f=Pbook.f) == NULL ) return -1; booksize = Pbook.filesize; postr(p); /* Current position to string */ if( binSB(f,0,booksize,p,s) == -1 ) { return -1; } /*** Position found in book! ***/ index=0; for(i=0; s[i]!='\n'; i++) { int pm; while(s[i]!=' ' && s[i]!='\n') { if(s[i]=='!' && index) values[index-1]+=100; i++; } if(s[i]=='\n') break; i++; if(s[i]=='\n') break; if( (pm=parsemove(&s[i],m,n)) != -1 ) { moves[index]=pm; values[index]=100; index++; } } return index; }
int MainWindow::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { _id = QMainWindow::qt_metacall(_c, _id, _a); if (_id < 0) return _id; if (_c == QMetaObject::InvokeMetaMethod) { switch (_id) { case 0: postr(); break; case 1: vod(); break; case 2: exit(); break; case 3: inform(); break; default: ; } _id -= 4; } return _id; }
void bk( tmove *m, int n ) { int moves[80], values[80], idx; int pass; int sumvalues=0; #define SHOWECO #ifdef SHOWECO static int att=1; static int seco=0; if( att != 0 && Eco!=NULL ) { typedef struct teco { unsigned hashboard; unsigned point; } teco; static struct teco * peco = NULL; if( att==1 && Counter==0 && G[Counter].hashboard==0x39512910 /*init*/) { int c; tmove * move; peco = malloc(2048*sizeof(teco)); if( peco == NULL ) { puts(" telluser cannot alloc memory for ECO"); att=0; goto abort; } printf("telluser creating ECO index, please wait\n"); while( (c=fgetc(Eco))!='[' ) if( c==EOF ) goto doneinit; for(;;) { tmove m[128]; char ms[32]; int msi; int n; (peco+seco)->point=ftell(Eco)-1; while( (c=getc(Eco))!=']' ) if( c==EOF ) goto doneinit; if( c==EOF ) goto doneinit; setfen("rnbqkbnr/pppppppp/////PPPPPPPP/RNBQKBNR/w"); for(;;) { msi=0; while( (c=getc(Eco))==' ' || c=='\n' ) {} ungetc(c,Eco); while( (c=getc(Eco))!=' ' && c!='\n' && msi<=30 ) { ms[msi]=c; msi++; if( c==EOF || c=='[' ) { ungetc(c,Eco); goto nextgame; } } ms[msi]='\0'; /* printf("%s ",ms); */ generate_legal_moves( m, &n, checktest(Color) ); move=sandex(ms,m,n); if( move==NULL ) goto nextgame; do_move(move); if( c==EOF ) goto doneinit; } nextgame:; peco[seco].hashboard=G[Counter].hashboard; seco++; while( (c=fgetc(Eco))!='[' ) if( c==EOF ) goto doneinit; } doneinit:; printf(" parsed %i ECO records\n",seco); att=2; setfen("rnbqkbnr/pppppppp/////PPPPPPPP/RNBQKBNR/w"); } if( Counter>0 && att>1 ) { int counter=Counter; int text=-1; while( Counter > 0 ) { int i; for(i=0;i!=seco;i++) if(peco[i].hashboard==G[Counter].hashboard) { text=peco[i].point; goto foundeco; } undo_move( & G[Counter-1].m ); } foundeco:; while( counter > Counter ) do_move( & G[Counter].m ); if( text==-1 ) printf(" no eco found\n"); else { char t[128], * c; fseek(Eco,text,SEEK_SET); fgets(t,126,Eco); t[127]='\0'; c=strchr(t,'['); if(c!=NULL) *c = ' '; c=strrchr(t,']'); if(c!=NULL) *c = '\0'; puts(t); } } abort:; } #endif for( pass=0; pass!=2; pass++ ) { if( pass==0 ) { int i; idx = bookmoves(moves,values,m,n); if( idx != -1 ) for(i=0;i!=idx;i++) sumvalues += values[i]; printf(" primary book moves\n"); } else { idx = sbookmoves(moves,values,m,n); printf(" secondary book moves\n"); } if( idx > 0 ) { int i; for( i=0; i!=idx; i++ ) if( i==0 || moves[i-1]!=moves[i] ) { printf(" "); printm( m[moves[i]], NULL ); if(pass==0) printf("%3d%%\n",values[i]*100/sumvalues); else printf("\n"); } } else printf(" no move found\n"); } if( Flag.xboard>1 ) puts(""); { char c[128]; postr(c); printf("%s\n",c); } }
/* * This function returns index of a random move found in book * or -1, if no move was found. Moves must be generated. */ int bookmove( tmove *m, int n ) { int moves[80], values[80], index; int foundtxt=0; index = bookmoves(moves,values,m,n); if( index <= 0 ) index = sbookmoves(moves,values,m,n); else { #ifdef SHOWDUPS int moves2[80]; int values2[80], index2; index2 = sbookmoves(moves2,values,m,n); if( index2>=1 && index>=1 && Counter>8 ) { char p[256]; int i; postr(p); puts("found in both sbook and pbook"); printboard(NULL); puts(p); printf(" 0 0 0 0 book2 "); for( i=0; i!=index2; i++ ) if( i==0 || moves2[i-1]!=moves2[i] ) { printm( m[moves2[i]], NULL ); } puts(""); } #endif foundtxt=1; } if( index > 0 ) { int ii, sumvalues=0, rn; for( ii=0; ii!=index; ii++ ) { sumvalues += values[ii]; } rn = rand()%sumvalues; sumvalues = 0; for( ii=0; ii!=index; ii++ ) { sumvalues += values[ii]; if( sumvalues >= rn ) break; } if( Flag.post ) { int i; char s[128]; if( Flag.xboard ) sprintf(s," 0 0 0 0 book"); else sprintf(s,"Book moves "); if( foundtxt ) sprintf(s+strlen(s),"1 "); else sprintf(s+strlen(s),"2 "); for( i=0; i!=index; i++ ) { printm( m[moves[i]], s+strlen(s) ); } sprintf(s+strlen(s),"\n"); printf(s); if( Flag.log!=NULL && Flag.ponder<2 ) { char sm[64]; if(Flag.xboard) fprintf(Flag.log,s+26); else fprintf(Flag.log,s); fprintf(Flag.log," selected move "); printm( m[moves[ii]], sm ); fprintf(Flag.log,sm); fprintf(Flag.log,"\n"); } } return ( moves[ii] ); } return( -1 ); }