From 9ad093dc713f5464181b1018e80ec359ca381bb3 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 2 Jan 2018 11:27:27 +0000 Subject: Add json rules for bakers dozen --- src/main/input-output/sol_preset_types.cpp | 74 ++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 23 deletions(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/sol_preset_types.cpp b/src/main/input-output/sol_preset_types.cpp index 169a8a5..2ba14d9 100644 --- a/src/main/input-output/sol_preset_types.cpp +++ b/src/main/input-output/sol_preset_types.cpp @@ -22,7 +22,8 @@ std::map sol_preset_types::mp = { "tableau piles": { "count": 8, "build order": "descending", - "build policy": "any-suit" + "build policy": "any-suit", + "spaces policy": "any" }, "max rank": 13, "hole": false, @@ -35,85 +36,112 @@ std::map sol_preset_types::mp = { }, { - "black-hole", + "spanish-patience", R"( { "tableau piles": { - "count": 17, - "build order": "no-build" + "count": 13 + } +} +)" + + }, + { + "simple-spanish-patience", + + R"( +{ + "tableau piles": { + "count": 3 }, - "hole": true, - "foundations": false + "max rank": 2 } )" }, { - "simple-black-hole", + "free-cell", + + R"( +{ + "tableau piles": { + "build policy": "red-black" + }, + "cells": 4 +} +)" + + }, + { + "simple-free-cell", R"( { "tableau piles": { "count": 4, - "build order": "no-build" + "build policy": "red-black" }, - "max rank": 5, - "hole": true, - "foundations": false + "max rank": 4, + "cells": 1 } )" }, { - "spanish-patience", + "bakers-dozen", R"( { "tableau piles": { - "count": 13 + "count": 13, + "spaces policy": "no-build" } } )" }, { - "simple-spanish-patience", + "simple-bakers-dozen", R"( { "tableau piles": { - "count": 3 + "count": 3, + "spaces policy": "no-build" }, - "max rank": 2 + "max rank": 3 } )" }, { - "free-cell", + "black-hole", R"( { "tableau piles": { - "build policy": "red-black" + "count": 17, + "build order": "no-build" }, - "cells": 4 + "hole": true, + "foundations": false } )" }, { - "simple-free-cell", + "simple-black-hole", R"( { "tableau piles": { "count": 4, - "build policy": "red-black" + "build order": "no-build" }, - "max rank": 4, - "cells": 1 + "max rank": 5, + "hole": true, + "foundations": false } )" -- cgit v1.2.1 From 77a7aeba7854d15b140cc6dc3c69b15ed5f3b936 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 12:30:14 +0000 Subject: Major refactor, putting pile logic into game_state Much of the logic that was in the pile class was beginning to seem out of place. A lot of the rules have fairly arbitrary interactons *between* piles, which weren't best represented in the pile classes themselves, which were getting unweildy and inefficient. Hence, much of this logic has been moved to the game state class. --- src/main/input-output/deal_parser.h | 1 - src/main/input-output/sol_preset_types.cpp | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/deal_parser.h b/src/main/input-output/deal_parser.h index 255116b..76a723a 100644 --- a/src/main/input-output/deal_parser.h +++ b/src/main/input-output/deal_parser.h @@ -11,7 +11,6 @@ #include #include -#include "../game/card.h" #include "../game/sol_rules.h" class game_state; diff --git a/src/main/input-output/sol_preset_types.cpp b/src/main/input-output/sol_preset_types.cpp index 2ba14d9..3c9d295 100644 --- a/src/main/input-output/sol_preset_types.cpp +++ b/src/main/input-output/sol_preset_types.cpp @@ -122,7 +122,7 @@ std::map sol_preset_types::mp = { { "tableau piles": { "count": 17, - "build order": "no-build" + "build policy": "no-build" }, "hole": true, "foundations": false @@ -137,7 +137,7 @@ std::map sol_preset_types::mp = { { "tableau piles": { "count": 4, - "build order": "no-build" + "build policy": "no-build" }, "max rank": 5, "hole": true, -- cgit v1.2.1 From fa40f6facc5c500cae8bf890e66a7bf5d490387e Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 12:39:06 +0000 Subject: Rename util to json_helper --- src/main/input-output/deal_parser.cpp | 6 +++--- src/main/input-output/json_helper.cpp | 35 +++++++++++++++++++++++++++++++++++ src/main/input-output/json_helper.h | 19 +++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/main/input-output/json_helper.cpp create mode 100644 src/main/input-output/json_helper.h (limited to 'src/main/input-output') diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index c876e91..8637649 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -4,7 +4,7 @@ #include "deal_parser.h" #include "../game/game_state.h" -#include "../util/util.h" +#include "json_helper.h" using namespace std; using namespace rapidjson; @@ -35,7 +35,7 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& assert(json_tab_piles.IsArray()); if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) { - util::json_parse_err("Incorrect number of tableau piles"); + json_helper::json_parse_err("Incorrect number of tableau piles"); } for (auto p = std::make_pair(begin(json_tab_piles.GetArray()), begin(gs.tableau_piles)); @@ -67,7 +67,7 @@ void deal_parser::parse_cells(game_state &gs, const Document& doc) { if (json_cell_arr.Empty()) return; else if (gs.rules.cells != json_cell_arr.Size()) { - util::json_parse_err("Incorrect number of cells"); + json_helper::json_parse_err("Incorrect number of cells"); } for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.cells)); diff --git a/src/main/input-output/json_helper.cpp b/src/main/input-output/json_helper.cpp new file mode 100644 index 0000000..89f4564 --- /dev/null +++ b/src/main/input-output/json_helper.cpp @@ -0,0 +1,35 @@ +// +// Created by thecharlesblake on 11/13/17. +// + +#include +#include + +#include "json_helper.h" + +using namespace std; +using namespace rapidjson; + +Document json_helper::get_file_json(const string& filename) { + // Reads the file into a string + std::ifstream ifstr(filename); + std::stringstream buf; + buf << ifstr.rdbuf(); + + if (ifstr.fail()) { + throw runtime_error("could not read file " + filename); + } else { + Document d; + + d.Parse(buf.str().c_str()); + if (d.HasParseError()) { + throw runtime_error(filename + " not valid json"); + } else { + return d; + } + } +} + +void json_helper::json_parse_err(const string& msg) { + throw runtime_error("Error in JSON doc: " + msg); +} diff --git a/src/main/input-output/json_helper.h b/src/main/input-output/json_helper.h new file mode 100644 index 0000000..5ab24bc --- /dev/null +++ b/src/main/input-output/json_helper.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 11/13/17. +// + +#ifndef SOLVITAIRE_UTIL_H +#define SOLVITAIRE_UTIL_H + +#include + +#include + +class json_helper { +public: + static rapidjson::Document get_file_json(const std::string&); + static void json_parse_err(const std::string& msg); +}; + + +#endif //SOLVITAIRE_UTIL_H -- cgit v1.2.1 From 758d919cb5d600a421636ce54df9f28b6fc832ba Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 12:51:47 +0000 Subject: Refactor sol_rules into seperate parser class --- src/main/input-output/rules_parser.cpp | 173 +++++++++++++++++++++++++++++++++ src/main/input-output/rules_parser.h | 21 ++++ 2 files changed, 194 insertions(+) create mode 100644 src/main/input-output/rules_parser.cpp create mode 100644 src/main/input-output/rules_parser.h (limited to 'src/main/input-output') diff --git a/src/main/input-output/rules_parser.cpp b/src/main/input-output/rules_parser.cpp new file mode 100644 index 0000000..81cdd20 --- /dev/null +++ b/src/main/input-output/rules_parser.cpp @@ -0,0 +1,173 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#include +#include + +#include "rules_parser.h" +#include "../input-output/json_helper.h" +#include "../input-output/sol_preset_types.h" + +using namespace std; +using namespace rapidjson; + +//////////// +// Static // +//////////// + +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; + +const sol_rules rules_parser::from_file(const string rules_file) { + sol_rules sr = get_default(); + Document d = json_helper::get_file_json(rules_file); + + modify_sol_rules(sr, d); + + return sr; +} + +const sol_rules rules_parser::from_preset(const string sol_type) { + sol_rules sr = get_default(); + + const string preset_json = sol_preset_types::get(sol_type); + + Document d; + d.Parse(preset_json.c_str()); + assert(!d.HasParseError()); + + modify_sol_rules(sr, d); + return sr; +} + +sol_rules rules_parser::get_default() { + sol_rules sr; + const string default_json = sol_preset_types::get("default"); + + Document d; + d.Parse(default_json.c_str()); + assert(!d.HasParseError()); + + modify_sol_rules(sr, d); + return sr; +} + +void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { + + if (!d.IsObject()) { + json_helper::json_parse_err("JSON doc must be object"); + } + + if (d.HasMember("tableau piles")) { + if (d["tableau piles"].IsObject()) { + + if (d["tableau piles"].HasMember("count")) { + if (d["tableau piles"]["count"].IsInt()) { + sr.tableau_pile_count = static_cast(d["tableau piles"]["count"].GetInt()); + } else { + json_helper::json_parse_err("[tableau piles][count] must be an integer"); + } + } + + if (d["tableau piles"].HasMember("build order")) { + if (d["tableau piles"]["build order"].IsString()) { + string bo_str = d["tableau piles"]["build order"].GetString(); + + if (bo_str == "ascending") { + sr.build_ord = ord::ASCENDING; + } else if (bo_str == "descending") { + sr.build_ord = ord::DESCENDING; + } else { + json_helper::json_parse_err("[tableau piles][build order] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][build order] must be an string"); + } + } + + if (d["tableau piles"].HasMember("build policy")) { + if (d["tableau piles"]["build policy"].IsString()) { + string bp_str = d["tableau piles"]["build policy"].GetString(); + + if (bp_str == "any-suit") { + sr.build_pol = pol::ANY_SUIT; + } else if (bp_str == "red-black") { + sr.build_pol = pol::RED_BLACK; + } else if (bp_str == "same-suit") { + sr.build_pol = pol::SAME_SUIT; + } else if (bp_str == "no-build") { + sr.build_pol = pol::NO_BUILD; + } else { + json_helper::json_parse_err("[tableau piles][build policy] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][build policy] must be an string"); + } + } + + if (d["tableau piles"].HasMember("spaces policy")) { + if (d["tableau piles"]["spaces policy"].IsString()) { + string sp_str = d["tableau piles"]["spaces policy"].GetString(); + + if (sp_str == "any") { + sr.spaces_pol = s_pol::ANY; + } else if (sp_str == "no-build") { + sr.spaces_pol = s_pol::NO_BUILD; + } else { + json_helper::json_parse_err("[tableau piles][spaces policy] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); + } + } + + } else { + json_helper::json_parse_err("[tableau piles] must be an object"); + } + } + + if (d.HasMember("max rank")) { + if (d["max rank"].IsInt()) { + sr.max_rank = static_cast(d["max rank"].GetInt()); + } else { + json_helper::json_parse_err("[max rank] must be an integer"); + } + } + + if (d.HasMember("hole")) { + if (d["hole"].IsBool()) { + sr.hole = d["hole"].GetBool(); + } else { + json_helper::json_parse_err("[hole] must be a boolean"); + } + } + + if (d.HasMember("foundations")) { + if (d["foundations"].IsBool()) { + sr.foundations = d["foundations"].GetBool(); + } else { + json_helper::json_parse_err("[foundations] must be a boolean"); + } + } + + if (d.HasMember("cells")) { + if (d["cells"].IsInt()) { + sr.cells = static_cast(d["cells"].GetInt()); + } else { + json_helper::json_parse_err("[cells] must be an integer"); + } + } + + if (d.HasMember("stock size")) { + if (d["stock size"].IsInt()) { + sr.stock_size = static_cast(d["stock size"].GetInt()); + } else { + json_helper::json_parse_err("[stock size] must be an integer"); + } + } +} diff --git a/src/main/input-output/rules_parser.h b/src/main/input-output/rules_parser.h new file mode 100644 index 0000000..a478a01 --- /dev/null +++ b/src/main/input-output/rules_parser.h @@ -0,0 +1,21 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#ifndef SOLVITAIRE_RULES_PARSER_H +#define SOLVITAIRE_RULES_PARSER_H + +#include "../game/sol_rules.h" + +class rules_parser { +public: + static const sol_rules from_file(std::string); + static const sol_rules from_preset(std::string); + +private: + static sol_rules get_default(); + static void modify_sol_rules(sol_rules&, rapidjson::Document&); +}; + + +#endif //SOLVITAIRE_RULES_PARSER_H -- cgit v1.2.1 From c70a063f407534ac3d8997b9f1bc58d23c9360b8 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 13:03:26 +0000 Subject: Tidy up card class --- src/main/input-output/deal_parser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index 8637649..1746603 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -76,7 +76,7 @@ void deal_parser::parse_cells(game_state &gs, const Document& doc) { auto json_card = p.first; assert(json_card->IsString()); - gs.piles[*p.second].place(json_card->GetString()); + gs.piles[*p.second].place(card(json_card->GetString())); } } } -- cgit v1.2.1 From 7f4712b292632becfc83bc0e887271b25fefd21a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 13:05:31 +0000 Subject: Refactor io dir structure --- src/main/input-output/command_line_helper.cpp | 170 ------------------- src/main/input-output/command_line_helper.h | 40 ----- src/main/input-output/deal_parser.cpp | 82 ---------- src/main/input-output/deal_parser.h | 27 ---- .../input-output/input/command_line_helper.cpp | 170 +++++++++++++++++++ src/main/input-output/input/command_line_helper.h | 40 +++++ .../input/json-parsing/deal_parser.cpp | 82 ++++++++++ .../input-output/input/json-parsing/deal_parser.h | 27 ++++ .../input/json-parsing/json_helper.cpp | 35 ++++ .../input-output/input/json-parsing/json_helper.h | 19 +++ .../input/json-parsing/rules_parser.cpp | 173 ++++++++++++++++++++ .../input-output/input/json-parsing/rules_parser.h | 21 +++ src/main/input-output/input/sol_preset_types.cpp | 180 +++++++++++++++++++++ src/main/input-output/input/sol_preset_types.h | 19 +++ src/main/input-output/json_helper.cpp | 35 ---- src/main/input-output/json_helper.h | 19 --- src/main/input-output/log_helper.h | 19 --- src/main/input-output/output/log_helper.h | 19 +++ src/main/input-output/rules_parser.cpp | 173 -------------------- src/main/input-output/rules_parser.h | 21 --- src/main/input-output/sol_preset_types.cpp | 180 --------------------- src/main/input-output/sol_preset_types.h | 19 --- 22 files changed, 785 insertions(+), 785 deletions(-) delete mode 100644 src/main/input-output/command_line_helper.cpp delete mode 100644 src/main/input-output/command_line_helper.h delete mode 100644 src/main/input-output/deal_parser.cpp delete mode 100644 src/main/input-output/deal_parser.h create mode 100644 src/main/input-output/input/command_line_helper.cpp create mode 100644 src/main/input-output/input/command_line_helper.h create mode 100644 src/main/input-output/input/json-parsing/deal_parser.cpp create mode 100644 src/main/input-output/input/json-parsing/deal_parser.h create mode 100644 src/main/input-output/input/json-parsing/json_helper.cpp create mode 100644 src/main/input-output/input/json-parsing/json_helper.h create mode 100644 src/main/input-output/input/json-parsing/rules_parser.cpp create mode 100644 src/main/input-output/input/json-parsing/rules_parser.h create mode 100644 src/main/input-output/input/sol_preset_types.cpp create mode 100644 src/main/input-output/input/sol_preset_types.h delete mode 100644 src/main/input-output/json_helper.cpp delete mode 100644 src/main/input-output/json_helper.h delete mode 100644 src/main/input-output/log_helper.h create mode 100644 src/main/input-output/output/log_helper.h delete mode 100644 src/main/input-output/rules_parser.cpp delete mode 100644 src/main/input-output/rules_parser.h delete mode 100644 src/main/input-output/sol_preset_types.cpp delete mode 100644 src/main/input-output/sol_preset_types.h (limited to 'src/main/input-output') diff --git a/src/main/input-output/command_line_helper.cpp b/src/main/input-output/command_line_helper.cpp deleted file mode 100644 index 97751a4..0000000 --- a/src/main/input-output/command_line_helper.cpp +++ /dev/null @@ -1,170 +0,0 @@ -// -// Created by thecharlesblake on 10/16/17. -// - -#include -#include - -#include - -#include "command_line_helper.h" -#include "../game/sol_rules.h" -#include "log_helper.h" -#include "sol_preset_types.h" - -using namespace std; - -namespace po = boost::program_options; - -command_line_helper::command_line_helper() - : main_options("options") { - - main_options.add_options() - ("help", "produce help message") - ("type", po::value(), - "specify the type of the solitaire game to be solved. Must supply " - "either 'type' or 'rules' file") - ("rules", po::value(), - "the path to a JSON file describing the rules of the solitaire " - "to be solved. Must supply either 'type' or 'rules' file") - ("random", po::value(), "create and solve a random solitaire " - "deal based on a seed. Must supply either 'random' or list " - "of deals to be sold."); - - po::options_description hidden_options("Hidden options"); - hidden_options.add_options() - ("input-files", po::value>(), - "the file containing the deal to be solved"); - - cmdline_options.add(main_options).add(hidden_options); - - p.add("input-files", -1); -} - -// Returns true if we can continue solving the supplied solitaire(s) -bool command_line_helper::parse(int argc, const char* argv[]) { - po::variables_map vm; - // Attempts to parse cmdln options. Catches basic errors - try { - store(po::command_line_parser(argc, argv).options(cmdline_options) - .positional(p).run(), vm); - } catch(po::error& e) { - LOG_ERROR ("Error: " << e.what()); - return false; - } - - // Updates the variables map which the user's options are stored in - notify(vm); - - // Converts the variables map into flags and values - - help = (vm.count("help") != 0); - - if (vm.count("input-files")) { - input_files = vm["input-files"].as>(); - } - - if (vm.count("type")) { - solitaire_type = vm["type"].as(); - } - - if (vm.count("rules")) { - rules_file = vm["rules"].as(); - } - - if (vm.count("random")) { - random_deal = vm["random"].as(); - } else { - random_deal = -1; - } - - // Handle logic error scenarios - return assess_errors(); -} - -bool command_line_helper::assess_errors() { - if (help) { - print_help(); - return false; - } - - // The user must either supply input files or a random seed - - if (random_deal != -1 && !input_files.empty()) { - print_rand_plus_input_err(); - return false; - } - - if (input_files.empty() && random_deal == -1) { - print_no_input_error(); - return false; - } - - // The user must supply either a solitaire type or a rules file - if ((solitaire_type.empty() && rules_file.empty()) - || (!solitaire_type.empty() && !rules_file.empty())) { - print_sol_type_rules_error(); - return false; - } - - if (!solitaire_type.empty()) { - return assess_sol_type(); - } else { - return true; - } -} - -// Checks if the supplied solitaire type is in the list of valid solitaires -bool command_line_helper::assess_sol_type() { - if (sol_preset_types::is_valid_preset(solitaire_type)) { - return true; - - } else { - LOG_ERROR ("Error: Solitaire type is not valid: " << solitaire_type); - print_help(); - return false; - } -} - -void command_line_helper::print_help() { - LOG_ERROR ("Usage: solvitaire [options] input-file1 input-file2 ...\n" - << main_options); -} - -void command_line_helper::print_no_input_error() { - LOG_ERROR ("Error: User must supply input file(s) or the '--random' " - "option"); - print_help(); -} - -void command_line_helper::print_sol_type_rules_error() { - LOG_ERROR ("Error: User must supply either a solitaire type, or a 'rules' " - "file"); - print_help(); -} - -void command_line_helper::print_rand_plus_input_err() { - LOG_ERROR ("Error: User must supply input file(s) or the '--random' option, " - "not both"); - print_help(); -} - -const vector command_line_helper::get_input_files() { - return input_files; -} - -const string command_line_helper::get_solitaire_type() { - return solitaire_type; -} - -const string command_line_helper::get_rules_file() { - return rules_file; -} - -int command_line_helper::get_random_deal() { - return random_deal; -} - -bool command_line_helper::get_help() { - return help; -} diff --git a/src/main/input-output/command_line_helper.h b/src/main/input-output/command_line_helper.h deleted file mode 100644 index fd1468c..0000000 --- a/src/main/input-output/command_line_helper.h +++ /dev/null @@ -1,40 +0,0 @@ -// -// Created by thecharlesblake on 10/16/17. -// - -#ifndef SOLVITAIRE_COMMAND_LINE_HELPER_H -#define SOLVITAIRE_COMMAND_LINE_HELPER_H - -#include - -class command_line_helper { -public: - command_line_helper(); - - bool parse(int argc, const char* argv[]); - const std::vector get_input_files(); - const std::string get_solitaire_type(); - const std::string get_rules_file(); - int get_random_deal(); - bool get_help(); - -private: - bool assess_errors(); - bool assess_sol_type(); - void print_help(); - void print_no_input_error(); - void print_sol_type_rules_error(); - void print_rand_plus_input_err(); - - boost::program_options::options_description cmdline_options; - boost::program_options::options_description main_options; - boost::program_options::positional_options_description p; - - std::vector input_files; - std::string solitaire_type; - std::string rules_file; - int random_deal; - bool help; -}; - -#endif //SOLVITAIRE_COMMAND_LINE_HELPER_H diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp deleted file mode 100644 index 1746603..0000000 --- a/src/main/input-output/deal_parser.cpp +++ /dev/null @@ -1,82 +0,0 @@ -// -// Created by thecharlesblake on 10/21/17. -// - -#include "deal_parser.h" -#include "../game/game_state.h" -#include "json_helper.h" - -using namespace std; -using namespace rapidjson; - -typedef sol_rules::build_order ord; -typedef sol_rules::build_policy pol; - -void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { - // Construct tableau piles - if (gs.rules.tableau_pile_count > 0) { - parse_tableau_piles(gs, doc); - } - - // Construct hole card - if (gs.rules.hole) { - parse_hole(gs, doc); - } - - // Construct filled cells - if (gs.rules.cells) { - parse_cells(gs, doc); - } -} - -void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { - assert(doc.HasMember("tableau piles")); - const Value& json_tab_piles = doc["tableau piles"]; - assert(json_tab_piles.IsArray()); - - if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) { - json_helper::json_parse_err("Incorrect number of tableau piles"); - } - - for (auto p = std::make_pair(begin(json_tab_piles.GetArray()), begin(gs.tableau_piles)); - p.second != end(gs.tableau_piles); - ++p.first, ++p.second) { - - for (auto& json_card : p.first->GetArray()) { - assert(json_card.IsString()); - gs.piles[*p.second].place(card(json_card.GetString())); - } - } -} - -void deal_parser::parse_hole(game_state &gs, const Document& doc) { - if (doc.HasMember("hole")) { - const Value &json_hole = doc["hole"]; - assert(json_hole.IsString()); - - gs.piles[gs.hole].place(card(json_hole.GetString())); - } -} - -void deal_parser::parse_cells(game_state &gs, const Document& doc) { - if (doc.HasMember("cells")) { - const Value &json_cells = doc["cells"]; - assert(json_cells.IsArray()); - - const auto json_cell_arr = json_cells.GetArray(); - - if (json_cell_arr.Empty()) return; - else if (gs.rules.cells != json_cell_arr.Size()) { - json_helper::json_parse_err("Incorrect number of cells"); - } - - for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.cells)); - p.second != end(gs.cells); - ++p.first, ++p.second) { - - auto json_card = p.first; - assert(json_card->IsString()); - gs.piles[*p.second].place(card(json_card->GetString())); - } - } -} diff --git a/src/main/input-output/deal_parser.h b/src/main/input-output/deal_parser.h deleted file mode 100644 index 76a723a..0000000 --- a/src/main/input-output/deal_parser.h +++ /dev/null @@ -1,27 +0,0 @@ -// -// Created by thecharlesblake on 10/21/17. -// - -#ifndef SOLVITAIRE_DEAL_PARSER_H -#define SOLVITAIRE_DEAL_PARSER_H - -#include - -#include -#include -#include - -#include "../game/sol_rules.h" - -class game_state; - -class deal_parser { -public: - static void parse(game_state&, const rapidjson::Document&); -private: - static void parse_tableau_piles(game_state&, const rapidjson::Document&); - static void parse_hole(game_state&, const rapidjson::Document&); - static void parse_cells(game_state&, const rapidjson::Document&); -}; - -#endif //SOLVITAIRE_DEAL_PARSER_H diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp new file mode 100644 index 0000000..2391e3a --- /dev/null +++ b/src/main/input-output/input/command_line_helper.cpp @@ -0,0 +1,170 @@ +// +// Created by thecharlesblake on 10/16/17. +// + +#include +#include + +#include + +#include "command_line_helper.h" +#include "../../game/sol_rules.h" +#include "../output/log_helper.h" +#include "sol_preset_types.h" + +using namespace std; + +namespace po = boost::program_options; + +command_line_helper::command_line_helper() + : main_options("options") { + + main_options.add_options() + ("help", "produce help message") + ("type", po::value(), + "specify the type of the solitaire game to be solved. Must supply " + "either 'type' or 'rules' file") + ("rules", po::value(), + "the path to a JSON file describing the rules of the solitaire " + "to be solved. Must supply either 'type' or 'rules' file") + ("random", po::value(), "create and solve a random solitaire " + "deal based on a seed. Must supply either 'random' or list " + "of deals to be sold."); + + po::options_description hidden_options("Hidden options"); + hidden_options.add_options() + ("input-files", po::value>(), + "the file containing the deal to be solved"); + + cmdline_options.add(main_options).add(hidden_options); + + p.add("input-files", -1); +} + +// Returns true if we can continue solving the supplied solitaire(s) +bool command_line_helper::parse(int argc, const char* argv[]) { + po::variables_map vm; + // Attempts to parse cmdln options. Catches basic errors + try { + store(po::command_line_parser(argc, argv).options(cmdline_options) + .positional(p).run(), vm); + } catch(po::error& e) { + LOG_ERROR ("Error: " << e.what()); + return false; + } + + // Updates the variables map which the user's options are stored in + notify(vm); + + // Converts the variables map into flags and values + + help = (vm.count("help") != 0); + + if (vm.count("input-files")) { + input_files = vm["input-files"].as>(); + } + + if (vm.count("type")) { + solitaire_type = vm["type"].as(); + } + + if (vm.count("rules")) { + rules_file = vm["rules"].as(); + } + + if (vm.count("random")) { + random_deal = vm["random"].as(); + } else { + random_deal = -1; + } + + // Handle logic error scenarios + return assess_errors(); +} + +bool command_line_helper::assess_errors() { + if (help) { + print_help(); + return false; + } + + // The user must either supply input files or a random seed + + if (random_deal != -1 && !input_files.empty()) { + print_rand_plus_input_err(); + return false; + } + + if (input_files.empty() && random_deal == -1) { + print_no_input_error(); + return false; + } + + // The user must supply either a solitaire type or a rules file + if ((solitaire_type.empty() && rules_file.empty()) + || (!solitaire_type.empty() && !rules_file.empty())) { + print_sol_type_rules_error(); + return false; + } + + if (!solitaire_type.empty()) { + return assess_sol_type(); + } else { + return true; + } +} + +// Checks if the supplied solitaire type is in the list of valid solitaires +bool command_line_helper::assess_sol_type() { + if (sol_preset_types::is_valid_preset(solitaire_type)) { + return true; + + } else { + LOG_ERROR ("Error: Solitaire type is not valid: " << solitaire_type); + print_help(); + return false; + } +} + +void command_line_helper::print_help() { + LOG_ERROR ("Usage: solvitaire [options] input-file1 input-file2 ...\n" + << main_options); +} + +void command_line_helper::print_no_input_error() { + LOG_ERROR ("Error: User must supply input file(s) or the '--random' " + "option"); + print_help(); +} + +void command_line_helper::print_sol_type_rules_error() { + LOG_ERROR ("Error: User must supply either a solitaire type, or a 'rules' " + "file"); + print_help(); +} + +void command_line_helper::print_rand_plus_input_err() { + LOG_ERROR ("Error: User must supply input file(s) or the '--random' option, " + "not both"); + print_help(); +} + +const vector command_line_helper::get_input_files() { + return input_files; +} + +const string command_line_helper::get_solitaire_type() { + return solitaire_type; +} + +const string command_line_helper::get_rules_file() { + return rules_file; +} + +int command_line_helper::get_random_deal() { + return random_deal; +} + +bool command_line_helper::get_help() { + return help; +} diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h new file mode 100644 index 0000000..fd1468c --- /dev/null +++ b/src/main/input-output/input/command_line_helper.h @@ -0,0 +1,40 @@ +// +// Created by thecharlesblake on 10/16/17. +// + +#ifndef SOLVITAIRE_COMMAND_LINE_HELPER_H +#define SOLVITAIRE_COMMAND_LINE_HELPER_H + +#include + +class command_line_helper { +public: + command_line_helper(); + + bool parse(int argc, const char* argv[]); + const std::vector get_input_files(); + const std::string get_solitaire_type(); + const std::string get_rules_file(); + int get_random_deal(); + bool get_help(); + +private: + bool assess_errors(); + bool assess_sol_type(); + void print_help(); + void print_no_input_error(); + void print_sol_type_rules_error(); + void print_rand_plus_input_err(); + + boost::program_options::options_description cmdline_options; + boost::program_options::options_description main_options; + boost::program_options::positional_options_description p; + + std::vector input_files; + std::string solitaire_type; + std::string rules_file; + int random_deal; + bool help; +}; + +#endif //SOLVITAIRE_COMMAND_LINE_HELPER_H diff --git a/src/main/input-output/input/json-parsing/deal_parser.cpp b/src/main/input-output/input/json-parsing/deal_parser.cpp new file mode 100644 index 0000000..0e89f43 --- /dev/null +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -0,0 +1,82 @@ +// +// Created by thecharlesblake on 10/21/17. +// + +#include "deal_parser.h" +#include "../../../game/game_state.h" +#include "json_helper.h" + +using namespace std; +using namespace rapidjson; + +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; + +void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { + // Construct tableau piles + if (gs.rules.tableau_pile_count > 0) { + parse_tableau_piles(gs, doc); + } + + // Construct hole card + if (gs.rules.hole) { + parse_hole(gs, doc); + } + + // Construct filled cells + if (gs.rules.cells) { + parse_cells(gs, doc); + } +} + +void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { + assert(doc.HasMember("tableau piles")); + const Value& json_tab_piles = doc["tableau piles"]; + assert(json_tab_piles.IsArray()); + + if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) { + json_helper::json_parse_err("Incorrect number of tableau piles"); + } + + for (auto p = std::make_pair(begin(json_tab_piles.GetArray()), begin(gs.tableau_piles)); + p.second != end(gs.tableau_piles); + ++p.first, ++p.second) { + + for (auto& json_card : p.first->GetArray()) { + assert(json_card.IsString()); + gs.piles[*p.second].place(card(json_card.GetString())); + } + } +} + +void deal_parser::parse_hole(game_state &gs, const Document& doc) { + if (doc.HasMember("hole")) { + const Value &json_hole = doc["hole"]; + assert(json_hole.IsString()); + + gs.piles[gs.hole].place(card(json_hole.GetString())); + } +} + +void deal_parser::parse_cells(game_state &gs, const Document& doc) { + if (doc.HasMember("cells")) { + const Value &json_cells = doc["cells"]; + assert(json_cells.IsArray()); + + const auto json_cell_arr = json_cells.GetArray(); + + if (json_cell_arr.Empty()) return; + else if (gs.rules.cells != json_cell_arr.Size()) { + json_helper::json_parse_err("Incorrect number of cells"); + } + + for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.cells)); + p.second != end(gs.cells); + ++p.first, ++p.second) { + + auto json_card = p.first; + assert(json_card->IsString()); + gs.piles[*p.second].place(card(json_card->GetString())); + } + } +} diff --git a/src/main/input-output/input/json-parsing/deal_parser.h b/src/main/input-output/input/json-parsing/deal_parser.h new file mode 100644 index 0000000..9db9e2b --- /dev/null +++ b/src/main/input-output/input/json-parsing/deal_parser.h @@ -0,0 +1,27 @@ +// +// Created by thecharlesblake on 10/21/17. +// + +#ifndef SOLVITAIRE_DEAL_PARSER_H +#define SOLVITAIRE_DEAL_PARSER_H + +#include + +#include +#include +#include + +#include "../../../game/sol_rules.h" + +class game_state; + +class deal_parser { +public: + static void parse(game_state&, const rapidjson::Document&); +private: + static void parse_tableau_piles(game_state&, const rapidjson::Document&); + static void parse_hole(game_state&, const rapidjson::Document&); + static void parse_cells(game_state&, const rapidjson::Document&); +}; + +#endif //SOLVITAIRE_DEAL_PARSER_H diff --git a/src/main/input-output/input/json-parsing/json_helper.cpp b/src/main/input-output/input/json-parsing/json_helper.cpp new file mode 100644 index 0000000..89f4564 --- /dev/null +++ b/src/main/input-output/input/json-parsing/json_helper.cpp @@ -0,0 +1,35 @@ +// +// Created by thecharlesblake on 11/13/17. +// + +#include +#include + +#include "json_helper.h" + +using namespace std; +using namespace rapidjson; + +Document json_helper::get_file_json(const string& filename) { + // Reads the file into a string + std::ifstream ifstr(filename); + std::stringstream buf; + buf << ifstr.rdbuf(); + + if (ifstr.fail()) { + throw runtime_error("could not read file " + filename); + } else { + Document d; + + d.Parse(buf.str().c_str()); + if (d.HasParseError()) { + throw runtime_error(filename + " not valid json"); + } else { + return d; + } + } +} + +void json_helper::json_parse_err(const string& msg) { + throw runtime_error("Error in JSON doc: " + msg); +} diff --git a/src/main/input-output/input/json-parsing/json_helper.h b/src/main/input-output/input/json-parsing/json_helper.h new file mode 100644 index 0000000..5ab24bc --- /dev/null +++ b/src/main/input-output/input/json-parsing/json_helper.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 11/13/17. +// + +#ifndef SOLVITAIRE_UTIL_H +#define SOLVITAIRE_UTIL_H + +#include + +#include + +class json_helper { +public: + static rapidjson::Document get_file_json(const std::string&); + static void json_parse_err(const std::string& msg); +}; + + +#endif //SOLVITAIRE_UTIL_H diff --git a/src/main/input-output/input/json-parsing/rules_parser.cpp b/src/main/input-output/input/json-parsing/rules_parser.cpp new file mode 100644 index 0000000..200dbab --- /dev/null +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -0,0 +1,173 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#include +#include + +#include "rules_parser.h" +#include "json_helper.h" +#include "../sol_preset_types.h" + +using namespace std; +using namespace rapidjson; + +//////////// +// Static // +//////////// + +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; + +const sol_rules rules_parser::from_file(const string rules_file) { + sol_rules sr = get_default(); + Document d = json_helper::get_file_json(rules_file); + + modify_sol_rules(sr, d); + + return sr; +} + +const sol_rules rules_parser::from_preset(const string sol_type) { + sol_rules sr = get_default(); + + const string preset_json = sol_preset_types::get(sol_type); + + Document d; + d.Parse(preset_json.c_str()); + assert(!d.HasParseError()); + + modify_sol_rules(sr, d); + return sr; +} + +sol_rules rules_parser::get_default() { + sol_rules sr; + const string default_json = sol_preset_types::get("default"); + + Document d; + d.Parse(default_json.c_str()); + assert(!d.HasParseError()); + + modify_sol_rules(sr, d); + return sr; +} + +void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { + + if (!d.IsObject()) { + json_helper::json_parse_err("JSON doc must be object"); + } + + if (d.HasMember("tableau piles")) { + if (d["tableau piles"].IsObject()) { + + if (d["tableau piles"].HasMember("count")) { + if (d["tableau piles"]["count"].IsInt()) { + sr.tableau_pile_count = static_cast(d["tableau piles"]["count"].GetInt()); + } else { + json_helper::json_parse_err("[tableau piles][count] must be an integer"); + } + } + + if (d["tableau piles"].HasMember("build order")) { + if (d["tableau piles"]["build order"].IsString()) { + string bo_str = d["tableau piles"]["build order"].GetString(); + + if (bo_str == "ascending") { + sr.build_ord = ord::ASCENDING; + } else if (bo_str == "descending") { + sr.build_ord = ord::DESCENDING; + } else { + json_helper::json_parse_err("[tableau piles][build order] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][build order] must be an string"); + } + } + + if (d["tableau piles"].HasMember("build policy")) { + if (d["tableau piles"]["build policy"].IsString()) { + string bp_str = d["tableau piles"]["build policy"].GetString(); + + if (bp_str == "any-suit") { + sr.build_pol = pol::ANY_SUIT; + } else if (bp_str == "red-black") { + sr.build_pol = pol::RED_BLACK; + } else if (bp_str == "same-suit") { + sr.build_pol = pol::SAME_SUIT; + } else if (bp_str == "no-build") { + sr.build_pol = pol::NO_BUILD; + } else { + json_helper::json_parse_err("[tableau piles][build policy] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][build policy] must be an string"); + } + } + + if (d["tableau piles"].HasMember("spaces policy")) { + if (d["tableau piles"]["spaces policy"].IsString()) { + string sp_str = d["tableau piles"]["spaces policy"].GetString(); + + if (sp_str == "any") { + sr.spaces_pol = s_pol::ANY; + } else if (sp_str == "no-build") { + sr.spaces_pol = s_pol::NO_BUILD; + } else { + json_helper::json_parse_err("[tableau piles][spaces policy] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); + } + } + + } else { + json_helper::json_parse_err("[tableau piles] must be an object"); + } + } + + if (d.HasMember("max rank")) { + if (d["max rank"].IsInt()) { + sr.max_rank = static_cast(d["max rank"].GetInt()); + } else { + json_helper::json_parse_err("[max rank] must be an integer"); + } + } + + if (d.HasMember("hole")) { + if (d["hole"].IsBool()) { + sr.hole = d["hole"].GetBool(); + } else { + json_helper::json_parse_err("[hole] must be a boolean"); + } + } + + if (d.HasMember("foundations")) { + if (d["foundations"].IsBool()) { + sr.foundations = d["foundations"].GetBool(); + } else { + json_helper::json_parse_err("[foundations] must be a boolean"); + } + } + + if (d.HasMember("cells")) { + if (d["cells"].IsInt()) { + sr.cells = static_cast(d["cells"].GetInt()); + } else { + json_helper::json_parse_err("[cells] must be an integer"); + } + } + + if (d.HasMember("stock size")) { + if (d["stock size"].IsInt()) { + sr.stock_size = static_cast(d["stock size"].GetInt()); + } else { + json_helper::json_parse_err("[stock size] must be an integer"); + } + } +} diff --git a/src/main/input-output/input/json-parsing/rules_parser.h b/src/main/input-output/input/json-parsing/rules_parser.h new file mode 100644 index 0000000..1e28867 --- /dev/null +++ b/src/main/input-output/input/json-parsing/rules_parser.h @@ -0,0 +1,21 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#ifndef SOLVITAIRE_RULES_PARSER_H +#define SOLVITAIRE_RULES_PARSER_H + +#include "../../game/sol_rules.h" + +class rules_parser { +public: + static const sol_rules from_file(std::string); + static const sol_rules from_preset(std::string); + +private: + static sol_rules get_default(); + static void modify_sol_rules(sol_rules&, rapidjson::Document&); +}; + + +#endif //SOLVITAIRE_RULES_PARSER_H diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp new file mode 100644 index 0000000..3c9d295 --- /dev/null +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -0,0 +1,180 @@ +// +// Created by thecharlesblake on 11/14/17. +// + +#include "sol_preset_types.h" +using namespace std; + +string sol_preset_types::get(const string& s) { + return mp[s]; +} + +bool sol_preset_types::is_valid_preset(const string& s) { + return mp.count(s) > 0; +} + +std::map sol_preset_types::mp = { + { + "default", + + R"( +{ + "tableau piles": { + "count": 8, + "build order": "descending", + "build policy": "any-suit", + "spaces policy": "any" + }, + "max rank": 13, + "hole": false, + "foundations": true, + "cells": 0, + "reserve size": 0, + "stock size": 0 +} +)" + + }, + { + "spanish-patience", + + R"( +{ + "tableau piles": { + "count": 13 + } +} +)" + + }, + { + "simple-spanish-patience", + + R"( +{ + "tableau piles": { + "count": 3 + }, + "max rank": 2 +} +)" + + }, + { + "free-cell", + + R"( +{ + "tableau piles": { + "build policy": "red-black" + }, + "cells": 4 +} +)" + + }, + { + "simple-free-cell", + + R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black" + }, + "max rank": 4, + "cells": 1 +} +)" + + }, + { + "bakers-dozen", + + R"( +{ + "tableau piles": { + "count": 13, + "spaces policy": "no-build" + } +} +)" + + }, + { + "simple-bakers-dozen", + + R"( +{ + "tableau piles": { + "count": 3, + "spaces policy": "no-build" + }, + "max rank": 3 +} +)" + + }, + { + "black-hole", + + R"( +{ + "tableau piles": { + "count": 17, + "build policy": "no-build" + }, + "hole": true, + "foundations": false +} +)" + + }, + { + "simple-black-hole", + + R"( +{ + "tableau piles": { + "count": 4, + "build policy": "no-build" + }, + "max rank": 5, + "hole": true, + "foundations": false +} +)" + + }, + { + "canfield", + + R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black" + }, + "reserve size": 14, + "stock size": 34 +} +)" + + }, + { + "simple-canfield", + + R"( +{ + "tableau piles": { + "count": 2, + "build policy": "red-black" + }, + "max rank": 2, + "reserve size": 2, + "stock size": 2 +} +)" + + } +}; \ No newline at end of file diff --git a/src/main/input-output/input/sol_preset_types.h b/src/main/input-output/input/sol_preset_types.h new file mode 100644 index 0000000..02f602d --- /dev/null +++ b/src/main/input-output/input/sol_preset_types.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 11/14/17. +// + +#ifndef SOLVITAIRE_SOL_PRESET_TYPES_H +#define SOLVITAIRE_SOL_PRESET_TYPES_H + +#include +#include + +class sol_preset_types { +public: + static std::string get(const std::string& s); + static bool is_valid_preset(const std::string& s); +private: + static std::map mp; +}; + +#endif //SOLVITAIRE_SOL_PRESET_TYPES_H diff --git a/src/main/input-output/json_helper.cpp b/src/main/input-output/json_helper.cpp deleted file mode 100644 index 89f4564..0000000 --- a/src/main/input-output/json_helper.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// -// Created by thecharlesblake on 11/13/17. -// - -#include -#include - -#include "json_helper.h" - -using namespace std; -using namespace rapidjson; - -Document json_helper::get_file_json(const string& filename) { - // Reads the file into a string - std::ifstream ifstr(filename); - std::stringstream buf; - buf << ifstr.rdbuf(); - - if (ifstr.fail()) { - throw runtime_error("could not read file " + filename); - } else { - Document d; - - d.Parse(buf.str().c_str()); - if (d.HasParseError()) { - throw runtime_error(filename + " not valid json"); - } else { - return d; - } - } -} - -void json_helper::json_parse_err(const string& msg) { - throw runtime_error("Error in JSON doc: " + msg); -} diff --git a/src/main/input-output/json_helper.h b/src/main/input-output/json_helper.h deleted file mode 100644 index 5ab24bc..0000000 --- a/src/main/input-output/json_helper.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by thecharlesblake on 11/13/17. -// - -#ifndef SOLVITAIRE_UTIL_H -#define SOLVITAIRE_UTIL_H - -#include - -#include - -class json_helper { -public: - static rapidjson::Document get_file_json(const std::string&); - static void json_parse_err(const std::string& msg); -}; - - -#endif //SOLVITAIRE_UTIL_H diff --git a/src/main/input-output/log_helper.h b/src/main/input-output/log_helper.h deleted file mode 100644 index 71cc5f4..0000000 --- a/src/main/input-output/log_helper.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by thecharlesblake on 10/16/17. -// - -#ifndef SOLVITAIRE_LOG_HELPER_H -#define SOLVITAIRE_LOG_HELPER_H - -#include - -#if defined NDEBUG - #define LOG_DEBUG(msg) -#else - #define LOG_DEBUG(msg) clog << "[debug]\n" << msg << "\n" -#endif -#define LOG_INFO(msg) clog << "[info] " << msg << "\n" -#define LOG_WARNING clog(msg) << "[warning] " << msg << "\n" -#define LOG_ERROR(msg) cerr << "[ERROR] " << msg << "\n" - -#endif //SOLVITAIRE_LOG_HELPER_H diff --git a/src/main/input-output/output/log_helper.h b/src/main/input-output/output/log_helper.h new file mode 100644 index 0000000..71cc5f4 --- /dev/null +++ b/src/main/input-output/output/log_helper.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 10/16/17. +// + +#ifndef SOLVITAIRE_LOG_HELPER_H +#define SOLVITAIRE_LOG_HELPER_H + +#include + +#if defined NDEBUG + #define LOG_DEBUG(msg) +#else + #define LOG_DEBUG(msg) clog << "[debug]\n" << msg << "\n" +#endif +#define LOG_INFO(msg) clog << "[info] " << msg << "\n" +#define LOG_WARNING clog(msg) << "[warning] " << msg << "\n" +#define LOG_ERROR(msg) cerr << "[ERROR] " << msg << "\n" + +#endif //SOLVITAIRE_LOG_HELPER_H diff --git a/src/main/input-output/rules_parser.cpp b/src/main/input-output/rules_parser.cpp deleted file mode 100644 index 81cdd20..0000000 --- a/src/main/input-output/rules_parser.cpp +++ /dev/null @@ -1,173 +0,0 @@ -// -// Created by thecharlesblake on 1/3/18. -// - -#include -#include - -#include "rules_parser.h" -#include "../input-output/json_helper.h" -#include "../input-output/sol_preset_types.h" - -using namespace std; -using namespace rapidjson; - -//////////// -// Static // -//////////// - -typedef sol_rules::build_order ord; -typedef sol_rules::build_policy pol; -typedef sol_rules::spaces_policy s_pol; - -const sol_rules rules_parser::from_file(const string rules_file) { - sol_rules sr = get_default(); - Document d = json_helper::get_file_json(rules_file); - - modify_sol_rules(sr, d); - - return sr; -} - -const sol_rules rules_parser::from_preset(const string sol_type) { - sol_rules sr = get_default(); - - const string preset_json = sol_preset_types::get(sol_type); - - Document d; - d.Parse(preset_json.c_str()); - assert(!d.HasParseError()); - - modify_sol_rules(sr, d); - return sr; -} - -sol_rules rules_parser::get_default() { - sol_rules sr; - const string default_json = sol_preset_types::get("default"); - - Document d; - d.Parse(default_json.c_str()); - assert(!d.HasParseError()); - - modify_sol_rules(sr, d); - return sr; -} - -void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { - - if (!d.IsObject()) { - json_helper::json_parse_err("JSON doc must be object"); - } - - if (d.HasMember("tableau piles")) { - if (d["tableau piles"].IsObject()) { - - if (d["tableau piles"].HasMember("count")) { - if (d["tableau piles"]["count"].IsInt()) { - sr.tableau_pile_count = static_cast(d["tableau piles"]["count"].GetInt()); - } else { - json_helper::json_parse_err("[tableau piles][count] must be an integer"); - } - } - - if (d["tableau piles"].HasMember("build order")) { - if (d["tableau piles"]["build order"].IsString()) { - string bo_str = d["tableau piles"]["build order"].GetString(); - - if (bo_str == "ascending") { - sr.build_ord = ord::ASCENDING; - } else if (bo_str == "descending") { - sr.build_ord = ord::DESCENDING; - } else { - json_helper::json_parse_err("[tableau piles][build order] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][build order] must be an string"); - } - } - - if (d["tableau piles"].HasMember("build policy")) { - if (d["tableau piles"]["build policy"].IsString()) { - string bp_str = d["tableau piles"]["build policy"].GetString(); - - if (bp_str == "any-suit") { - sr.build_pol = pol::ANY_SUIT; - } else if (bp_str == "red-black") { - sr.build_pol = pol::RED_BLACK; - } else if (bp_str == "same-suit") { - sr.build_pol = pol::SAME_SUIT; - } else if (bp_str == "no-build") { - sr.build_pol = pol::NO_BUILD; - } else { - json_helper::json_parse_err("[tableau piles][build policy] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][build policy] must be an string"); - } - } - - if (d["tableau piles"].HasMember("spaces policy")) { - if (d["tableau piles"]["spaces policy"].IsString()) { - string sp_str = d["tableau piles"]["spaces policy"].GetString(); - - if (sp_str == "any") { - sr.spaces_pol = s_pol::ANY; - } else if (sp_str == "no-build") { - sr.spaces_pol = s_pol::NO_BUILD; - } else { - json_helper::json_parse_err("[tableau piles][spaces policy] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); - } - } - - } else { - json_helper::json_parse_err("[tableau piles] must be an object"); - } - } - - if (d.HasMember("max rank")) { - if (d["max rank"].IsInt()) { - sr.max_rank = static_cast(d["max rank"].GetInt()); - } else { - json_helper::json_parse_err("[max rank] must be an integer"); - } - } - - if (d.HasMember("hole")) { - if (d["hole"].IsBool()) { - sr.hole = d["hole"].GetBool(); - } else { - json_helper::json_parse_err("[hole] must be a boolean"); - } - } - - if (d.HasMember("foundations")) { - if (d["foundations"].IsBool()) { - sr.foundations = d["foundations"].GetBool(); - } else { - json_helper::json_parse_err("[foundations] must be a boolean"); - } - } - - if (d.HasMember("cells")) { - if (d["cells"].IsInt()) { - sr.cells = static_cast(d["cells"].GetInt()); - } else { - json_helper::json_parse_err("[cells] must be an integer"); - } - } - - if (d.HasMember("stock size")) { - if (d["stock size"].IsInt()) { - sr.stock_size = static_cast(d["stock size"].GetInt()); - } else { - json_helper::json_parse_err("[stock size] must be an integer"); - } - } -} diff --git a/src/main/input-output/rules_parser.h b/src/main/input-output/rules_parser.h deleted file mode 100644 index a478a01..0000000 --- a/src/main/input-output/rules_parser.h +++ /dev/null @@ -1,21 +0,0 @@ -// -// Created by thecharlesblake on 1/3/18. -// - -#ifndef SOLVITAIRE_RULES_PARSER_H -#define SOLVITAIRE_RULES_PARSER_H - -#include "../game/sol_rules.h" - -class rules_parser { -public: - static const sol_rules from_file(std::string); - static const sol_rules from_preset(std::string); - -private: - static sol_rules get_default(); - static void modify_sol_rules(sol_rules&, rapidjson::Document&); -}; - - -#endif //SOLVITAIRE_RULES_PARSER_H diff --git a/src/main/input-output/sol_preset_types.cpp b/src/main/input-output/sol_preset_types.cpp deleted file mode 100644 index 3c9d295..0000000 --- a/src/main/input-output/sol_preset_types.cpp +++ /dev/null @@ -1,180 +0,0 @@ -// -// Created by thecharlesblake on 11/14/17. -// - -#include "sol_preset_types.h" -using namespace std; - -string sol_preset_types::get(const string& s) { - return mp[s]; -} - -bool sol_preset_types::is_valid_preset(const string& s) { - return mp.count(s) > 0; -} - -std::map sol_preset_types::mp = { - { - "default", - - R"( -{ - "tableau piles": { - "count": 8, - "build order": "descending", - "build policy": "any-suit", - "spaces policy": "any" - }, - "max rank": 13, - "hole": false, - "foundations": true, - "cells": 0, - "reserve size": 0, - "stock size": 0 -} -)" - - }, - { - "spanish-patience", - - R"( -{ - "tableau piles": { - "count": 13 - } -} -)" - - }, - { - "simple-spanish-patience", - - R"( -{ - "tableau piles": { - "count": 3 - }, - "max rank": 2 -} -)" - - }, - { - "free-cell", - - R"( -{ - "tableau piles": { - "build policy": "red-black" - }, - "cells": 4 -} -)" - - }, - { - "simple-free-cell", - - R"( -{ - "tableau piles": { - "count": 4, - "build policy": "red-black" - }, - "max rank": 4, - "cells": 1 -} -)" - - }, - { - "bakers-dozen", - - R"( -{ - "tableau piles": { - "count": 13, - "spaces policy": "no-build" - } -} -)" - - }, - { - "simple-bakers-dozen", - - R"( -{ - "tableau piles": { - "count": 3, - "spaces policy": "no-build" - }, - "max rank": 3 -} -)" - - }, - { - "black-hole", - - R"( -{ - "tableau piles": { - "count": 17, - "build policy": "no-build" - }, - "hole": true, - "foundations": false -} -)" - - }, - { - "simple-black-hole", - - R"( -{ - "tableau piles": { - "count": 4, - "build policy": "no-build" - }, - "max rank": 5, - "hole": true, - "foundations": false -} -)" - - }, - { - "canfield", - - R"( -{ - "tableau piles": { - "count": 4, - "build policy": "red-black" - }, - "reserve size": 14, - "stock size": 34 -} -)" - - }, - { - "simple-canfield", - - R"( -{ - "tableau piles": { - "count": 2, - "build policy": "red-black" - }, - "max rank": 2, - "reserve size": 2, - "stock size": 2 -} -)" - - } -}; \ No newline at end of file diff --git a/src/main/input-output/sol_preset_types.h b/src/main/input-output/sol_preset_types.h deleted file mode 100644 index 02f602d..0000000 --- a/src/main/input-output/sol_preset_types.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by thecharlesblake on 11/14/17. -// - -#ifndef SOLVITAIRE_SOL_PRESET_TYPES_H -#define SOLVITAIRE_SOL_PRESET_TYPES_H - -#include -#include - -class sol_preset_types { -public: - static std::string get(const std::string& s); - static bool is_valid_preset(const std::string& s); -private: - static std::map mp; -}; - -#endif //SOLVITAIRE_SOL_PRESET_TYPES_H -- cgit v1.2.1 From 4208d2570e74ab54d1878c72110bd380e7b197f2 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 15:21:47 +0000 Subject: Move game state printing into dedicated class --- .../input-output/input/json-parsing/rules_parser.h | 2 +- src/main/input-output/output/state_printer.cpp | 96 ++++++++++++++++++++++ src/main/input-output/output/state_printer.h | 28 +++++++ 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 src/main/input-output/output/state_printer.cpp create mode 100644 src/main/input-output/output/state_printer.h (limited to 'src/main/input-output') diff --git a/src/main/input-output/input/json-parsing/rules_parser.h b/src/main/input-output/input/json-parsing/rules_parser.h index 1e28867..21dc635 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.h +++ b/src/main/input-output/input/json-parsing/rules_parser.h @@ -5,7 +5,7 @@ #ifndef SOLVITAIRE_RULES_PARSER_H #define SOLVITAIRE_RULES_PARSER_H -#include "../../game/sol_rules.h" +#include "../../../game/sol_rules.h" class rules_parser { public: diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp new file mode 100644 index 0000000..c9b8bc9 --- /dev/null +++ b/src/main/input-output/output/state_printer.cpp @@ -0,0 +1,96 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#include "state_printer.h" + +using namespace std; + +ostream& state_printer::print(ostream& stream, const game_state& gs) { + if (gs.rules.foundations) { + state_printer::print_header(stream, "Foundations"); + state_printer::print_top_of_piles(stream, gs.foundations, gs); + } + if (gs.rules.cells) { + state_printer::print_header(stream, "Cells"); + state_printer::print_piles(stream, gs.cells, gs); + } + if (gs.rules.tableau_pile_count > 0) { + state_printer::print_header(stream, "Tableau Piles"); + state_printer::print_piles(stream, gs.tableau_piles, gs); + } + if (gs.rules.stock_size > 0) { + state_printer::print_header(stream, "Stock | Waste"); + state_printer::print_piles(stream, {gs.stock, gs.waste}, gs); + } + if (gs.rules.hole) { + state_printer::print_header(stream, "Hole Card"); + state_printer::print_top_of_pile(stream, gs.hole, gs); + } + return stream << "==================================="; +} + +void state_printer::print_header(ostream& stream, const char* header) { + stream << "--- " << header << " "; + size_t pad = 20 - strlen(header); + for (size_t i = 0; i < pad; i++) { + stream << '-'; + } + stream << "\n"; +} + +void state_printer::print_piles(ostream& stream, + const vector& pile_rs, + const game_state& gs) { + bool empty_row = false; + vector::size_type row_idx = 0; + + // Loops through the rows in each pile, starting from the bottom, until + // one is empty + while (!empty_row) { + // Loops through the current row to determine if it is empty + empty_row = true; + for (const game_state::pile_ref pile_r : pile_rs) { + if (gs.piles[pile_r].size() > row_idx) { + empty_row = false; + break; + } + } + if (!empty_row || row_idx == 0) { + // Loops through the current (non-empty) row, and outputs the values + for (const game_state::pile_ref pile_r : pile_rs) { + if (gs.piles[pile_r].size() > row_idx) { + stream << gs.piles[pile_r][row_idx]; + } else if (row_idx == 0) { + stream << "[]"; + } + stream << "\t"; + } + + stream << "\n"; + row_idx++; + } + } +} + +void state_printer::print_top_of_piles(ostream& stream, + const vector& vp, + const game_state& gs) { + vector top(vp); + + for (game_state::pile_ref p : top) { + if (gs.piles[p].empty()) { + stream << "[]"; + } else { + stream << gs.piles[p].top_card(); + } + stream << "\t"; + } + stream << "\n"; +} + +void state_printer::print_top_of_pile(ostream& stream, + const game_state::pile_ref pile_r, + const game_state& gs) { + print_top_of_piles(stream, {pile_r}, gs); +} \ No newline at end of file diff --git a/src/main/input-output/output/state_printer.h b/src/main/input-output/output/state_printer.h new file mode 100644 index 0000000..c918bcd --- /dev/null +++ b/src/main/input-output/output/state_printer.h @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#ifndef SOLVITAIRE_STATE_PRINT_H +#define SOLVITAIRE_STATE_PRINT_H + +#include + +#include "../../game/game_state.h" + +class state_printer { +public: + static std::ostream& print(std::ostream&, const game_state&); + +private: + static void print_header(std::ostream&, const char*); + static void print_piles(std::ostream&, + const std::vector&, + const game_state&); + static void print_top_of_piles(std::ostream&, + const std::vector&, + const game_state&); + static void print_top_of_pile(std::ostream&, game_state::pile_ref, + const game_state&); +}; + +#endif //SOLVITAIRE_STATE_PRINT_H -- cgit v1.2.1 From 66545273423eae0d8f945f7c3ac5871b6955ff94 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 15:27:47 +0000 Subject: Move card printing into dedicated class --- src/main/input-output/output/state_printer.cpp | 22 +++++++++++++++++++--- src/main/input-output/output/state_printer.h | 1 + 2 files changed, 20 insertions(+), 3 deletions(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index c9b8bc9..88181de 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -60,7 +60,7 @@ void state_printer::print_piles(ostream& stream, // Loops through the current (non-empty) row, and outputs the values for (const game_state::pile_ref pile_r : pile_rs) { if (gs.piles[pile_r].size() > row_idx) { - stream << gs.piles[pile_r][row_idx]; + print_card(stream, gs.piles[pile_r][row_idx]); } else if (row_idx == 0) { stream << "[]"; } @@ -82,7 +82,7 @@ void state_printer::print_top_of_piles(ostream& stream, if (gs.piles[p].empty()) { stream << "[]"; } else { - stream << gs.piles[p].top_card(); + print_card(stream, gs.piles[p].top_card()); } stream << "\t"; } @@ -93,4 +93,20 @@ void state_printer::print_top_of_pile(ostream& stream, const game_state::pile_ref pile_r, const game_state& gs) { print_top_of_piles(stream, {pile_r}, gs); -} \ No newline at end of file +} + +void state_printer::print_card(std::ostream& s, const card c) { + switch(c.get_rank()) { + case 1 : s << "A"; break; + case 11: s << "J"; break; + case 12: s << "Q"; break; + case 13: s << "K"; break; + default: s << int(c.get_rank()); + } + switch(c.get_suit()) { + case card::suit_t::Spades : s << "S"; break; + case card::suit_t::Hearts : s << "H"; break; + case card::suit_t::Clubs : s << "C"; break; + case card::suit_t::Diamonds: s << "D"; break; + } +} diff --git a/src/main/input-output/output/state_printer.h b/src/main/input-output/output/state_printer.h index c918bcd..ae63182 100644 --- a/src/main/input-output/output/state_printer.h +++ b/src/main/input-output/output/state_printer.h @@ -23,6 +23,7 @@ private: const game_state&); static void print_top_of_pile(std::ostream&, game_state::pile_ref, const game_state&); + static void print_card(std::ostream&, card); }; #endif //SOLVITAIRE_STATE_PRINT_H -- cgit v1.2.1 From 6ce3b5a142b3e357eeabde37368915e8ef916f3d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 11:38:36 +0000 Subject: Add json rules for fortunes favor --- src/main/input-output/input/sol_preset_types.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 3c9d295..96cbba9 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -147,32 +147,32 @@ std::map sol_preset_types::mp = { }, { - "canfield", + "fortunes-favor", R"( { "tableau piles": { - "count": 4, - "build policy": "red-black" + "count": 12, + "build policy": "same-suit" }, - "reserve size": 14, - "stock size": 34 + "foundations initial card": true, + "stock size": 36 } )" }, { - "simple-canfield", + "simple-fortunes-favor", R"( { "tableau piles": { - "count": 2, - "build policy": "red-black" + "count": 3, + "build policy": "same-suit" }, - "max rank": 2, - "reserve size": 2, - "stock size": 2 + "max rank": 4, + "foundations initial card": true, + "stock size": 3 } )" -- cgit v1.2.1 From 0bf65cecb1a8b9d3c8269ad579ddd833dd7f3163 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 11:40:01 +0000 Subject: Add rules parsing for (FF) foundations init card --- src/main/input-output/input/json-parsing/rules_parser.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/main/input-output') 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 200dbab..3c372ae 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -155,6 +155,14 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d.HasMember("foundations initial card")) { + if (d["foundations initial card"].IsBool()) { + sr.foundations_init_card = d["foundations initial card"].GetBool(); + } else { + json_helper::json_parse_err("[foundations initial card] must be a boolean"); + } + } + if (d.HasMember("cells")) { if (d["cells"].IsInt()) { sr.cells = static_cast(d["cells"].GetInt()); -- cgit v1.2.1 From 3f93158b539430a7cb08534fff19903acd56ff66 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 11:41:32 +0000 Subject: Add deal parsing for (FF) foundations init card --- .../input/json-parsing/deal_parser.cpp | 33 ++++++++++++++++++++++ .../input-output/input/json-parsing/deal_parser.h | 2 ++ 2 files changed, 35 insertions(+) (limited to 'src/main/input-output') 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 0e89f43..e7f8209 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -27,6 +27,16 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { if (gs.rules.cells) { parse_cells(gs, doc); } + + // Construct stock + if (gs.rules.stock_size > 0) { + parse_stock(gs, doc); + } + + // If the foundations begin with cards in them, fill them + if (gs.rules.foundations_init_card) { + fill_foundations(gs); + } } void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { @@ -80,3 +90,26 @@ void deal_parser::parse_cells(game_state &gs, const Document& doc) { } } } + +void deal_parser::parse_stock(game_state &gs, const Document& doc) { + if (doc.HasMember("stock")) { + const Value &json_stock = doc["stock"]; + assert(json_stock.IsArray()); + + if (gs.rules.stock_size != json_stock.Size()) { + json_helper::json_parse_err("Incorrect stock size"); + } + + for (const Value& json_card : json_stock.GetArray()) { + gs.piles[gs.stock].place(card(json_card.GetString())); + } + } +} + +void deal_parser::fill_foundations(game_state &gs) { + for (uint8_t f_idx = 0; f_idx < 4; f_idx++) { + gs.piles[gs.foundations[f_idx]].place( + card(card::to_suit(f_idx), 1) + ); + } +} 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 9db9e2b..5496eb2 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.h +++ b/src/main/input-output/input/json-parsing/deal_parser.h @@ -22,6 +22,8 @@ private: static void parse_tableau_piles(game_state&, const rapidjson::Document&); static void parse_hole(game_state&, const rapidjson::Document&); static void parse_cells(game_state&, const rapidjson::Document&); + static void parse_stock(game_state&, const rapidjson::Document&); + static void fill_foundations(game_state&); }; #endif //SOLVITAIRE_DEAL_PARSER_H -- cgit v1.2.1 From 88ed4cae6e8174102ee21ce15b03aa40fa032c29 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:44:47 +0000 Subject: Add rules json for flower garden --- src/main/input-output/input/sol_preset_types.cpp | 32 ++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 96cbba9..5e0fcff 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -28,9 +28,10 @@ std::map sol_preset_types::mp = { "max rank": 13, "hole": false, "foundations": true, + "foundations initial card": false, "cells": 0, - "reserve size": 0, - "stock size": 0 + "stock size": 0, + "reserve size": 0 } )" @@ -174,6 +175,33 @@ std::map sol_preset_types::mp = { "foundations initial card": true, "stock size": 3 } +)" + + }, + { + "flower-garden", + + R"( +{ + "tableau piles": { + "count": 6 + }, + "reserve size": 16 +} +)" + + }, + { + "simple-flower-garden", + + R"( +{ + "tableau piles": { + "count": 3 + }, + "max rank": 3, + "reserve size": 3 +} )" } -- cgit v1.2.1 From 6e458408c692f05f8d8033a29c38fe3fdc18723c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:45:57 +0000 Subject: Add rules parsing for flower garden --- src/main/input-output/input/json-parsing/rules_parser.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/main/input-output') 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 3c372ae..cc893fa 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -178,4 +178,12 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { json_helper::json_parse_err("[stock size] must be an integer"); } } + + if (d.HasMember("reserve size")) { + if (d["reserve size"].IsInt()) { + sr.reserve_size = static_cast(d["reserve size"].GetInt()); + } else { + json_helper::json_parse_err("[reserve size] must be an integer"); + } + } } -- cgit v1.2.1 From 75f3ac710731890af853e5c45def1b97d49e0a74 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:46:25 +0000 Subject: Add deal parsing for flower garden --- .../input/json-parsing/deal_parser.cpp | 23 ++++++++++++++++++++++ .../input-output/input/json-parsing/deal_parser.h | 1 + 2 files changed, 24 insertions(+) (limited to 'src/main/input-output') 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 e7f8209..8bcb3b6 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -33,6 +33,11 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { parse_stock(gs, doc); } + // Construct reserve + if (gs.rules.reserve_size > 0) { + parse_reserve(gs, doc); + } + // If the foundations begin with cards in them, fill them if (gs.rules.foundations_init_card) { fill_foundations(gs); @@ -106,6 +111,24 @@ void deal_parser::parse_stock(game_state &gs, const Document& doc) { } } +void deal_parser::parse_reserve(game_state &gs, const Document& doc) { + assert(doc.HasMember("reserve")); + const Value& json_reserve_piles = doc["reserve"]; + assert(json_reserve_piles.IsArray()); + + if (gs.reserve.size() != gs.rules.reserve_size) { + json_helper::json_parse_err("Incorrect reserve size"); + } + + for (auto p = std::make_pair(begin(json_reserve_piles.GetArray()), begin(gs.reserve)); + p.second != end(gs.reserve); + ++p.first, ++p.second) { + + assert(p.first->IsString()); + gs.piles[*p.second].place(card(p.first->GetString())); + } +} + void deal_parser::fill_foundations(game_state &gs) { for (uint8_t f_idx = 0; f_idx < 4; f_idx++) { gs.piles[gs.foundations[f_idx]].place( 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 5496eb2..ecb596c 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 @@ private: static void parse_hole(game_state&, const rapidjson::Document&); static void parse_cells(game_state&, const rapidjson::Document&); static void parse_stock(game_state&, const rapidjson::Document&); + static void parse_reserve(game_state&, const rapidjson::Document&); static void fill_foundations(game_state&); }; -- cgit v1.2.1 From 2ac80451bc8a0d7a6830e1d9274f33138844b386 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:47:41 +0000 Subject: Add state printing for regular reserve --- src/main/input-output/output/state_printer.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/main/input-output') diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index 88181de..1828390 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -19,6 +19,10 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { state_printer::print_header(stream, "Tableau Piles"); state_printer::print_piles(stream, gs.tableau_piles, gs); } + if (gs.rules.reserve_size > 0) { + state_printer::print_header(stream, "Reserve"); + state_printer::print_piles(stream, gs.reserve, gs); + } if (gs.rules.stock_size > 0) { state_printer::print_header(stream, "Stock | Waste"); state_printer::print_piles(stream, {gs.stock, gs.waste}, gs); -- cgit v1.2.1 From 6f56c50d2678f1640f8c15ff5cfcff03af3f52b2 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 15:53:30 +0000 Subject: Add json rules for canfield --- src/main/input-output/input/sol_preset_types.cpp | 36 +++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 5e0fcff..8bff279 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -31,7 +31,8 @@ std::map sol_preset_types::mp = { "foundations initial card": false, "cells": 0, "stock size": 0, - "reserve size": 0 + "reserve size": 0, + "reserve stacked": false } )" @@ -202,6 +203,39 @@ std::map sol_preset_types::mp = { "max rank": 3, "reserve size": 3 } +)" + + }, + { + "canfield", + + R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black" + }, + "stock size": 34, + "reserve size": 14, + "reserve stacked": true +} +)" + + }, + { + "simple-canfield", + + R"( +{ + "tableau piles": { + "count": 3, + "build policy": "red-black" + }, + "max rank": 3, + "stock size": 1, + "reserve size": 2, + "reserve stacked": true +} )" } -- cgit v1.2.1 From e1eb16767fee2241aa27896332a01ed5987fdf8b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 15:54:31 +0000 Subject: Add rules parsing for stacked reserve (canfield) --- src/main/input-output/input/json-parsing/rules_parser.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/main/input-output') 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 cc893fa..f997ba3 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -186,4 +186,12 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { json_helper::json_parse_err("[reserve size] must be an integer"); } } + + if (d.HasMember("reserve stacked")) { + if (d["reserve stacked"].IsBool()) { + sr.reserve_stacked = d["reserve stacked"].GetBool(); + } else { + json_helper::json_parse_err("[reserve stacked] must be a boolean"); + } + } } -- cgit v1.2.1 From dc5a834c0d2d2a25f3023f812f8d85376855f0be Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 15:55:59 +0000 Subject: Add deal parser for stacked reserve (canfield) --- .../input/json-parsing/deal_parser.cpp | 26 +++++++++++++++------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'src/main/input-output') 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 8bcb3b6..6f0694d 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -116,16 +116,26 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) { const Value& json_reserve_piles = doc["reserve"]; assert(json_reserve_piles.IsArray()); - if (gs.reserve.size() != gs.rules.reserve_size) { - json_helper::json_parse_err("Incorrect reserve size"); + // We treat a regular reserve like multiple single-card piles, + // but a stacked reserve as a single multiple-card pile + if (gs.rules.reserve_stacked) { + if (json_reserve_piles.Size() != gs.rules.reserve_size + || json_reserve_piles.Size() != gs.rules.reserve_size) { + json_helper::json_parse_err("Incorrect reserve size"); + } + } else { + if (gs.reserve.size() != gs.rules.reserve_size + || json_reserve_piles.Size() != gs.rules.reserve_size) { + json_helper::json_parse_err("Incorrect reserve size"); + } } - for (auto p = std::make_pair(begin(json_reserve_piles.GetArray()), begin(gs.reserve)); - p.second != end(gs.reserve); - ++p.first, ++p.second) { - - assert(p.first->IsString()); - gs.piles[*p.second].place(card(p.first->GetString())); + const auto& json_card_arr = json_reserve_piles.GetArray(); + for (game_state::pile_ref i = 0; i < json_card_arr.Size(); i++) { + assert(json_card_arr[i].IsString()); + game_state::pile_ref pr = gs.reserve[0]; + if (!gs.rules.reserve_stacked) pr += i; + gs.piles[pr].place(card(json_card_arr[i].GetString())); } } -- cgit v1.2.1 From 748dceab129dc57aa7a3e58ef538b9701398846c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 15:56:46 +0000 Subject: Add state printing for stacked reserve (canfield) --- src/main/input-output/output/state_printer.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index 1828390..2c23496 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -20,7 +20,11 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { state_printer::print_piles(stream, gs.tableau_piles, gs); } if (gs.rules.reserve_size > 0) { - state_printer::print_header(stream, "Reserve"); + if (gs.rules.reserve_stacked) { + state_printer::print_header(stream, "Reserve (Stacked)"); + } else { + state_printer::print_header(stream, "Reserve"); + } state_printer::print_piles(stream, gs.reserve, gs); } if (gs.rules.stock_size > 0) { -- cgit v1.2.1 From 5886a4e8be7fb5e15617a8e58f207ec008ca93e2 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 18:00:41 +0000 Subject: Add json rules for somerset --- src/main/input-output/input/sol_preset_types.cpp | 32 +++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 8bff279..89a5842 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -23,7 +23,8 @@ std::map sol_preset_types::mp = { "count": 8, "build order": "descending", "build policy": "any-suit", - "spaces policy": "any" + "spaces policy": "any", + "diagonal deal": true }, "max rank": 13, "hole": false, @@ -236,6 +237,35 @@ std::map sol_preset_types::mp = { "reserve size": 2, "reserve stacked": true } +)" + + }, + { + "somerset", + + R"( +{ + "tableau piles": { + "count": 10, + "build policy": "red-black", + "diagonal deal": true + } +} +)" + + }, + { + "simple-somerset", + + R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black", + "diagonal deal": true + }, + "max rank": 2 +} )" } -- cgit v1.2.1 From f481f8bb2f6ba7c34e36fb3a439500ebf0b42b35 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 18:03:09 +0000 Subject: Add diagonal deal to rules parser (somerset) --- src/main/input-output/input/json-parsing/rules_parser.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/main/input-output') 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 f997ba3..9d20a5a 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -163,6 +163,14 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d.HasMember("diagonal deal")) { + if (d["diagonal deal"].IsBool()) { + sr.diagonal_deal = d["diagonal deal"].GetBool(); + } else { + json_helper::json_parse_err("[diagonal deal] must be a boolean"); + } + } + if (d.HasMember("cells")) { if (d["cells"].IsInt()) { sr.cells = static_cast(d["cells"].GetInt()); -- cgit v1.2.1 From 22b347e97a9da26a2cddd7b5a04e1a7a62069d23 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 8 Jan 2018 18:46:11 +0000 Subject: Add json rules for alpha star --- src/main/input-output/input/sol_preset_types.cpp | 34 +++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 89a5842..6805f8e 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -24,7 +24,8 @@ std::map sol_preset_types::mp = { "build order": "descending", "build policy": "any-suit", "spaces policy": "any", - "diagonal deal": true + "diagonal deal": true, + "move built group": false }, "max rank": 13, "hole": false, @@ -266,6 +267,37 @@ std::map sol_preset_types::mp = { }, "max rank": 2 } +)" + + }, + { + "alpha-star", + + R"( +{ + "tableau piles": { + "count": 12, + "build policy": "same-suit", + "move built group": true + }, + "foundations initial card": true +} +)" + + }, + { + "simple-alpha-star", + + R"( +{ + "tableau piles": { + "count": 3, + "build policy": "same-suit", + "move built group": true + }, + "max rank": 5, + "foundations initial card": true +} )" } -- cgit v1.2.1 From cc5cb4243d2015f218ed5258df47ec678175a486 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 8 Jan 2018 18:59:44 +0000 Subject: Add rules parsing for built pile (AS) --- .../input/json-parsing/rules_parser.cpp | 24 ++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'src/main/input-output') 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 9d20a5a..9ec2339 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -126,6 +126,22 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d["tableau piles"].HasMember("move built group")) { + if (d["tableau piles"]["move built group"].IsBool()) { + sr.move_built_group = d["tableau piles"]["move built group"].GetBool(); + } else { + json_helper::json_parse_err("[tableau piles][move built group] must be a boolean"); + } + } + + if (d["tableau piles"].HasMember("diagonal deal")) { + if (d["tableau piles"]["diagonal deal"].IsBool()) { + sr.diagonal_deal = d["tableau piles"]["diagonal deal"].GetBool(); + } else { + json_helper::json_parse_err("[tableau piles][diagonal deal] must be a boolean"); + } + } + } else { json_helper::json_parse_err("[tableau piles] must be an object"); } @@ -163,14 +179,6 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } - if (d.HasMember("diagonal deal")) { - if (d["diagonal deal"].IsBool()) { - sr.diagonal_deal = d["diagonal deal"].GetBool(); - } else { - json_helper::json_parse_err("[diagonal deal] must be a boolean"); - } - } - if (d.HasMember("cells")) { if (d["cells"].IsInt()) { sr.cells = static_cast(d["cells"].GetInt()); -- cgit v1.2.1 From 067152f57ced18c5f706144909345125754e8664 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 8 Jan 2018 19:02:36 +0000 Subject: Add game logic for built pile (AA) and refactor --- src/main/input-output/output/state_printer.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index 2c23496..cf9f09e 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -51,7 +51,7 @@ void state_printer::print_piles(ostream& stream, const vector& pile_rs, const game_state& gs) { bool empty_row = false; - vector::size_type row_idx = 0; + pile::size_type row_idx = 0; // Loops through the rows in each pile, starting from the bottom, until // one is empty @@ -67,8 +67,9 @@ void state_printer::print_piles(ostream& stream, if (!empty_row || row_idx == 0) { // Loops through the current (non-empty) row, and outputs the values for (const game_state::pile_ref pile_r : pile_rs) { - if (gs.piles[pile_r].size() > row_idx) { - print_card(stream, gs.piles[pile_r][row_idx]); + const pile& p = gs.piles[pile_r]; + if (p.size() > row_idx) { + print_card(stream, p[p.size() - 1 - row_idx]); } else if (row_idx == 0) { stream << "[]"; } -- cgit v1.2.1 From ef291b15a21f2f6cfa98b2c4f2572de5a61a2fa9 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 9 Jan 2018 08:51:11 +0000 Subject: Add json rules for spider --- src/main/input-output/input/sol_preset_types.cpp | 35 +++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 6805f8e..8f0293f 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -24,15 +24,17 @@ std::map sol_preset_types::mp = { "build order": "descending", "build policy": "any-suit", "spaces policy": "any", - "diagonal deal": true, + "diagonal deal": false, "move built group": false }, "max rank": 13, + "two decks": false, "hole": false, "foundations": true, "foundations initial card": false, "cells": 0, "stock size": 0, + "stock deal type": "waste", "reserve size": 0, "reserve stacked": false } @@ -298,6 +300,37 @@ std::map sol_preset_types::mp = { "max rank": 5, "foundations initial card": true } +)" + + }, + { + "spider", + + R"( +{ + "tableau piles": { + "count": 10 + }, + "two decks": true, + "stock size": 60, + "stock deal type": "tableau piles" +} +)" + + }, + { + "simple-spider", + + R"( +{ + "tableau piles": { + "count": 4 + }, + "two decks": true, + "max rank": 3, + "stock size": 12, + "stock deal type": "tableau piles" +} )" } -- cgit v1.2.1 From 9efeb027c72460e7d5b6e320819788b7f38d8035 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 9 Jan 2018 08:52:22 +0000 Subject: Add rules parsing for stock-tableau deal & 2 deck --- .../input/json-parsing/rules_parser.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/main/input-output') 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 9ec2339..948eb39 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -19,6 +19,7 @@ using namespace rapidjson; typedef sol_rules::build_order ord; typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; +typedef sol_rules::stock_deal_type sdt; const sol_rules rules_parser::from_file(const string rules_file) { sol_rules sr = get_default(); @@ -155,6 +156,14 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d.HasMember("two decks")) { + if (d["two decks"].IsBool()) { + sr.two_decks = d["two decks"].GetBool(); + } else { + json_helper::json_parse_err("[two decks] must be a boolean"); + } + } + if (d.HasMember("hole")) { if (d["hole"].IsBool()) { sr.hole = d["hole"].GetBool(); @@ -195,6 +204,18 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d.HasMember("stock deal type")) { + if (d["stock deal type"].IsString()) { + if(string(d["stock deal type"].GetString()) == "waste") { + sr.stock_deal_t = sdt::WASTE; + } else { + sr.stock_deal_t = sdt::TABLEAU_PILES; + } + } else { + json_helper::json_parse_err("[stock deal type] must be a string"); + } + } + if (d.HasMember("reserve size")) { if (d["reserve size"].IsInt()) { sr.reserve_size = static_cast(d["reserve size"].GetInt()); -- cgit v1.2.1 From 077c56fbee736d9c597d9735229a1f625be46fe0 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 9 Jan 2018 08:56:01 +0000 Subject: Add state printing for stock-tableau deal --- src/main/input-output/output/state_printer.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index cf9f09e..bfb6c7d 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -6,6 +6,8 @@ using namespace std; +typedef sol_rules::stock_deal_type sdt; + ostream& state_printer::print(ostream& stream, const game_state& gs) { if (gs.rules.foundations) { state_printer::print_header(stream, "Foundations"); @@ -28,8 +30,13 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { state_printer::print_piles(stream, gs.reserve, gs); } if (gs.rules.stock_size > 0) { - state_printer::print_header(stream, "Stock | Waste"); - state_printer::print_piles(stream, {gs.stock, gs.waste}, gs); + if (gs.rules.stock_deal_t == sdt::WASTE) { + state_printer::print_header(stream, "Stock | Waste"); + state_printer::print_piles(stream, {gs.stock, gs.waste}, gs); + } else { + state_printer::print_header(stream, "Stock"); + state_printer::print_piles(stream, {gs.stock}, gs); + } } if (gs.rules.hole) { state_printer::print_header(stream, "Hole Card"); -- cgit v1.2.1