diff options
| author | Charlie Blake | 2018-07-14 15:06:04 +0100 |
|---|---|---|
| committer | GitHub | 2018-07-14 15:06:04 +0100 |
| commit | 7a2eaa8280ca35a52227084c037b1adb57193291 (patch) | |
| tree | 64ab75568e80890a6b5b66ab0481dd4ea24d1d24 /src/main/input-output | |
| parent | Merge pull request #75 from thecharlesblake/refactor-rules-json (diff) | |
| parent | Gaps / sequence implementation (diff) | |
| download | metasol-7a2eaa8280ca35a52227084c037b1adb57193291.tar.gz metasol-7a2eaa8280ca35a52227084c037b1adb57193291.zip | |
Merge pull request #76 from thecharlesblake/add-gaps
Add gaps
Diffstat (limited to 'src/main/input-output')
6 files changed, 195 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, diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index fdd353a..88ebb5f 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 }, + "sequences": { + "count": 0, + "direction": "L", + "build policy": "same-suit" + }, "max rank": 13, "two decks": false } @@ -1037,6 +1042,47 @@ map<string, string> sol_preset_types::mp = { } )" + }, + { + "gaps", + + R"( +{ + "foundations": { + "present": false + }, + "tableau piles": { + "count": 0 + }, + "sequences": { + "count": 4, + "direction": "L", + "build policy": "same-suit" + } +} +)" + + }, + { + "simple-gaps", + + R"( +{ + "foundations": { + "present": false + }, + "tableau piles": { + "count": 0 + }, + "sequences": { + "count": 4, + "direction": "L", + "build policy": "same-suit" + }, + "max rank": 4 +} +)" + } }; diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index 67702e2..e48ed51 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -43,6 +43,10 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { state_printer::print_header(stream, "Hole Card"); state_printer::print_top_of_pile(stream, gs.hole, gs); } + if (gs.rules.sequence_count > 0) { + state_printer::print_header(stream, "Sequences"); + state_printer::print_sequences(stream, gs.sequences, gs); + } return stream << "==================================="; } @@ -90,6 +94,25 @@ void state_printer::print_piles(ostream& stream, } } +void state_printer::print_sequences(ostream& stream, + const vector<pile::ref>& seq_rs, + const game_state& gs) { + for (auto s : seq_rs) { + stream << "\n"; + auto& pile = gs.piles[s]; + for (auto i = pile.size(); i --> 0 ;) { + card c = pile[i]; + if (c == "AS") + stream << "__"; + else + print_card(stream, pile[i]); + stream << " "; + } + stream << "\n"; + } + stream << "\n"; +} + void state_printer::print_top_of_piles(ostream& stream, const vector<pile::ref>& vp, const game_state& gs) { @@ -134,6 +157,9 @@ void state_printer::print_move(std::ostream& s, const move m) { case move::mtype::stock_to_all_tableau: s << "stock-to-all-tableau"; break; + case move::mtype::sequence: + s << "sequence"; + break; case move::mtype::null: s << "null"; break; diff --git a/src/main/input-output/output/state_printer.h b/src/main/input-output/output/state_printer.h index 7d4a702..7e01aea 100644 --- a/src/main/input-output/output/state_printer.h +++ b/src/main/input-output/output/state_printer.h @@ -19,6 +19,9 @@ private: static void print_piles(std::ostream&, const std::vector<pile::ref>&, const game_state&); + static void print_sequences(std::ostream&, + const std::vector<pile::ref>&, + const game_state&); static void print_top_of_piles(std::ostream&, const std::vector<pile::ref>&, const game_state&); |
