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
|
#include <boost/program_options.hpp>
#include <boost/optional.hpp>
#include <rapidjson/document.h>
#include "input-output/command_line_helper.h"
#include "input-output/deal_parser.h"
#include "solver/solver.h"
#include "input-output/log_helper.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&);
void solve_input_files(const vector<string>, const sol_rules&);
void solve_game(const game_state&, const sol_rules&);
int main(int argc, const char* argv[]) {
// Parses the command-line options
command_line_helper clh;
if (!clh.parse(argc, argv)) {
return EXIT_FAILURE;
}
// Generates the rules of the solitaire from the game type
const optional<sol_rules> rules = gen_rules(clh);
if (!rules) return EXIT_FAILURE;
// Retrieves the input files to be solved
const vector<string> input_files = clh.get_input_files();
// If there are no input files, solve a random deal based on the
// supplied seed
if (input_files.empty()) {
solve_random_game(clh.get_random_deal(), *rules);
}
// Otherwise, solve the input files
else {
solve_input_files(input_files, *rules);
}
return EXIT_SUCCESS;
}
const optional<sol_rules> gen_rules(command_line_helper& clh) {
try {
if (!clh.get_solitaire_type().empty()) {
return sol_rules::from_preset(clh.get_solitaire_type());
} else {
return sol_rules::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) {
LOG_INFO ("Attempting to solve with seed: " << seed << "...");
game_state gs(rules, seed);
solve_game(gs, rules);
}
void solve_input_files(const vector<string> input_files, const sol_rules& rules) {
deal_parser dp(rules);
for (const string& input_json : input_files) {
try {
// Attempts to create a game state object from the json
game_state gs = dp.parse(input_json);
LOG_INFO ("Attempting to solve " << input_json << "...");
solve_game(gs, rules);
} catch (const runtime_error& error) {
string errmsg = "Error parsing deal file: ";
errmsg += error.what();
LOG_ERROR(errmsg);
}
}
}
void solve_game(const game_state& gs, const sol_rules& rules) {
solver sol(gs, rules);
optional<solver::node> solution = sol.run();
if (solution) {
cout << "Solution:\n" << *solution << "States Searched: "
<< sol.get_states_searched() << "\n";
} else {
cout << "Deal:\n" << sol.get_root() << "\nNo Possible Solution\n";
}
}
|