diff options
| author | Charlie Blake | 2018-10-13 22:33:07 +0100 |
|---|---|---|
| committer | GitHub | 2018-10-13 22:33:07 +0100 |
| commit | b78faa72859e37f41503db1815e5496f327d20b8 (patch) | |
| tree | f5ec04f085779c03045fab051eb0bb519d4bfd96 /src/main | |
| parent | Merge pull request #97 from thecharlesblake/comma-bug-ctd (diff) | |
| parent | Add json printing of initial deal (diff) | |
| download | metasol-b78faa72859e37f41503db1815e5496f327d20b8.tar.gz metasol-b78faa72859e37f41503db1815e5496f327d20b8.zip | |
Merge pull request #98 from thecharlesblake/deal-print
Deal print
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/game/search-state/game_state.h | 1 | ||||
| -rw-r--r-- | src/main/input-output/input/command_line_helper.cpp | 13 | ||||
| -rw-r--r-- | src/main/input-output/input/command_line_helper.h | 3 | ||||
| -rw-r--r-- | src/main/input-output/input/json-parsing/json_helper.cpp | 52 | ||||
| -rw-r--r-- | src/main/input-output/input/json-parsing/json_helper.h | 10 | ||||
| -rw-r--r-- | src/main/main.cpp | 6 |
6 files changed, 81 insertions, 4 deletions
diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index 8b14079..9bbea23 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -27,6 +27,7 @@ class game_state { friend class deal_parser; friend class state_printer; friend class test_helper; + friend class json_helper; public: enum class streamliner_options {NONE, AUTO_FOUNDATIONS, SUIT_SYMMETRY, BOTH}; diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index 365fe3f..ae0f9c4 100644 --- a/src/main/input-output/input/command_line_helper.cpp +++ b/src/main/input-output/input/command_line_helper.cpp @@ -56,9 +56,10 @@ command_line_helper::command_line_helper() " runs first with both streamliners and a 10% timeout. If this" " is unsuccessful (unsolvable or timeout), then runs again without streamliners.") ("benchmark", "outputs performance statistics for the solver on the " - "supplied solitaire game. Must supply " - "either 'random', 'benchmark', 'solvability' or list of deals to be " - "solved."); + "supplied solitaire game. Must supply " + "either 'random', 'benchmark', 'solvability' or list of deals to be " + "solved.") + ("deal-only", "outputs the starting deal for a given game type & random seed as json"); po::options_description hidden_options("Hidden options"); hidden_options.add_options() @@ -97,6 +98,8 @@ bool command_line_helper::parse(int argc, const char* argv[]) { classify = (vm.count("classify") != 0); + deal_only = (vm.count("deal-only") != 0); + if (vm.count("input-files")) { input_files = vm["input-files"].as<vector<string>>(); } @@ -273,6 +276,10 @@ bool command_line_helper::get_classify() { return classify; } +bool command_line_helper::get_deal_only() { + return deal_only; +} + uint64_t command_line_helper::get_cache_capacity() { return cache_capacity; } diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h index 862e39f..f2a9d74 100644 --- a/src/main/input-output/input/command_line_helper.h +++ b/src/main/input-output/input/command_line_helper.h @@ -19,6 +19,7 @@ public: const std::string get_rules_file(); int get_random_deal(); bool get_classify(); + bool get_deal_only(); int get_solvability(); uint get_cores(); bool get_available_game_types(); @@ -40,7 +41,6 @@ private: void print_too_many_opts_error(); void print_resume_error(); void print_streamliner_error(const std::string&); - void print_streamliner_solvability_error(); boost::program_options::options_description cmdline_options; boost::program_options::options_description main_options; @@ -53,6 +53,7 @@ private: int random_deal; bool help; bool classify; + bool deal_only; int solvability; std::vector<int> resume; uint cores; diff --git a/src/main/input-output/input/json-parsing/json_helper.cpp b/src/main/input-output/input/json-parsing/json_helper.cpp index 1d1ed85..3e6b99c 100644 --- a/src/main/input-output/input/json-parsing/json_helper.cpp +++ b/src/main/input-output/input/json-parsing/json_helper.cpp @@ -12,6 +12,9 @@ using namespace std; using namespace rapidjson; +using boost::property_tree::ptree; +using boost::property_tree::write_json; + Document json_helper::get_file_json(const string& filename) { // Reads the file into a string std::ifstream ifstr(filename); @@ -56,3 +59,52 @@ const string json_helper::schema_err_str(const SchemaValidator& validator) { ret += sb.GetString(); return ret; } + +void json_helper::print_game_state_as_json(const game_state& gs) { + ptree pt; + if (!gs.tableau_piles.empty()) pt.add_child("tableau piles", piles_to_ptree(gs, gs.tableau_piles)); + if (!gs.cells.empty()) pt.add_child("cells", piles_to_ptree(gs, gs.cells)); + if (!gs.reserve.empty()) pt.add_child("reserve", piles_to_ptree(gs, gs.reserve)); + if (!gs.foundations.empty()) pt.add_child("foundations", piles_to_ptree(gs, gs.foundations)); + if (!gs.sequences.empty()) pt.add_child("sequences", piles_to_ptree(gs, gs.sequences)); + if (!gs.accordion.empty()) pt.add_child("accordion", piles_to_ptree(gs, gs.accordion)); + if (gs.rules.stock_size > 0) { + pt.add_child("stock", pile_to_ptree(gs.piles[gs.stock])); + if (gs.rules.stock_deal_t == sol_rules::stock_deal_type::WASTE) { + pt.add_child("waste", pile_to_ptree(gs.piles[gs.waste])); + } + } + if (gs.rules.hole) pt.add_child("hole", pile_to_ptree(gs.piles[gs.hole])); + + write_json (cout, pt); +} + +ptree json_helper::piles_to_ptree(const game_state& gs, const std::list<pile::ref>& piles) { + ptree pt; + for (pile::ref pr : piles) { + pt.push_back(make_pair("", pile_to_ptree(gs.piles[pr]))); + } + return pt; +} + +ptree json_helper::piles_to_ptree(const game_state& gs, const std::vector<pile::ref>& piles) { + ptree pt; + for (pile::ref pr : piles) { + pt.push_back(make_pair("", pile_to_ptree(gs.piles[pr]))); + } + return pt; +} + +ptree json_helper::pile_to_ptree(const pile& pile) { + ptree pt; + for (pile::size_type i = pile.size(); i-->0; ) { + pt.push_back(make_pair("", card_to_ptree(pile[i]))); + } + return pt; +} + +ptree json_helper::card_to_ptree(const card& card) { + ptree pt; + pt.put("", card.to_string()); + return pt; +} diff --git a/src/main/input-output/input/json-parsing/json_helper.h b/src/main/input-output/input/json-parsing/json_helper.h index b5e2d51..460911f 100644 --- a/src/main/input-output/input/json-parsing/json_helper.h +++ b/src/main/input-output/input/json-parsing/json_helper.h @@ -7,7 +7,11 @@ #include <string> +#include <boost/property_tree/ptree.hpp> +#include <boost/property_tree/json_parser.hpp> + #include "../../../../../lib/rapidjson/schema.h" +#include "../../../game/search-state/game_state.h" class json_helper { public: @@ -15,6 +19,12 @@ public: static void json_parse_warning(const std::string& msg); static void json_parse_err(const std::string& msg); static const std::string schema_err_str(const rapidjson::SchemaValidator&); + static void print_game_state_as_json(const game_state&); +private: + static boost::property_tree::ptree piles_to_ptree(const game_state&, const std::list<pile::ref>&); + static boost::property_tree::ptree piles_to_ptree(const game_state&, const std::vector<pile::ref>&); + static boost::property_tree::ptree pile_to_ptree(const pile&); + static boost::property_tree::ptree card_to_ptree(const card&); }; diff --git a/src/main/main.cpp b/src/main/main.cpp index 808ee78..faed0ca 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -53,6 +53,12 @@ int main(int argc, const char* argv[]) { const optional<sol_rules> rules = gen_rules(clh); if (!rules) return EXIT_FAILURE; + if (clh.get_deal_only()) { + game_state gs(*rules, clh.get_random_deal(), game_state::streamliner_options::NONE); + json_helper::print_game_state_as_json(gs); + return EXIT_SUCCESS; + } + // If the user has asked for a solvability percentage, calculates it if (clh.get_solvability() > 0) { solvability_calc solv_c(*rules, clh.get_cache_capacity()); |
