/* * Main recursive search function. */ static SearchNode * game_position_solve_impl (ExactSolution *const result, const GamePosition *const gp, const int achievable, const int cutoff, PVCell ***pve_parent_line_p) { result->node_count++; SearchNode *node = NULL; SearchNode *node2 = NULL; PVCell **pve_line = NULL; if (log_env->log_is_on) { call_count++; gp_hash_stack_fill_point++; LogDataH log_data; log_data.sub_run_id = 0; log_data.call_id = call_count; log_data.hash = game_position_hash(gp); gp_hash_stack[gp_hash_stack_fill_point] = log_data.hash; log_data.parent_hash = gp_hash_stack[gp_hash_stack_fill_point - 1]; log_data.blacks = (gp->board)->blacks; log_data.whites = (gp->board)->whites; log_data.player = gp->player; gchar *json_doc = game_tree_log_data_h_json_doc(gp_hash_stack_fill_point, gp); log_data.json_doc = json_doc; log_data.json_doc_len = strlen(json_doc); game_tree_log_write_h(log_env, &log_data); g_free(json_doc); } const SquareSet moves = game_position_legal_moves(gp); if (0ULL == moves) { pve_line = pve_line_create(pve); GamePosition *flipped_players = game_position_pass(gp); if (game_position_has_any_legal_move(flipped_players)) { node = search_node_negated(game_position_solve_impl(result, flipped_players, -cutoff, -achievable, &pve_line)); } else { result->leaf_count++; node = search_node_new(pass_move, game_position_final_value(gp)); } pve_line_add_move(pve, pve_line, pass_move, flipped_players); pve_line_delete(pve, *pve_parent_line_p); *pve_parent_line_p = pve_line; game_position_free(flipped_players); } else { MoveList move_list; bool branch_is_active = false; move_list_init(&move_list); sort_moves_by_mobility_count(&move_list, gp); for (MoveListElement *element = move_list.head.succ; element != &move_list.tail; element = element->succ) { const Square move = element->sq; if (!node) node = search_node_new(move, (pv_full_recording) ? achievable - 1 : achievable); GamePosition *gp2 = game_position_make_move(gp, move); pve_line = pve_line_create(pve); node2 = search_node_negated(game_position_solve_impl(result, gp2, -cutoff, -node->value, &pve_line)); if (node2->value > node->value || (!branch_is_active && node2->value == node->value)) { branch_is_active = true; search_node_free(node); node = node2; node->move = move; node2 = NULL; pve_line_add_move(pve, pve_line, move, gp2); game_position_free(gp2); pve_line_delete(pve, *pve_parent_line_p); *pve_parent_line_p = pve_line; if (node->value > cutoff) goto out; if (!pv_full_recording && node->value == cutoff) goto out; } else { if (pv_full_recording && node2->value == node->value) { pve_line_add_move(pve, pve_line, move, gp2); pve_line_add_variant(pve, *pve_parent_line_p, pve_line); } else { pve_line_delete(pve, pve_line); } search_node_free(node2); game_position_free(gp2); } } } out: if (log_env->log_is_on) { gp_hash_stack_fill_point--; } return node; }
int main(int argc, char **argv) { cl_options_t cl_options = {0}; dbg_init(); DBG_LOG("Version %s", g_version); ui = &ui_sdlgl; printf("DreamChess %s\n", g_version); parse_options(argc, argv, &ui, &cl_options); config_init(); set_cl_options(&cl_options); if (!ui) { DBG_ERROR("Failed to find a user interface driver"); exit(1); } ui->init(); init_resolution(); while (1) { board_t board; int pgn_slot; option_t *option; if (!(config = ui->config(&pgn_slot))) break; ch_userdir(); option = config_get_option("first_engine"); #ifdef __APPLE__ char temp1[200]; char temp2[200]; if (!strcmp(option->string, "dreamer") || !strcmp(option->string, "Dreamer")) { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef bundledir = CFBundleCopyBundleURL(mainBundle); CFStringRef stringref = CFURLCopyFileSystemPath(bundledir, kCFURLPOSIXPathStyle); CFStringGetCString(stringref, temp1, 200, kCFStringEncodingMacRoman); snprintf(temp2, sizeof(temp2), "%s/contents/MacOS/dreamer", temp1); game_set_engine_error(comm_init(temp2)); } else game_set_engine_error(comm_init(option->string)); #else game_set_engine_error(comm_init(option->string)); #endif comm_send("xboard\n"); comm_send("new\n"); comm_send("random\n"); comm_send("sd %i\n", config->cpu_level); comm_send("depth %i\n", config->cpu_level); if (config->difficulty == 0) comm_send("noquiesce\n"); if (config->player[WHITE] == PLAYER_UI && config->player[BLACK] == PLAYER_UI) comm_send("force\n"); if (config->player[WHITE] == PLAYER_ENGINE) comm_send("go\n"); in_game = 1; board_setup(&board); history = history_init(&board); move_list_init(&san_list); move_list_init(&fan_list); move_list_init(&fullalg_list); if (pgn_slot >= 0) if (game_load(pgn_slot)) { DBG_ERROR("Failed to load savegame in slot %i", pgn_slot); exit(1); } ui->update(history->view->board, NULL); while (in_game) { char *s; if ((s = comm_poll())) { DBG_LOG("Message from engine: '%s'", s); if (!history->result) { if ((!strncmp(s, "move ", 4) || strstr(s, "... ")) && config->player[history->last->board->turn] == PLAYER_ENGINE) { char *move_str = strrchr(s, ' ') + 1; board_t new_board = *history->last->board; move_t *engine_move; DBG_LOG("Parsing move string '%s'", move_str); engine_move = san_to_move(&new_board, move_str); if (!engine_move) engine_move = fullalg_to_move(&new_board, move_str); if (engine_move) { audio_play_sound(AUDIO_MOVE); do_move(engine_move, 1); free(engine_move); } else DBG_ERROR("Failed to parse move string '%s'", move_str); } else if (strstr(s, "llegal move")) game_undo(); /* Ignore result message if we've already determined a result ourselves. */ else { char *start = strchr(s, '{'); char *end = strchr(s, '}'); if (start && end && end > start) { char *comment = malloc(end - start); history->result = malloc(sizeof(result_t)); strncpy(comment, start + 1, end - start - 1); comment[end - start - 1] = '\0'; history->result->reason = comment; if (strstr(s, "1-0")) { history->result->code = RESULT_WHITE_WINS; ui->show_result(history->result); } else if (strstr(s, "1/2-1/2")) { history->result->code = RESULT_DRAW; ui->show_result(history->result); } else if (strstr(s, "0-1")) { history->result->code = RESULT_BLACK_WINS; ui->show_result(history->result); } else { free(history->result->reason); free(history->result); history->result = NULL; } } } } free(s); } ui->poll(); } comm_send("quit\n"); comm_exit(); history_exit(history); move_list_exit(&san_list); move_list_exit(&fan_list); move_list_exit(&fullalg_list); } ui->exit(); dbg_exit(); return 0; }