Example #1
0
static void
game_position_has_any_legal_move_test (GamePositionDbFixture *fixture,
                                       gconstpointer          test_data)
{
  GamePositionDb *db = fixture->db;

  g_assert(TRUE  == game_position_has_any_legal_move(get_gp_from_db(db, "initial")));
  g_assert(TRUE  == game_position_has_any_legal_move(get_gp_from_db(db, "early-game-b-9-moves")));
  g_assert(FALSE == game_position_has_any_legal_move(get_gp_from_db(db, "black-has-to-pass")));
  g_assert(TRUE  == game_position_has_any_legal_move(get_gp_from_db(db, "early-game-c-12-moves")));
  g_assert(FALSE == game_position_has_any_legal_move(get_gp_from_db(db, "final-b37-w27")));
}
Example #2
0
static void
game_position_solve_test (GamePositionDbFixture *fixture,
                          gconstpointer          test_data)
{
  GamePositionDb *db = fixture->db;

  /*
   * FFO position #05:
   *
   * a b c d e f g h 
   * 1  . O O O O O . . 
   * 2  . . O @ @ O . @ 
   * 3  @ @ O @ O @ @ . 
   * 4  @ @ O @ O @ @ O 
   * 5  @ @ O O @ O O O 
   * 6  @ @ @ @ O O . O 
   * 7  @ . @ O O O . . 
   * 8  . @ @ @ @ @ . . 
   * Player to move: BLACK
   *
   * Move values: G8:+32. G2:+12. B2:-20. G6:-26. G1:-32. G7:-34.
   *
   * Principal Variation, PV: g8 g7 h8 g2 b2 a2 a1 g6 h7 b7 a8 -- h3
   * Final score is +32
   */
  GamePosition *ffo_05 = get_gp_from_db(db, "ffo-05");
  g_assert(TRUE  == game_position_has_any_legal_move(ffo_05));

  ExactSolution *solution = game_position_solve(ffo_05);
  g_assert(+32 == solution->outcome);
  g_assert(G8 == solution->principal_variation[0]);
}
Example #3
0
/*
 * 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;
}