diff options
| author | Charlie Blake | 2018-07-18 16:04:56 +0100 |
|---|---|---|
| committer | GitHub | 2018-07-18 16:04:56 +0100 |
| commit | 4ddf1752f3909eb1c87c06414597463208120058 (patch) | |
| tree | 62bbba8f7dd114ccd39f0e935b1bc4fbd74250dd /src/main/input-output/input | |
| parent | Merge pull request #76 from thecharlesblake/add-gaps (diff) | |
| parent | Add accordion move making (diff) | |
| download | metasol-4ddf1752f3909eb1c87c06414597463208120058.tar.gz metasol-4ddf1752f3909eb1c87c06414597463208120058.zip | |
Merge pull request #77 from thecharlesblake/add-accordion
Add accordion
Diffstat (limited to 'src/main/input-output/input')
4 files changed, 185 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 998f337..7b8cf18 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -56,6 +56,11 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { parse_sequences(gs, doc); } + // Construct accordion + if (gs.rules.accordion_size > 0) { + parse_accordion(gs, doc); + } + // If the foundations begin with cards in them, fill them if (gs.rules.foundations_present) { bool supplied_foundations = parse_foundations(gs, doc); @@ -109,7 +114,8 @@ string deal_parser::deal_schema_json() { "hole": {"$ref": "#/definitions/card"}, "stock": {"$ref": "#/definitions/cardarray"}, "waste": {"$ref": "#/definitions/cardarray"}, - "reserve": {"$ref": "#/definitions/cardarray"} + "reserve": {"$ref": "#/definitions/cardarray"}, + "accordion": {"$ref": "#/definitions/cardarray"} }, "additionalProperties": false } @@ -236,6 +242,27 @@ void deal_parser::parse_sequences(game_state& gs, const rapidjson::Document& doc } } +void deal_parser::parse_accordion(game_state &gs, const Document& doc) { + assert(doc.HasMember("accordion")); + const Value& json_accordion_piles = doc["accordion"]; + assert(json_accordion_piles.IsArray()); + + const auto& json_card_arr = json_accordion_piles.GetArray(); + + // We treat a regular reserve like multiple single-card piles, + // but a stacked reserve as a single multiple-card pile + if (json_card_arr.Size() != gs.rules.accordion_size) { + json_helper::json_parse_err("Incorrect accordion size"); + } + + auto acc_it = begin(gs.accordion); + for (pile::ref i = 0; i < json_card_arr.Size(); i++, acc_it++) { + assert(json_card_arr[i].IsString()); + pile::ref pr = *acc_it; + gs.place_card(pr, card(json_card_arr[i].GetString())); + } +} + 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 bce4a29..36f58d5 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.h +++ b/src/main/input-output/input/json-parsing/deal_parser.h @@ -24,6 +24,7 @@ public: 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 void parse_accordion(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 e3c4601..b70abdf 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -20,6 +20,7 @@ using namespace rapidjson; typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; +typedef sol_rules::accordion_policy acc_pol; typedef sol_rules::stock_deal_type sdt; typedef sol_rules::face_up_policy fu; typedef sol_rules::direction dir; @@ -306,6 +307,66 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d.HasMember("accordion")) { + if (d["accordion"].IsObject()) { + if (d["accordion"].HasMember("size")) { + if (d["accordion"]["size"].IsInt()) { + sr.accordion_size = static_cast<uint8_t>(d["accordion"]["size"].GetInt()); + } else { + json_helper::json_parse_err("[size] must be an integer"); + } + } + + if (d["accordion"].HasMember("moves")) { + if (d["accordion"]["moves"].IsArray()) { + for (auto& json_move : d["accordion"]["moves"].GetArray()) { + string move_str = json_move.GetString(); + + sol_rules::direction move_dir; + if (move_str[0] == 'L') { + move_dir = sol_rules::direction::LEFT; + } else if (move_str[0] == 'R') { + move_dir = sol_rules::direction::RIGHT; + } else { + json_helper::json_parse_err(R"([accordion][moves] must begin 'L' or 'R')"); + } + + auto count_str = move_str.substr(1, move_str.size() - 1); + auto count = static_cast<uint8_t>(stoi(count_str)); + + sr.accordion_moves.emplace_back(move_dir, count); + } + } else { + json_helper::json_parse_err("[accordion][moves] must be an array"); + } + } + + if (d["accordion"].HasMember("build policies")) { + if (d["accordion"]["build policies"].IsArray()) { + for (auto& json_policy : d["accordion"]["build policies"].GetArray()) { + string policy_str = json_policy.GetString(); + + if (policy_str == "any-suit") { + sr.accordion_pol.emplace_back(acc_pol::ANY_SUIT); + } else if (policy_str == "red-black") { + sr.accordion_pol.emplace_back(acc_pol::RED_BLACK); + } else if (policy_str == "same-suit") { + sr.accordion_pol.emplace_back(acc_pol::SAME_SUIT); + } else if (policy_str == "same-rank") { + sr.accordion_pol.emplace_back(acc_pol::SAME_RANK); + } else { + json_helper::json_parse_err("[accordion][build policies] is invalid"); + } + } + } else { + json_helper::json_parse_err("[accordion][build policies] must be an array"); + } + } + } else { + json_helper::json_parse_err("[accordion] must be an object"); + } + } + if (d.HasMember("sequences")) { if (d["sequences"].IsObject()) { if (d["sequences"].HasMember("count")) { @@ -379,8 +440,9 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { if (sr.hole) solution_types++; if (sr.foundations_present) solution_types++; if (sr.sequence_count > 0) solution_types++; + if (sr.accordion_size > 0) solution_types++; if (solution_types != 1) { - json_helper::json_parse_err("one and only one of [hole], [foundations] and [sequences][count] > 0 " + json_helper::json_parse_err("one and only one of [hole], [foundations], [accordion][size] > 0 and [sequences][count] > 0 " "must be true"); } @@ -514,6 +576,34 @@ string rules_parser::rules_schema_json() { }, "additionalProperties": false }, + "accordion": { + "type": "object", + "properties": { + "size": { + "type": "integer", + "minimum": 0 + }, + "moves": { + "items": { + "type": "string", + "pattern": "^((L|R)([1-9]|[1-4][0-9]|5[0-2]))$" + } + }, + "build policies": { + "type": "array", + "items": { + "type": "string", + "enum": [ + "same-suit", + "red-black", + "any-suit", + "same-rank" + ] + } + } + }, + "additionalProperties": false + }, "sequences": { "type": "object", "properties": { diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 88ebb5f..0c96651 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -47,6 +47,11 @@ map<string, string> sol_preset_types::mp = { "size": 0, "stacked": false }, + "accordion": { + "size": 0, + "moves": [], + "build policies": [] + }, "sequences": { "count": 0, "direction": "L", @@ -1083,6 +1088,66 @@ map<string, string> sol_preset_types::mp = { } )" + }, + { + "simple-accordion", + + R"( +{ + "foundations": { + "present": false + }, + "tableau piles": { + "count": 0 + }, + "accordion": { + "size": 10, + "moves": ["L1", "L3"], + "build policies": ["same-suit", "same-rank"] + } +} +)" + + }, + { + "accordion-knuth", + + R"( +{ + "foundations": { + "present": false + }, + "tableau piles": { + "count": 0 + }, + "accordion": { + "size": 18, + "moves": ["L1", "L3"], + "build policies": ["same-suit", "same-rank"] + } +} +)" + + }, + { + "accordion", + + R"( +{ + "foundations": { + "present": false + }, + "tableau piles": { + "count": 0 + }, + "accordion": { + "size": 52, + "moves": ["L1", "L3"], + "build policies": ["same-suit", "same-rank"] + } +} +)" + } }; |
