aboutsummaryrefslogtreecommitdiff
path: root/src/main/input-output/input/json-parsing
diff options
context:
space:
mode:
authorCharlie Blake2018-07-14 15:06:04 +0100
committerGitHub2018-07-14 15:06:04 +0100
commit7a2eaa8280ca35a52227084c037b1adb57193291 (patch)
tree64ab75568e80890a6b5b66ab0481dd4ea24d1d24 /src/main/input-output/input/json-parsing
parentMerge pull request #75 from thecharlesblake/refactor-rules-json (diff)
parentGaps / sequence implementation (diff)
downloadmetasol-7a2eaa8280ca35a52227084c037b1adb57193291.tar.gz
metasol-7a2eaa8280ca35a52227084c037b1adb57193291.zip

Merge pull request #76 from thecharlesblake/add-gaps

Add gaps

Diffstat (limited to 'src/main/input-output/input/json-parsing')
-rw-r--r--src/main/input-output/input/json-parsing/deal_parser.cpp41
-rw-r--r--src/main/input-output/input/json-parsing/deal_parser.h1
-rw-r--r--src/main/input-output/input/json-parsing/rules_parser.cpp80
3 files changed, 120 insertions, 2 deletions
diff --git a/src/main/input-output/input/json-parsing/deal_parser.cpp b/src/main/input-output/input/json-parsing/deal_parser.cpp
index 5a6dff6..998f337 100644
--- a/src/main/input-output/input/json-parsing/deal_parser.cpp
+++ b/src/main/input-output/input/json-parsing/deal_parser.cpp
@@ -51,6 +51,11 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) {
parse_reserve(gs, doc);
}
+ // Construct sequences
+ if (gs.rules.sequence_count > 0) {
+ parse_sequences(gs, doc);
+ }
+
// If the foundations begin with cards in them, fill them
if (gs.rules.foundations_present) {
bool supplied_foundations = parse_foundations(gs, doc);
@@ -78,7 +83,16 @@ string deal_parser::deal_schema_json() {
"definitions": {
"card": {"type": "string", "pattern": "^(([0-9]|1[0-3]|a|A|j|J|q|Q|k|K)(c|C|d|D|h|H|s|S))$"},
- "cardarray": {"type": "array", "items": {"$ref": "#/definitions/card"}}
+ "cardarray": {"type": "array", "items": {"$ref": "#/definitions/card"}},
+ "cardarraywithempty": {
+ "type": "array",
+ "items": {
+ "oneOf":[
+ {"$ref": "#/definitions/card"},
+ {"type": "string","enum": [""]}
+ ]
+ }
+ }
},
"type": "object", "properties": {
@@ -89,6 +103,9 @@ string deal_parser::deal_schema_json() {
"foundations": {
"type": "array", "items": {"$ref": "#/definitions/cardarray"}
},
+ "sequences": {
+ "type": "array", "items": {"$ref": "#/definitions/cardarraywithempty"}
+ },
"hole": {"$ref": "#/definitions/card"},
"stock": {"$ref": "#/definitions/cardarray"},
"waste": {"$ref": "#/definitions/cardarray"},
@@ -197,6 +214,28 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) {
}
}
+void deal_parser::parse_sequences(game_state& gs, const rapidjson::Document& doc) {
+ assert(doc.HasMember("sequences"));
+ const Value& json_seqs = doc["sequences"];
+ assert(json_seqs.IsArray());
+
+ if (json_seqs.Size() != gs.rules.sequence_count ) {
+ json_helper::json_parse_err("Incorrect number of sequences");
+ }
+
+ for (auto p = std::make_pair(begin(json_seqs.GetArray()), begin(gs.sequences));
+ p.second != end(gs.sequences);
+ ++p.first, ++p.second) {
+
+ for (auto& json_card : p.first->GetArray()) {
+ assert(json_card.IsString());
+ string card_str = json_card.GetString();
+ card c = card_str.empty() ? "AS" : card(json_card.GetString());
+ gs.place_card(*p.second, c);
+ }
+ }
+}
+
bool deal_parser::parse_foundations(game_state &gs, const rapidjson::Document& doc) {
if (!doc.HasMember("foundations")) return false;
diff --git a/src/main/input-output/input/json-parsing/deal_parser.h b/src/main/input-output/input/json-parsing/deal_parser.h
index c34a93f..bce4a29 100644
--- a/src/main/input-output/input/json-parsing/deal_parser.h
+++ b/src/main/input-output/input/json-parsing/deal_parser.h
@@ -23,6 +23,7 @@ public:
static void parse_stock(game_state&, const rapidjson::Document&);
static void parse_waste(game_state&, const rapidjson::Document&);
static void parse_reserve(game_state&, const rapidjson::Document&);
+ static void parse_sequences(game_state&, const rapidjson::Document&);
static bool parse_foundations(game_state&, const rapidjson::Document&);
static void fill_foundations(game_state&);
static std::string deal_schema_json();
diff --git a/src/main/input-output/input/json-parsing/rules_parser.cpp b/src/main/input-output/input/json-parsing/rules_parser.cpp
index c47ed2b..e3c4601 100644
--- a/src/main/input-output/input/json-parsing/rules_parser.cpp
+++ b/src/main/input-output/input/json-parsing/rules_parser.cpp
@@ -22,6 +22,7 @@ typedef sol_rules::build_policy pol;
typedef sol_rules::spaces_policy s_pol;
typedef sol_rules::stock_deal_type sdt;
typedef sol_rules::face_up_policy fu;
+typedef sol_rules::direction dir;
const sol_rules rules_parser::from_file(const string rules_file) {
sol_rules sr = get_default();
@@ -305,6 +306,56 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
}
}
+ if (d.HasMember("sequences")) {
+ if (d["sequences"].IsObject()) {
+ if (d["sequences"].HasMember("count")) {
+ if (d["sequences"]["count"].IsInt()) {
+ sr.sequence_count = static_cast<uint8_t>(d["sequences"]["count"].GetInt());
+ } else {
+ json_helper::json_parse_err("[count] must be an integer");
+ }
+ }
+
+ if (d["sequences"].HasMember("build policy")) {
+ if (d["sequences"]["build policy"].IsString()) {
+ string bp_str = d["sequences"]["build policy"].GetString();
+
+ if (bp_str == "any-suit") {
+ sr.sequence_build_pol = pol::ANY_SUIT;
+ } else if (bp_str == "red-black") {
+ sr.sequence_build_pol = pol::RED_BLACK;
+ } else if (bp_str == "same-suit") {
+ sr.sequence_build_pol = pol::SAME_SUIT;
+ } else {
+ json_helper::json_parse_err("[sequences][build policy] is invalid");
+ }
+ } else {
+ json_helper::json_parse_err("[sequences][build policy] must be an string");
+ }
+ }
+
+ if (d["sequences"].HasMember("direction")) {
+ if (d["sequences"]["direction"].IsString()) {
+ string dir_str = d["sequences"]["direction"].GetString();
+
+ if (dir_str == "L") {
+ sr.sequence_direction = dir::LEFT;
+ } else if (dir_str == "R") {
+ sr.sequence_direction = dir::RIGHT;
+ } else if (dir_str == "LR") {
+ sr.sequence_direction = dir::BOTH;
+ } else {
+ json_helper::json_parse_err("[sequences][direction] is invalid");
+ }
+ } else {
+ json_helper::json_parse_err("[sequences][direction] must be an string");
+ }
+ }
+ } else {
+ json_helper::json_parse_err("[sequences] 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());
@@ -327,8 +378,9 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
int solution_types = 0;
if (sr.hole) solution_types++;
if (sr.foundations_present) solution_types++;
+ if (sr.sequence_count > 0) solution_types++;
if (solution_types != 1) {
- json_helper::json_parse_err("one and only one of [hole] and [foundations] "
+ json_helper::json_parse_err("one and only one of [hole], [foundations] and [sequences][count] > 0 "
"must be true");
}
@@ -462,6 +514,32 @@ string rules_parser::rules_schema_json() {
},
"additionalProperties": false
},
+ "sequences": {
+ "type": "object",
+ "properties": {
+ "count": {
+ "type": "integer",
+ "minimum": 0
+ },
+ "direction": {
+ "type": "string",
+ "enum": [
+ "L",
+ "R",
+ "LR"
+ ]
+ },
+ "build policy": {
+ "type": "string",
+ "enum": [
+ "any-suit",
+ "red-black",
+ "same-suit"
+ ]
+ }
+ },
+ "additionalProperties": false
+ },
"max rank": {
"type": "integer",
"minimum": 1,