aboutsummaryrefslogtreecommitdiff
path: root/src/main/main.cpp
blob: 16a5ba12a0e8f8b06ec62430daa72c5d4c2c8e65 (plain) (blame)
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
#include <boost/program_options.hpp>
#include <boost/optional.hpp>
#include <rapidjson/document.h>
#include <gperftools/profiler.h>

#include "input-output/input/command_line_helper.h"
#include "input-output/input/sol_preset_types.h"
#include "input-output/input/json-parsing/json_helper.h"
#include "input-output/input/json-parsing/rules_parser.h"
#include "input-output/output/log_helper.h"
#include "solver/solver.h"
#include "solvability/solvability_calc.h"

using namespace rapidjson;

using namespace std;
using namespace boost;

namespace po = boost::program_options;

const optional<sol_rules> gen_rules(command_line_helper&);
void solve_random_game(int, const sol_rules&, bool, bool);
void solve_input_files(vector<string>, const sol_rules&, bool, bool);
void solve_game(const game_state&, bool, bool);

int main(int argc, const char* argv[]) {

    // Parses the command-line options
    command_line_helper clh;
    if (!clh.parse(argc, argv)) {
        return EXIT_FAILURE;
    }

    // If the user has asked for the list of preset game types, prints it
    if (clh.get_available_game_types()) {
        sol_preset_types::print_available_games();
        return EXIT_SUCCESS;
    }
    string game_rule_str = clh.get_describe_game_rules();
    if (!game_rule_str.empty()) {
        sol_preset_types::describe_game_rules(game_rule_str);
        return EXIT_SUCCESS;
    }

    // Generates the rules of the solitaire from the game type
    const optional<sol_rules> rules = gen_rules(clh);
    if (!rules) return EXIT_FAILURE;

    // If the user has asked for a solvability percentage, calculates it
    if (clh.get_solvability()) {
        solvability_calc solv_c(*rules);
        solv_c.calculate_solvability_percentage();
    }
    // If a random deal seed has been supplied, solves it
    else if (clh.get_random_deal() != -1) {
        solve_random_game(clh.get_random_deal(), *rules, clh.get_short_sols(),
                          clh.get_classify());
    }
    // Otherwise there are supplied input files which should be solved
    else {
        const vector<string> input_files = clh.get_input_files();

        // If there are no input files, solve a random deal based on the
        // supplied seed
        assert (!input_files.empty());
        solve_input_files(input_files, *rules, clh.get_short_sols(),
                          clh.get_classify());
    }

    return EXIT_SUCCESS;
}

const optional<sol_rules> gen_rules(command_line_helper& clh) {
    try {
        if (!clh.get_solitaire_type().empty()) {
            return rules_parser::from_preset(clh.get_solitaire_type());
        } else {
            return rules_parser::from_file(clh.get_rules_file());
        }
    } catch (const runtime_error& error) {
        string errmsg = "Error in rules generation: ";
        errmsg += error.what();
        LOG_ERROR(errmsg);
        return none;
    }
}

void solve_random_game(int seed, const sol_rules& rules, bool short_sol,
                       bool classify) {
    LOG_INFO ("Attempting to solve with seed: " << seed << "...");
    game_state gs(rules, seed);
    solve_game(gs, short_sol, classify);
}

void solve_input_files(const vector<string> input_files, const sol_rules& rules,
                       bool short_sol, bool classify) {
    for (const string& input_file : input_files) {
        try {
            // Reads in the input file to a json doc
            const Document in_doc = json_helper::get_file_json(input_file);

            // Attempts to create a game state object from the json
            game_state gs(rules, in_doc);

            LOG_INFO ("Attempting to solve " << input_file << "...");
            solve_game(gs, short_sol, classify);

        } catch (const runtime_error& error) {
            string errmsg = "Error parsing deal file: ";
            errmsg += error.what();
            LOG_ERROR(errmsg);
        }
    }
}

void solve_game(const game_state& gs, bool short_sol, bool classify) {
    solver s(gs);
    solver* solv = &s;
    bool solution;
    if (short_sol) {
        uint bound = 1;
        solver::sol_state ss;
        do {
            LOG_INFO("Depth: " << bound);
            solver s_(gs);
            solv = &s_;
            ss = s_.run_with_cutoff(none, bound++);
        } while (ss == solver::sol_state::cutoff);
        solution = ss == solver::sol_state::solved;
    } else {
        solution = solv->run() == solver::sol_state::solved;
    }
    ProfilerStop();

    if (solution) {
        if (!classify) solv->print_solution();
        cout << "Solved\n";
    } else {
        if (!classify) cout << "Deal:\n" << gs << "\n";
        cout << "No Possible Solution\n";
    }

    cout << "States Searched: " << solv->get_states_searched() << "\n";
    cout << "Final Depth: " << solv->get_final_depth() << "\n";
}