blob: 87d556f66e3546dcdc888ebc3283f40eaec3fa01 (
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
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
|
//
// Created by thecharlesblake on 10/28/17.
//
#include <string>
#include <vector>
#include <algorithm>
#include "rapidjson/error/en.h"
#include "sol_rules.h"
#include "../util/util.h"
#include "../input-output/sol_preset_types.h"
using namespace std;
using namespace boost;
using namespace rapidjson;
////////////
// Static //
////////////
typedef sol_rules::build_policy pol;
bool sol_rules::is_suit(pol bp) {
switch (bp) {
case pol::CLUBS :
case pol::DIAMONDS :
case pol::HEARTS :
case pol::SPADES : return true;
default : return false;
}
}
card::suit_t sol_rules::suit_val(pol bp) {
switch (bp) {
case pol::CLUBS : return card::suit_t::Clubs;
case pol::DIAMONDS : return card::suit_t::Diamonds;
case pol::HEARTS : return card::suit_t::Hearts;
case pol::SPADES : return card::suit_t::Spades;
default: assert(false); return card::suit_t::Spades;
}
}
const sol_rules sol_rules::from_file(const string rules_file) {
sol_rules sr = get_default();
Document d = util::get_file_json(rules_file);
modify_sol_rules(sr, d);
return sr;
}
const sol_rules sol_rules::from_preset(const string sol_type) {
sol_rules sr = get_default();
const string preset_json = sol_preset_types::get(sol_type);
Document d;
d.Parse(preset_json.c_str());
assert(!d.HasParseError());
modify_sol_rules(sr, d);
return sr;
}
sol_rules sol_rules::get_default() {
sol_rules sr;
const string default_json = sol_preset_types::get("default");
Document d;
d.Parse(default_json.c_str());
assert(!d.HasParseError());
modify_sol_rules(sr, d);
return sr;
}
void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) {
if (!d.IsObject()) {
util::json_parse_err("JSON doc must be object");
}
if (d.HasMember("tableau piles")) {
if (d["tableau piles"].IsObject()) {
if (d["tableau piles"].HasMember("count")) {
if (d["tableau piles"]["count"].IsInt()) {
sr.tableau_pile_count = static_cast<uint8_t>(d["tableau piles"]["count"].GetInt());
} else {
util::json_parse_err("[tableau piles][count] must be an integer");
}
}
if (d["tableau piles"].HasMember("build order")) {
if (d["tableau piles"]["build order"].IsString()) {
string bo_str = d["tableau piles"]["build order"].GetString();
if (bo_str == "no-build") {
sr.build_ord = build_order::NO_BUILD;
} else if (bo_str == "descending") {
sr.build_ord = build_order::DESCENDING;
} else {
util::json_parse_err("[tableau piles][build order] is invalid");
}
} else {
util::json_parse_err("[tableau piles][build order] must be an string");
}
}
if (d["tableau piles"].HasMember("build policy")) {
if (d["tableau piles"]["build policy"].IsString()) {
string bp_str = d["tableau piles"]["build policy"].GetString();
if (bp_str == "any-suit") {
sr.build_pol = build_policy::ANY_SUIT;
} else if (bp_str == "red-black") {
sr.build_pol = build_policy::RED_BLACK;
} else {
util::json_parse_err("[tableau piles][build policy] is invalid");
}
} else {
util::json_parse_err("[tableau piles][build policy] must be an string");
}
}
if (d["tableau piles"].HasMember("spaces policy")) {
if (d["tableau piles"]["spaces policy"].IsString()) {
string sp_str = d["tableau piles"]["spaces policy"].GetString();
if (sp_str == "any") {
sr.spaces_pol = spaces_policy::ANY;
} else if (sp_str == "no-build") {
sr.spaces_pol = spaces_policy::NO_BUILD;
} else {
util::json_parse_err("[tableau piles][spaces policy] is invalid");
}
} else {
util::json_parse_err("[tableau piles][spaces policy] must be an string");
}
}
} else {
util::json_parse_err("[tableau piles] must be an object");
}
}
if (d.HasMember("max rank")) {
if (d["max rank"].IsInt()) {
sr.max_rank = static_cast<card::rank_t>(d["max rank"].GetInt());
} else {
util::json_parse_err("[max rank] must be an integer");
}
}
if (d.HasMember("hole")) {
if (d["hole"].IsBool()) {
sr.hole = d["hole"].GetBool();
} else {
util::json_parse_err("[hole] must be a boolean");
}
}
if (d.HasMember("foundations")) {
if (d["foundations"].IsBool()) {
sr.foundations = d["foundations"].GetBool();
} else {
util::json_parse_err("[foundations] must be a boolean");
}
}
if (d.HasMember("cells")) {
if (d["cells"].IsInt()) {
sr.cells = static_cast<uint8_t>(d["cells"].GetInt());
} else {
util::json_parse_err("[cells] must be an integer");
}
}
if (d.HasMember("reserve size")) {
if (d["reserve size"].IsInt()) {
sr.reserve_size = static_cast<uint8_t>(d["reserve size"].GetInt());
} else {
util::json_parse_err("[reserve size] must be an integer");
}
}
if (d.HasMember("stock size")) {
if (d["stock size"].IsInt()) {
sr.stock_size = static_cast<uint8_t>(d["stock size"].GetInt());
} else {
util::json_parse_err("[stock size] must be an integer");
}
}
}
|