From 506a17e822c0364cf440b3d8eb22dad0038ea1ca Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Sat, 14 Jul 2018 22:08:10 +0100 Subject: Add random gen and state print for accordion --- src/main/game/search-state/game_state.cpp | 17 +++++++++++++ src/main/game/search-state/game_state.h | 1 + .../input/json-parsing/deal_parser.cpp | 28 +++++++++++++++++++++- .../input-output/input/json-parsing/deal_parser.h | 1 + .../input/json-parsing/rules_parser.cpp | 3 ++- src/main/input-output/output/state_printer.cpp | 4 ++++ src/main/solver/solver.cpp | 2 +- 7 files changed, 53 insertions(+), 3 deletions(-) (limited to 'src/main') diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index b61be3b..b1a8e5e 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -98,6 +98,15 @@ game_state::game_state(const sol_rules& s_rules, streamliner_options stream_opts } } + // If there is a reserve, creates piles + if (rules.accordion_size > 0) { + uint8_t pile_count = rules.accordion_size; + for (uint8_t i = 0; i < pile_count; i++) { + piles.emplace_back(); + accordion.push_back(static_cast(piles.size() - 1)); + } + } + // Creates the tableau piles for (uint8_t i = 0; i < rules.tableau_pile_count; i++) { piles.emplace_back(); @@ -173,6 +182,14 @@ game_state::game_state(const sol_rules& s_rules, int seed, streamliner_options s } } + // Deals to the accordion + if (rules.accordion_size > 0) { + for (unsigned int i = 0; i < rules.accordion_size; i++) { + place_card(accordion[i], deck.back()); + deck.pop_back(); + } + } + // This only occurs during testing if (rules.tableau_pile_count == 0) return; diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index 05f6974..212229c 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -140,6 +140,7 @@ private: std::list reserve; std::vector foundations; std::vector sequences; + std::vector accordion; pile::ref hole; /* Pile references of starting/original layout */ 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..5efcb63 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,26 @@ 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"); + } + + for (pile::ref i = 0; i < json_card_arr.Size(); i++) { + assert(json_card_arr[i].IsString()); + pile::ref pr = gs.accordion[i]; + 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 1e501a1..b70abdf 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -440,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"); } diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index e48ed51..b418a42 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -47,6 +47,10 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { state_printer::print_header(stream, "Sequences"); state_printer::print_sequences(stream, gs.sequences, gs); } + if (gs.rules.accordion_size > 0) { + state_printer::print_header(stream, "Accordion"); + state_printer::print_top_of_piles(stream, gs.accordion, gs); + } return stream << "==================================="; } diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 3bfd6bd..cd8652f 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -48,7 +48,7 @@ solver::node::node(const move m) solver::sol_state solver::run(boost::optional &> terminate_solver) { bool states_exhausted = false; - while(!(state.is_solved() || states_exhausted)) { + while(!(/*state.is_solved() || */states_exhausted)) { // If the terminate flag was supplied and has been set to true, return if (terminate_solver && *terminate_solver) { return sol_state::timed_out; -- cgit v1.2.1