File: serial_range_solver.c

package info (click to toggle)
freecell-solver 5.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,256 kB
  • sloc: ansic: 28,700; perl: 10,050; xml: 5,600; python: 1,339; sh: 533; cpp: 275; makefile: 20; javascript: 8
file content (147 lines) | stat: -rw-r--r-- 4,692 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * This file is part of Freecell Solver. It is subject to the license terms in
 * the COPYING.txt file found in the top-level directory of this distribution
 * and at http://fc-solve.shlomifish.org/docs/distro/COPYING.html . No part of
 * Freecell Solver, including this file, may be copied, modified, propagated,
 * or distributed except according to the terms contained in the COPYING file.
 *
 * Copyright (c) 2000 Shlomi Fish
 */
// serial_range_solver.c - the Freecell Solver range solver.
//
// Also see:
//  - fc_pro_range_solver.c - range solver that can count the moves in
//  the solution and display the FC-Pro ones.
//  - threaded_range_solver.c - solve different boards in several POSIX
//  threads.
#include "default_iter_handler.h"
#include "cl_callback_range.h"
#include "trace_mem.h"
#include "try_param.h"
#include "range_solvers.h"

static inline int range_solvers_main(int argc, char *argv[], int arg,
    long long start_board, long long end_board, const long long stop_at)
{
    long long total_num_iters = 0;
#ifndef FCS_WITHOUT_MAX_NUM_STATES
    bool was_total_iterations_limit_per_board_set = FALSE;
#endif
    fcs_int_limit_t total_iterations_limit_per_board = -1;
    fcs_binary_output binary_output = INIT_BINARY_OUTPUT;
    const char *solutions_directory = NULL;
    char *solution_fn = NULL;

    for (; arg < argc; arg++)
    {
        const char *param;
        if ((param = TRY_P("--binary-output-to")))
        {
            binary_output.filename = param;
        }
        else if ((param = TRY_P("--total-iterations-limit")))
        {
#ifndef FCS_WITHOUT_MAX_NUM_STATES
            was_total_iterations_limit_per_board_set = TRUE;
            total_iterations_limit_per_board = (fcs_int_limit_t)atol(param);
#endif
        }
        else if ((param = TRY_P("--solutions-directory")))
        {
            solutions_directory = param;
            solution_fn =
                SREALLOC(solution_fn, strlen(solutions_directory) + 1024);
        }
        else
        {
            break;
        }
    }

    fc_solve_print_started_at();
    fflush(stdout);

    fc_solve_display_information_context display_context =
        INITIAL_DISPLAY_CONTEXT;
    void *const instance = alloc_instance_and_parse(argc, argv, &arg,
        known_parameters, cmd_line_callback, &display_context, TRUE);

    bin_init(&binary_output, &start_board, &end_board,
        &total_iterations_limit_per_board);

#ifndef FCS_WITHOUT_MAX_NUM_STATES
    if (was_total_iterations_limit_per_board_set)
    {
        freecell_solver_user_limit_iterations_long(
            instance, total_iterations_limit_per_board);
    }
#endif

    for (long long board_num = start_board; board_num <= end_board; ++board_num)
    {
        fcs_state_string state_string;
        get_board(board_num, state_string);

        const int ret =
            freecell_solver_user_solve_board(instance, state_string);

        switch (ret)
        {
        case FCS_STATE_SUSPEND_PROCESS:
            fc_solve_print_intractable(board_num);
            print_int(&binary_output, -1);
            break;

        case FCS_STATE_FLARES_PLAN_ERROR:
            print_flares_plan_error(instance);
            goto out_of_loop;

        case FCS_STATE_IS_NOT_SOLVEABLE:
            fc_solve_print_unsolved(board_num);
            print_int(&binary_output, -2);
            break;

        case FCS_STATE_WAS_SOLVED:
#ifdef FCS_RANGE_SOLVERS_PRINT_SOLVED
            printf("Solved Board No. " FCS_LL_FMT "\n", board_num);
#endif
            print_int(&binary_output,
                (int)freecell_solver_user_get_num_times_long(instance));
            break;

        default:
            fc_solve_err("%s", "Unknown ret code!");
        }

        if (solutions_directory)
        {
            sprintf(solution_fn, "%s" FCS_LL9_FMT ".sol", solutions_directory,
                board_num);
            FILE *const output_fh = fopen(solution_fn, "wt");
            if (!output_fh)
            {
                fc_solve_err("Could not open output file '%s' for writing!",
                    solution_fn);
            }
            fc_solve_output_result_to_file(
                output_fh, instance, ret, &display_context);
            fclose(output_fh);
        }
        total_num_iters += freecell_solver_user_get_num_times_long(instance);

        if (board_num % stop_at == 0)
        {
            fc_solve_print_reached(board_num, total_num_iters);
        }
        freecell_solver_user_recycle(instance);
        trace_mem(board_num);
    }
out_of_loop:

    freecell_solver_user_free(instance);
    bin_close(&binary_output);
    free(solution_fn);
    solution_fn = NULL;

    return 0;
}