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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
//
// Created by thecharlesblake on 10/16/17.
//
#include <tuple>
#include <vector>
#include <boost/program_options.hpp>
#include "command_line_helper.h"
#include "../../game/sol_rules.h"
#include "../output/log_helper.h"
#include "sol_preset_types.h"
using namespace std;
namespace po = boost::program_options;
command_line_helper::command_line_helper()
: main_options("options") {
main_options.add_options()
("help", "produce help message")
("type", po::value<string>(),
"specify the type of the solitaire game to be solved from the list "
"of preset games. Must supply either this 'type' option, "
"or the 'custom-rules' option")
("available-game-types", "outputs a list of the different preset "
"game types that can be solved")
("describe-game-rules", po::value<string>(),
"outputs the JSON that describes the rules of the supplied "
"preset game type")
("custom-rules", po::value<string>(),
"the path to a JSON file describing the rules of the solitaire "
"to be solved. Must supply either 'type' or 'custom-rules' option")
("random", po::value<int>(), "create and solve a random solitaire "
"deal based on a seed. Must supply either 'random',"
"'solvability' or list of deals to be solved.")
("shortest-sols", "for each instance returns the shortest possible "
"solution. cannot be supplied alongside '--solvability'")
("solvability", "calculates the solvability "
"percentage of the supplied solitaire game. Must supply "
"either 'random', 'solvability' or list of deals to be "
"solved.")
("classify", "outputs a simple 'solvable/not solvable' "
"classification");
po::options_description hidden_options("Hidden options");
hidden_options.add_options()
("input-files", po::value<vector<string>>(),
"the file containing the deal to be solved");
cmdline_options.add(main_options).add(hidden_options);
p.add("input-files", -1);
}
// Returns true if we can continue solving the supplied solitaire(s)
bool command_line_helper::parse(int argc, const char* argv[]) {
po::variables_map vm;
// Attempts to parse cmdln options. Catches basic errors
try {
store(po::command_line_parser(argc, argv).options(cmdline_options)
.positional(p).run(), vm);
} catch(po::error& e) {
LOG_ERROR ("Error: " << e.what());
return false;
}
// Updates the variables map which the user's options are stored in
notify(vm);
// Converts the variables map into flags and values
help = (vm.count("help") != 0);
available_game_types = (vm.count("available-game-types") != 0);
if (vm.count("describe-game-rules")) {
describe_game_rules = vm["describe-game-rules"].as<string>();
}
classify = (vm.count("classify") != 0);
if (vm.count("input-files")) {
input_files = vm["input-files"].as<vector<string>>();
}
if (vm.count("type")) {
solitaire_type = vm["type"].as<string>();
}
if (vm.count("rules")) {
rules_file = vm["rules"].as<string>();
}
if (vm.count("random")) {
random_deal = vm["random"].as<int>();
} else {
random_deal = -1;
}
solvability = (vm.count("solvability") != 0);
shortest_sols = (vm.count("shortest-sols") != 0);
// Handle logic error scenarios
return assess_errors();
}
bool command_line_helper::assess_errors() {
if (help) {
print_help();
return false;
}
if (available_game_types || !describe_game_rules.empty()) return true;
// The user must either supply input files, a random seed, or ask for the
// solvability percentage
int opt_count = (random_deal != -1) + !input_files.empty() + solvability;
if (opt_count > 1) {
print_too_many_opts_error();
return false;
} else if (opt_count < 1) {
print_no_opts_error();
return false;
}
// The user must supply either a solitaire type or a rules file
if ((solitaire_type.empty() && rules_file.empty())
|| (!solitaire_type.empty() && !rules_file.empty())) {
print_sol_type_rules_error();
return false;
}
if (shortest_sols && solvability) {
print_shortest_sols_error();
return false;
}
if (!solitaire_type.empty()) {
return assess_sol_type();
} else {
return true;
}
}
// Checks if the supplied solitaire type is in the list of valid solitaires
bool command_line_helper::assess_sol_type() {
if (sol_preset_types::is_valid_preset(solitaire_type)) {
return true;
} else {
LOG_ERROR ("Error: Solitaire type is not valid: " << solitaire_type);
print_help();
return false;
}
}
void command_line_helper::print_help() {
cerr << "Usage: solvitaire [options] input-file1 input-file2 ...\n"
<< main_options << "\n";
}
void command_line_helper::print_no_opts_error() {
LOG_ERROR ("Error: User must supply input file(s), the '--random' "
"option, or the '--solvability' option");
print_help();
}
void command_line_helper::print_sol_type_rules_error() {
LOG_ERROR ("Error: User must supply either a solitaire type, or a 'rules' "
"file");
print_help();
}
void command_line_helper::print_too_many_opts_error() {
LOG_ERROR ("Error: User must supply input file(s), the '--random' option, "
"or the '--solvability' option, not multiple");
print_help();
}
void command_line_helper::print_shortest_sols_error() {
LOG_ERROR ("Error: User cannot supply '--shortest-sols' and '--solvability'");
print_help();
}
const vector<string> command_line_helper::get_input_files() {
return input_files;
}
const string command_line_helper::get_solitaire_type() {
return solitaire_type;
}
const string command_line_helper::get_rules_file() {
return rules_file;
}
int command_line_helper::get_random_deal() {
return random_deal;
}
bool command_line_helper::get_help() {
return help;
}
bool command_line_helper::get_classify() {
return classify;
}
bool command_line_helper::get_solvability() {
return solvability;
}
bool command_line_helper::get_available_game_types() {
return available_game_types;
}
string command_line_helper::get_describe_game_rules() {
return describe_game_rules;
}
bool command_line_helper::get_short_sols() {
return shortest_sols;
}
|