move read_openbook() { int i, value; long move_str; move mv; hash_node hash_temp; move *pbook_move; int temp_value[MAX_BOOK_MOVE]; #if 0 if ((int)move_str) { /* do nothing */ } #endif hash_temp = hash_table[zobrist_key & hash_mask]; if ((hash_temp.type & BOOK_EXIST) != 0 && hash_temp.checksum == zobrist_key_check) { if ((hash_temp.type & BOOK_UNIQUE) != 0) { move_str = move_to_str(hash_temp.goodmove); return hash_temp.goodmove; } else { value = 0; pbook_move = book_move_array[hash_temp.value]; for (i = 0; i < hash_temp.depth; i++) { mv = pbook_move[i]; move_str = move_to_str(mv); value += mv.capture; temp_value[i] = value; } if (value > 0) { value = rand32() % value; for (i = 0; i < hash_temp.depth; i++) { if (value < temp_value[i]) { break; } } return pbook_move[i]; } else { mv = NULL_MOVE; return mv; } } } else { mv = NULL_MOVE; return mv; } }
static void print_move_info(move_t mv, int ply) { char buf[MAX_CHARS_IN_MOVE]; move_to_str(mv, buf, MAX_CHARS_IN_MOVE); printf("info"); for (int i = 0; i < ply; ++i) { printf(" ----"); } printf(" %s\n", buf); }
/* Choose the best move (by searching or using the book), and make it. */ static void cpu_move(Chess *chess) { bool book_used = false; char san_move[MAX_BUF]; char str_move[MAX_BUF]; U32 move = NULLMOVE; int score = 0; S64 timer; Board *board; ASSERT(1, chess != NULL); board = &chess->board; timer = get_ms(); chess->sd.cmd_type = CMDT_CONTINUE; if (settings.book_type != BOOK_OFF) move = get_book_move(board, chess->show_pv, chess->book); if (move != NULLMOVE) { book_used = true; chess->in_book = true; } else { score = id_search(chess, NULLMOVE); if (chess->sd.cmd_type == CMDT_CANCEL) { chess->cpu_color = COLOR_NONE; return; } move = chess->sd.move; chess->in_book = false; } timer = get_ms() - timer; ASSERT(1, move != NULLMOVE); move_to_str(move, str_move); if (SIGN(board->color)*score < VAL_RESIGN) { if (board->color == WHITE) printf("0-1 {White resigns}\n"); else printf("1-0 {Black resigns}\n"); chess->game_over = true; return; } printf("move %s\n", str_move); if (chess->debug && chess->sd.nnodes > 0) { print_search_data(&chess->sd, (int)timer); printf("Score: %d\n", score); } move_to_san(san_move, board, move); update_game_log(board, san_move, score, book_used); update_game(chess, move); }
static void getPV(move_t *pv, char *buf, size_t bufsize) { buf[0] = 0; for (int i = 0; i < (MAX_PLY_IN_SEARCH - 1) && pv[i] != 0; i++) { char a[MAX_CHARS_IN_MOVE]; move_to_str(pv[i], a, MAX_CHARS_IN_MOVE); if (i != 0) { strncat(buf, " ", bufsize - strlen(buf) - 1); // - 1, for the terminating '\0' } strncat(buf, a, bufsize - strlen(buf) - 1); // - 1, for the terminating '\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); } } }