int addmove(char *move) { static tmove m[256]; tmove * mf; static int n; int i; int p=PAWN+Color; for(i=2;i!=7;i++) if(piece[i]==move[0]) p=16*i+Color; if( move[0]=='O' ) { p=KING+Color; if(move[3]=='-') { if(Color==WHITE){} } } generate_legal_moves( m, &n, checktest(Color) ); if( (mf=sandex(move,m,n)) != NULL ) { if( Color==WHITE ) { if(wpoints) { A[I].hashboard=G[Counter].hashboard; A[I].move = smove(mf); A[I].n = wpoints; positions++; I++; if( I>=Asize ) compress(); } } else { if(bpoints) { A[I].hashboard=G[Counter].hashboard; A[I].move = smove(mf); A[I].n = bpoints; positions++; I++; if( I>=Asize ) compress(); } } do_move(mf); return 1; } else { return 0; } }
Move Book::get_move(const Position &pos) const { if(this->is_open()) { int bestMove = 0, bestScore = 0, move, score; uint64_t key = book_key(pos); BookEntry entry; for(int i = this->find_key(key); i < bookSize; i++) { this->read_entry(entry, i); if(entry.key != key) break; move = entry.move; score = entry.count; assert(score > 0); bestScore += score; if(int(genrand_int32() % bestScore) < score) bestMove = move; } if(bestMove != 0) { MoveStack moves[256]; int n, j; n = generate_legal_moves(pos, moves); for(j = 0; j < n; j++) if((int(moves[j].move) & 07777) == bestMove) return moves[j].move; } } return MOVE_NONE; }
BOOL is_stalemate(struct t_board *board) { struct t_move_list moves[1]; generate_legal_moves(board, moves); if (moves->count > 0) return FALSE; return TRUE; }
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); } }
BOOL test_genmove() { BOOL ok = TRUE; struct t_move_list moves[1]; set_fen(position, "r5r1/n1q1pP1k/3pPppp/P1pP4/2P4N/R1B5/2Q3PP/7K w - -"); assert(integrity(position)); assert(is_square_attacked(position, E4, WHITE)); assert(!is_square_attacked(position, A7, WHITE)); assert(!is_square_attacked(position, F4, BLACK)); assert(is_square_attacked(position, D8, BLACK)); generate_moves(position, moves); ok = ok && (moves->count == 42); flip_board(position); generate_moves(position, moves); //write_move_list(moves, "movelist.txt"); ok = ok && (moves->count == 42); set_fen(position, "1r2k2r/p1ppqpb1/b3pnp1/3PN3/1pn1P3/2N2Q1p/PPPBBPPP/R4K1R w - -"); assert(integrity(position)); generate_moves(position, moves); assert(move_list_integrity(position, moves)); ok = ok && (moves->count == 44); set_fen(position, "4q3/3P1P2/b4N2/8/3Q2Bb/2p3B1/1k4N1/4K1Nr w - -"); assert(integrity(position)); generate_evade_check(position, moves); ok = ok && (moves->count == 18); flip_board(position); generate_evade_check(position, moves); ok = ok && (moves->count == 18); set_fen(position, "1r2k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R4K1R w --k- -"); assert(integrity(position)); generate_moves(position, moves); //write_move_list(moves, "movelist.txt"); assert(move_list_integrity(position, moves)); ok = ok && (moves->count == 46); flip_board(position); generate_moves(position, moves); ok = ok && (moves->count == 46); // Chess960 Examples set_fen(position, "Rr4kr/8/8/8/8/8/PPPP4/R1K5 w Ahb -"); assert(integrity(position)); generate_moves(position, moves); assert(move_list_integrity(position, moves)); ok = ok && (moves->count == 18); flip_board(position); generate_moves(position, moves); ok = ok && (moves->count == 18); set_fen(position, "1r1kbb1r/1pp2ppp/3npn2/3pN3/1Q3P2/4PN2/2PP2PP/qR1KBB1R w HBhb -"); assert(integrity(position)); generate_legal_moves(position, moves); assert(move_list_integrity(position, moves)); ok = ok && (moves->count == 48); set_fen(position, "rkrbqnb1/pp2p2p/3p1pp1/2p1nP2/2P1P3/3P2N1/PP4PP/RKRBQNB1 w CAca -"); assert(integrity(position)); generate_legal_moves(position, moves); write_move_list(moves, "movelist.txt"); assert(move_list_integrity(position, moves)); ok = ok && (moves->count == 34); flip_board(position); generate_legal_moves(position, moves); ok = ok && (moves->count == 34); return ok; }