void init_openbook(const char *bookfile) { FILE *fp; char *linechar; char linestr[256]; move book_move; hash_node hash_temp, *phash; fp = fopen(bookfile, "rt"); if (fp == NULL) return; int a = 0; linestr[254] = linestr[255] = '\0'; while (fgets(linestr, 254, fp) != NULL) { a++; linechar = linestr; book_move = str_to_move(*(long *) linechar); if (!cmp_move(book_move, NULL_MOVE)) { linechar += 5; book_move.capture = 0; while (*linechar >= '0' && *linechar <= '9') { book_move.capture *= 10; book_move.capture += *linechar - '0'; linechar++; } linechar++; fen_to_arr(linechar); if (board[book_move.from]) { phash = &hash_table[zobrist_key & hash_mask]; hash_temp = *phash; if ((hash_temp.type & BOOK_EXIST) == 0) { hash_temp.checksum = zobrist_key_check; hash_temp.type = BOOK_UNIQUE; hash_temp.goodmove = book_move; hash_temp.value = book_move.capture; *phash = hash_temp; } else { if (hash_temp.checksum == zobrist_key_check) { if ((hash_temp.type & BOOK_UNIQUE) != 0) { if (book_num < MAX_BOOK_POS) { hash_temp.checksum = zobrist_key_check; hash_temp.type = BOOK_MULTI; book_move_array[book_num][0] = hash_temp.goodmove; book_move_array[book_num][1] = book_move; hash_temp.value = book_num; hash_temp.depth = 2; book_num++; *phash = hash_temp; } } else { if (hash_temp.depth < MAX_BOOK_MOVE) { book_move_array[hash_temp.value][hash_temp.depth] = book_move; hash_temp.depth++; } } } } } } } fclose(fp); }
void console() { char str[100]; Move movelist[256]; int n; int depth_limit = 8; setup_position(start_fen); printf("white or black?\n"); while(1) { gets(str); if(!strcmp(str, "white") || ! strcmp(str, "black")) { break; } } int is_reversed; if(!strcmp(str, "black")) { is_reversed = 1; print_position(is_reversed); Move bestmove = search(depth_limit); make_move(bestmove); } else { is_reversed = 0; } while(1) { print_position(is_reversed); n = generate_moves(movelist); if(n == 0) { while(1); } int flag = 1; while(flag) { gets(str); Move move = str_to_move(str); for(int i = 0; i < n; i += 1) { Move i_move = movelist[i]; if(move == i_move) { make_move(move); flag = 0; break; } } if(flag) { printf("Incorrect\n"); } } print_position(is_reversed); Move bestmove = search(depth_limit); if(bestmove == 0) { while(1); } make_move(bestmove); } }
int main() { char input[16]; int done = 0, state; if (!engine_init()) return 1; init_states(); while (!done) { state = read_keyword(input, sizeof(input)); if (state == 0) break; if (state == STATE_ERROR) continue; //fprintf(stderr, "cmd %s state %d\n", input, state); switch (state) { case STATE_BLACK: engine_set_color(BLACK); break; case STATE_WHITE: engine_set_color(WHITE); break; case STATE_CGCBOARD: write_string("\n"); break; case STATE_DRAW: engine_offer_draw(); break; case STATE_FORCE: engine_set_go(0); break; case STATE_GO: engine_set_go(1); break; case STATE_NEW: engine_new(); break; case STATE_RANDOM: engine_set_random(!engine_get_random()); break; case STATE_RESULT: if (!handle_result()) done = 1; break; case STATE_REMOVE: engine_undo(); engine_undo(); break; case STATE_QUIT: write_string("bye\n"); done = 1; break; case STATE_SD: if (!handle_sd()) done = 1; break; case STATE_UNDO: engine_undo(); break; case STATE_MOVE: engine_move(str_to_move(input)); break; case STATE_PLAY: engine_go(); break; } } engine_destroy(); return 0; }
void UCI() { char str[8192]; char *p; printf("id name chess\n"); printf("id author komendart\n"); printf("uciok\n"); fflush(stdout); while(1) { gets(str); p = strtok(str, " "); if(!p) { continue; } else if(!strcmp(p, "quit")) { return; } else if(!strcmp(p, "isready")) { printf("readyok\n"); fflush(stdout); } else if(!strcmp(p, "position")) { p = strtok(0, " "); if(!strcmp(p, "fen")) { p = strtok(0, "m"); setup_position(p); } else { setup_position(start_fen); } if (p = strtok(0, " ")) { while(p = strtok(0, " ")) { Move m = str_to_move(p); make_move(m); } } } else if(!strcmp(p, "go")) { int depth_limit = 8; while (p = strtok(0, " ")) { if(!strcmp(p, "depth")) { p = strtok(0, " "); depth_limit = atoi(p); } } Move move = search(depth_limit); char tmp[6]; move_to_str(move, tmp); sprintf(str, "bestmove %s\n", tmp); printf(str); fflush(stdout); } } }