aboutsummaryrefslogtreecommitdiff
path: root/solvitaire/src/main/input-output
diff options
context:
space:
mode:
authorTom Harley2019-01-27 15:37:06 +0000
committerTom Harley2019-01-27 15:37:06 +0000
commita996283af7c21baaff0e114d58c34db820902c6f (patch)
tree2fe5b6f7a800d4a7b8a65ee345f8c98aa138e36b /solvitaire/src/main/input-output
parentSplit code into 3 files (diff)
downloadmetasol-a996283af7c21baaff0e114d58c34db820902c6f.tar.gz
metasol-a996283af7c21baaff0e114d58c34db820902c6f.zip

Reorganise repo

All solvitaire content has been shoved into a subdirectory to tidy up the root of the project. This also means solvitaire is treated similarly to other dependencies of the project, and allows other dependencies to be added easily.

There’s also a lot of other work done here, such as usign a thread pool library to manage the running of the voters.

Diffstat (limited to 'solvitaire/src/main/input-output')
-rw-r--r--solvitaire/src/main/input-output/input/command_line_helper.cpp335
-rw-r--r--solvitaire/src/main/input-output/input/command_line_helper.h67
-rw-r--r--solvitaire/src/main/input-output/input/json-parsing/deal_parser.cpp290
-rw-r--r--solvitaire/src/main/input-output/input/json-parsing/deal_parser.h33
-rw-r--r--solvitaire/src/main/input-output/input/json-parsing/json_helper.cpp110
-rw-r--r--solvitaire/src/main/input-output/input/json-parsing/json_helper.h31
-rw-r--r--solvitaire/src/main/input-output/input/json-parsing/rules_parser.cpp746
-rw-r--r--solvitaire/src/main/input-output/input/json-parsing/rules_parser.h22
-rw-r--r--solvitaire/src/main/input-output/input/sol_preset_types.cpp1630
-rw-r--r--solvitaire/src/main/input-output/input/sol_preset_types.h22
-rw-r--r--solvitaire/src/main/input-output/output/log_helper.h19
-rw-r--r--solvitaire/src/main/input-output/output/state_printer.cpp194
-rw-r--r--solvitaire/src/main/input-output/output/state_printer.h35
13 files changed, 3534 insertions, 0 deletions
diff --git a/solvitaire/src/main/input-output/input/command_line_helper.cpp b/solvitaire/src/main/input-output/input/command_line_helper.cpp
new file mode 100644
index 0000000..ae0f9c4
--- /dev/null
+++ b/solvitaire/src/main/input-output/input/command_line_helper.cpp
@@ -0,0 +1,335 @@
+//
+// Created by thecharlesblake on 10/16/17.
+//
+
+#include <tuple>
+#include <vector>
+
+#include <boost/program_options.hpp>
+
+#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<string>(),
+ "specify the type of the solitaire game to be solved from the list "
+ "of preset games. Must supply either this 'type' option, "
+ "or the 'custom-rules' option")
+ ("available-game-types", "outputs a list of the different preset "
+ "game types that can be solved")
+ ("describe-game-rules", po::value<string>(),
+ "outputs the JSON that describes the rules of the supplied "
+ "preset game type")
+ ("custom-rules", po::value<string>(),
+ "the path to a JSON file describing the rules of the solitaire "
+ "to be solved. Must supply either 'type' or 'custom-rules' option")
+ ("random", po::value<int>(), "create and solve a random solitaire "
+ "deal based on a seed. Must supply either 'random',"
+ "'solvability', 'benchmark' or list of deals to be solved.")
+ ("classify", "outputs a simple 'solvable/not solvable' "
+ "classification")
+ ("cache-capacity", po::value<uint64_t>(), "sets an upper bound on the number of states allowed in "
+ "the cache")
+ ("solvability", po::value<int>(), "calculates the solvability "
+ "percentage of the supplied solitaire game, given a limit for the number of seeds. Must supply "
+ "either 'random', 'benchmark', 'solvability' or list of deals to be solved.")
+ ("timeout", po::value<uint64_t>(), "adds a timeout to searches")
+ ("resume", po::value<vector<int>>()->multitoken(), "resumes the solvability percentage calculation from a "
+ "previous run. Must be supplied with the solvability option. "
+ "Syntax: [sol unsol intract in-progress-1 in-progress-2 ...]")
+ ("cores", po::value<uint>(), "the number of cores for the solvability percentages to be run across. "
+ "Must be supplied with the solvability option.")
+ ("streamliners", po::value<string>(),
+ "Applies streamliners to the search. Options include 'none', 'both', 'suit-symmetry',"
+ " 'auto-foundations', and 'smart-solvability'. Defaults to 'none', unless '--solvability' is"
+ " also supplied, in which case defaults to 'smart-solvability'. 'smart-solvability' mode"
+ " 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.")
+ ("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()
+ ("input-files", po::value<vector<string>>(),
+ "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);
+
+ available_game_types = (vm.count("available-game-types") != 0);
+
+ if (vm.count("describe-game-rules")) {
+ describe_game_rules = vm["describe-game-rules"].as<string>();
+ }
+
+ 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>>();
+ }
+
+ if (vm.count("type")) {
+ solitaire_type = vm["type"].as<string>();
+ }
+
+ if (vm.count("custom-rules")) {
+ rules_file = vm["custom-rules"].as<string>();
+ }
+
+ if (vm.count("random")) {
+ random_deal = vm["random"].as<int>();
+ } else {
+ random_deal = -1;
+ }
+
+ if (vm.count("cache-capacity")) {
+ cache_capacity = vm["cache-capacity"].as<uint64_t>();
+ } else {
+ cache_capacity = 100000000; // One hundred-million
+ }
+
+ if (vm.count("solvability")) {
+ solvability = vm["solvability"].as<int>();
+ } else {
+ solvability = -1;
+ }
+
+ if (vm.count("timeout")) {
+ timeout = vm["timeout"].as<uint64_t>();
+ } else {
+ timeout = 604800000; // 1 week in milliseconds
+ }
+
+ if (vm.count("cores")) {
+ cores = vm["cores"].as<uint>();
+ } else {
+ cores = 1;
+ }
+
+ if (vm.count("resume")) {
+ resume = vm["resume"].as<vector<int>>();
+ } else {
+ resume = {0, 0, 0};
+ for (uint i = 0; i < cores; i++) resume.push_back(i);
+ }
+
+ if (vm.count("streamliners")) {
+ auto& s = vm["streamliners"].as<string>();
+
+ if (s == "auto-foundations" ) streamliners = streamliner_opt::AUTO_FOUNDATIONS;
+ else if (s == "suit-symmetry") streamliners = streamliner_opt::SUIT_SYMMETRY;
+ else if (s == "both") streamliners = streamliner_opt::BOTH;
+ else if (s == "smart-solvability") streamliners = streamliner_opt::SMART;
+ else if (s == "none") streamliners = streamliner_opt::NONE;
+ else {
+ print_streamliner_error(s);
+ return false;
+ }
+ } else {
+ if (solvability == -1) streamliners = streamliner_opt::NONE;
+ else streamliners = streamliner_opt::SMART;
+ }
+
+ benchmark = (vm.count("benchmark") != 0);
+
+ // Handle logic error scenarios
+ return assess_errors();
+}
+
+bool command_line_helper::assess_errors() {
+ if (help) {
+ print_help();
+ return false;
+ }
+
+ if (available_game_types || !describe_game_rules.empty()) return true;
+
+ if (solvability > 0 && resume.size() != 3 + cores) {
+ print_resume_error();
+ return false;
+ }
+
+ // The user must either supply input files, a random seed, or ask for the
+ // solvability percentage, or benchmark
+ int opt_count = (random_deal != -1) + !input_files.empty() + (solvability > 0) + benchmark;
+
+ if (opt_count > 1) {
+ print_too_many_opts_error();
+ return false;
+ } else if (opt_count < 1) {
+ print_no_opts_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() {
+ cerr << "Usage: solvitaire [options] input-file1 input-file2 ...\n"
+ << main_options << "\n";
+}
+
+void command_line_helper::print_no_opts_error() {
+ LOG_ERROR ("Error: User must supply input file(s), the '--random' "
+ "option, the 'benchmark' option, or the '--solvability' 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_too_many_opts_error() {
+ LOG_ERROR ("Error: User must supply input file(s), the '--random' option, the 'benchmark' option,, "
+ "or the '--solvability' option, not multiple");
+ print_help();
+}
+
+void command_line_helper::print_resume_error() {
+ LOG_ERROR ("Error: Resume contains " << resume.size() << " options, and should contain " << cores + 3);
+ print_help();
+}
+
+void command_line_helper::print_streamliner_error(const string& str) {
+ LOG_ERROR ("Error: invalid streamliner: " + str + ".\nAvailable options are: 'none', 'both', 'suit-symmetry',"
+ " 'auto-foundations', and 'smart-solvability'");
+}
+
+const vector<string> 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_classify() {
+ return classify;
+}
+
+bool command_line_helper::get_deal_only() {
+ return deal_only;
+}
+
+uint64_t command_line_helper::get_cache_capacity() {
+ return cache_capacity;
+}
+
+int command_line_helper::get_solvability() {
+ return solvability;
+}
+
+uint64_t command_line_helper::get_timeout() {
+ return timeout;
+}
+
+vector<int> command_line_helper::get_resume() {
+ return resume;
+}
+
+uint command_line_helper::get_cores() {
+ return cores;
+}
+
+bool command_line_helper::get_available_game_types() {
+ return available_game_types;
+}
+
+string command_line_helper::get_describe_game_rules() {
+ return describe_game_rules;
+}
+
+bool command_line_helper::get_benchmark() {
+ return benchmark;
+}
+
+command_line_helper::streamliner_opt command_line_helper::get_streamliners() {
+ return streamliners;
+}
+
+game_state::streamliner_options command_line_helper::get_streamliners_game_state() {
+ return convert_streamliners(streamliners);
+}
+
+game_state::streamliner_options command_line_helper::convert_streamliners(streamliner_opt so) {
+ switch (so){
+ case command_line_helper::streamliner_opt::NONE: return game_state::streamliner_options::NONE;
+ case command_line_helper::streamliner_opt::AUTO_FOUNDATIONS: return game_state::streamliner_options::AUTO_FOUNDATIONS;
+ case command_line_helper::streamliner_opt::SUIT_SYMMETRY: return game_state::streamliner_options::SUIT_SYMMETRY;
+ case command_line_helper::streamliner_opt::BOTH: return game_state::streamliner_options::BOTH;
+ case command_line_helper::streamliner_opt::SMART:
+ default:
+ assert(false);
+ throw runtime_error("attempted to convert smart streamliner mode to single streamliner");
+ return game_state::streamliner_options::NONE;;
+ }
+}
diff --git a/solvitaire/src/main/input-output/input/command_line_helper.h b/solvitaire/src/main/input-output/input/command_line_helper.h
new file mode 100644
index 0000000..f2a9d74
--- /dev/null
+++ b/solvitaire/src/main/input-output/input/command_line_helper.h
@@ -0,0 +1,67 @@
+//
+// Created by thecharlesblake on 10/16/17.
+//
+
+#ifndef SOLVITAIRE_COMMAND_LINE_HELPER_H
+#define SOLVITAIRE_COMMAND_LINE_HELPER_H
+
+#include <boost/program_options.hpp>
+#include "../../game/search-state/game_state.h"
+
+class command_line_helper {
+public:
+ command_line_helper();
+ enum class streamliner_opt {NONE, AUTO_FOUNDATIONS, SUIT_SYMMETRY, BOTH, SMART};
+
+ bool parse(int argc, const char* argv[]);
+ const std::vector<std::string> get_input_files();
+ const std::string get_solitaire_type();
+ 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();
+ bool get_benchmark();
+ streamliner_opt get_streamliners();
+ game_state::streamliner_options get_streamliners_game_state();
+ std::vector<int> get_resume();
+ uint64_t get_cache_capacity();
+ std::string get_describe_game_rules();
+ uint64_t get_timeout();
+ static game_state::streamliner_options convert_streamliners(streamliner_opt);
+
+private:
+ bool assess_errors();
+ bool assess_sol_type();
+ void print_help();
+ void print_sol_type_rules_error();
+ void print_no_opts_error();
+ void print_too_many_opts_error();
+ void print_resume_error();
+ void print_streamliner_error(const std::string&);
+
+ boost::program_options::options_description cmdline_options;
+ boost::program_options::options_description main_options;
+ boost::program_options::positional_options_description p;
+
+ std::vector<std::string> input_files;
+ std::string solitaire_type;
+ std::string rules_file;
+ std::string describe_game_rules;
+ int random_deal;
+ bool help;
+ bool classify;
+ bool deal_only;
+ int solvability;
+ std::vector<int> resume;
+ uint cores;
+ bool available_game_types;
+ bool benchmark;
+ streamliner_opt streamliners;
+ uint64_t cache_capacity;
+ uint64_t timeout;
+};
+
+#endif //SOLVITAIRE_COMMAND_LINE_HELPER_H
diff --git a/solvitaire/src/main/input-output/input/json-parsing/deal_parser.cpp b/solvitaire/src/main/input-output/input/json-parsing/deal_parser.cpp
new file mode 100644
index 0000000..8b8a36a
--- /dev/null
+++ b/solvitaire/src/main/input-output/input/json-parsing/deal_parser.cpp
@@ -0,0 +1,290 @@
+//
+// Created by thecharlesblake on 10/21/17.
+//
+
+#include "deal_parser.h"
+#include "../../../game/search-state/game_state.h"
+#include "json_helper.h"
+#include "../../../game/sol_rules.h"
+
+using namespace std;
+using namespace rapidjson;
+
+typedef sol_rules::build_policy pol;
+typedef sol_rules::stock_deal_type sdt;
+typedef sol_rules::face_up_policy fu;
+typedef sol_rules::foundations_init_type fit;
+
+void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) {
+ // There are two stages to reading in the supplied deal. It is both put
+ // through the deal schema validator, and also parsed into
+ // the game state object (with some further checking done). The parsing is
+ // done first because the errors it detects tend to give clearer error
+ // messages than the schema validator
+
+ // 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);
+ }
+
+ // Construct stock
+ if (gs.rules.stock_size > 0) {
+ parse_stock(gs, doc);
+ }
+
+ // Construct waste
+ if (gs.rules.stock_deal_t == sdt::WASTE) {
+ parse_waste(gs, doc);
+ }
+
+ // Construct reserve
+ if (gs.rules.reserve_size > 0) {
+ parse_reserve(gs, doc);
+ }
+
+ // Construct sequences
+ if (gs.rules.sequence_count > 0) {
+ parse_sequences(gs, doc);
+ }
+
+ // Construct accordion
+ if (gs.rules.accordion_size > 0) {
+ parse_accordion(gs, doc);
+ }
+
+ // If the foundations begin with cards in them, fill them
+ if (gs.rules.foundations_present) {
+ bool supplied_foundations = parse_foundations(gs, doc);
+
+ if (!supplied_foundations && gs.rules.foundations_init_cards == fit::ALL) {
+ fill_foundations(gs);
+ }
+ }
+
+ Document d;
+ d.Parse(deal_schema_json().c_str());
+ assert(!d.HasParseError());
+ SchemaDocument sd(d);
+ SchemaValidator validator(sd);
+ if(!doc.Accept(validator)) {
+ throw runtime_error(json_helper::schema_err_str(validator));
+ }
+}
+
+string deal_parser::deal_schema_json() {
+ string schema_json = R"(
+{
+ "$schema": "http://json-schema.org/draft-04/schema#",
+ "description": "JSON Schema representing a solitaire game state to solve",
+
+ "definitions": {
+ "card": {"type": "string", "pattern": "^(([0-9]|1[0-3]|a|A|j|J|q|Q|k|K)(c|C|d|D|h|H|s|S))$"},
+ "cardarray": {"type": "array", "items": {"$ref": "#/definitions/card"}},
+ "cardarraywithempty": {
+ "type": "array",
+ "items": {
+ "oneOf":[
+ {"$ref": "#/definitions/card"},
+ {"type": "string","enum": [""]}
+ ]
+ }
+ }
+ },
+
+ "type": "object", "properties": {
+
+ "tableau piles": {
+ "type": "array", "items": {"$ref": "#/definitions/cardarray"}
+ },
+ "foundations": {"$ref": "#/definitions/cardarray"},
+ "sequences": {
+ "type": "array", "items": {"$ref": "#/definitions/cardarraywithempty"}
+ },
+ "hole": {"$ref": "#/definitions/card"},
+ "stock": {"$ref": "#/definitions/cardarray"},
+ "waste": {"$ref": "#/definitions/cardarray"},
+ "reserve": {"$ref": "#/definitions/cardarray"},
+ "accordion": {"$ref": "#/definitions/cardarray"}
+
+ }, "additionalProperties": false
+}
+)";
+ return schema_json;
+}
+
+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 (json_tab_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.original_tableau_piles));
+ p.second != end(gs.original_tableau_piles);
+ ++p.first, ++p.second) {
+
+ for (auto& json_card : p.first->GetArray()) {
+ assert(json_card.IsString());
+ gs.place_card(*p.second, card(json_card.GetString(), gs.rules.face_up != fu::ALL));
+ }
+ }
+}
+
+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.place_card(gs.hole, 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 (json_cell_arr.Size() != gs.rules.cells) {
+ json_helper::json_parse_err("Incorrect number of cells");
+ }
+
+ for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.original_cells));
+ p.second != end(gs.original_cells);
+ ++p.first, ++p.second) {
+
+ auto json_card = p.first;
+ assert(json_card->IsString());
+ gs.place_card(*p.second, card(json_card->GetString()));
+ }
+ }
+}
+
+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());
+
+ for (const Value& json_card : json_stock.GetArray()) {
+ assert(json_card.IsString());
+ gs.place_card(gs.stock, card(json_card.GetString()));
+ }
+ }
+}
+
+void deal_parser::parse_waste(game_state &gs, const Document& doc) {
+ if (doc.HasMember("waste")) {
+ const Value &json_waste = doc["waste"];
+ assert(json_waste.IsArray());
+
+ for (const Value& json_card : json_waste.GetArray()) {
+ assert(json_card.IsString());
+ gs.place_card(gs.waste, card(json_card.GetString()));
+ }
+ }
+}
+
+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());
+
+ const auto& json_card_arr = json_reserve_piles.GetArray();
+
+ // We treat a regular reserve like multiple single-card piles,
+ // but a stacked reserve as a single multiple-card pile
+ for (pile::ref i = 0; i < json_card_arr.Size(); i++) {
+ assert(json_card_arr[i].IsString());
+ pile::ref pr = gs.original_reserve[0];
+ if (!gs.rules.reserve_stacked) pr += i;
+ gs.place_card(pr, card(json_card_arr[i].GetString()));
+ }
+}
+
+void deal_parser::parse_sequences(game_state& gs, const rapidjson::Document& doc) {
+ assert(doc.HasMember("sequences"));
+ const Value& json_seqs = doc["sequences"];
+ assert(json_seqs.IsArray());
+
+ if (json_seqs.Size() != gs.rules.sequence_count ) {
+ json_helper::json_parse_err("Incorrect number of sequences");
+ }
+
+ for (auto p = std::make_pair(begin(json_seqs.GetArray()), begin(gs.sequences));
+ p.second != end(gs.sequences);
+ ++p.first, ++p.second) {
+
+ for (auto& json_card : p.first->GetArray()) {
+ assert(json_card.IsString());
+ string card_str = json_card.GetString();
+ card c = card_str.empty() ? "AS" : card(json_card.GetString());
+ gs.place_card(*p.second, c);
+ }
+ }
+}
+
+void deal_parser::parse_accordion(game_state &gs, const Document& doc) {
+ assert(doc.HasMember("accordion"));
+ const Value& json_accordion_piles = doc["accordion"];
+ assert(json_accordion_piles.IsArray());
+
+ const auto& json_card_arr = json_accordion_piles.GetArray();
+
+ // We treat a regular reserve like multiple single-card piles,
+ // but a stacked reserve as a single multiple-card pile
+ if (json_card_arr.Size() != gs.rules.accordion_size) {
+ json_helper::json_parse_err("Incorrect accordion size");
+ }
+
+ auto acc_it = begin(gs.accordion);
+ for (pile::ref i = 0; i < json_card_arr.Size(); i++, acc_it++) {
+ assert(json_card_arr[i].IsString());
+ pile::ref pr = *acc_it;
+ gs.place_card(pr, card(json_card_arr[i].GetString()));
+ }
+}
+
+bool deal_parser::parse_foundations(game_state &gs, const rapidjson::Document& doc) {
+ if (!doc.HasMember("foundations")) return false;
+
+ const Value& json_foundations = doc["foundations"];
+ assert(json_foundations.IsArray());
+
+ card c;
+ for (auto j = begin(json_foundations.GetArray()); j != end(json_foundations.GetArray()); j++) {
+ assert(j->IsString());
+ c = card(j->GetString());
+ gs.place_card(gs.foundations[c.get_suit()], c);
+ }
+
+ // If the game uses a random base for foundations, assume that the first card in the first foundation is that base
+ if (!gs.rules.foundations_base) {
+ auto& first_found = gs.piles[gs.foundations[c.get_suit()]];
+ gs.foundations_base = first_found[first_found.size() - 1].get_rank();
+ }
+
+ return true;
+}
+
+void deal_parser::fill_foundations(game_state &gs) {
+ auto foundations_count = uint8_t(4 * (gs.rules.two_decks ? 2:1));
+ assert(gs.foundations.size() == foundations_count);
+
+ for (uint8_t f_idx = 0; f_idx < foundations_count; f_idx++) {
+ gs.place_card(gs.foundations[f_idx], card(f_idx % uint8_t(4), 1));
+ }
+}
diff --git a/solvitaire/src/main/input-output/input/json-parsing/deal_parser.h b/solvitaire/src/main/input-output/input/json-parsing/deal_parser.h
new file mode 100644
index 0000000..36f58d5
--- /dev/null
+++ b/solvitaire/src/main/input-output/input/json-parsing/deal_parser.h
@@ -0,0 +1,33 @@
+//
+// Created by thecharlesblake on 10/21/17.
+//
+
+#ifndef SOLVITAIRE_DEAL_PARSER_H
+#define SOLVITAIRE_DEAL_PARSER_H
+
+#include <vector>
+
+#include <boost/optional.hpp>
+
+#include "../../../../../lib/rapidjson/document.h"
+#include "../../../../../lib/rapidjson/schema.h"
+
+class game_state;
+
+class deal_parser {
+public:
+ static void parse(game_state&, const rapidjson::Document&);
+ 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 parse_waste(game_state&, const rapidjson::Document&);
+ static void parse_reserve(game_state&, const rapidjson::Document&);
+ static void parse_sequences(game_state&, const rapidjson::Document&);
+ static void parse_accordion(game_state&, const rapidjson::Document&);
+ static bool parse_foundations(game_state&, const rapidjson::Document&);
+ static void fill_foundations(game_state&);
+ static std::string deal_schema_json();
+};
+
+#endif //SOLVITAIRE_DEAL_PARSER_H
diff --git a/solvitaire/src/main/input-output/input/json-parsing/json_helper.cpp b/solvitaire/src/main/input-output/input/json-parsing/json_helper.cpp
new file mode 100644
index 0000000..3e6b99c
--- /dev/null
+++ b/solvitaire/src/main/input-output/input/json-parsing/json_helper.cpp
@@ -0,0 +1,110 @@
+//
+// Created by thecharlesblake on 11/13/17.
+//
+
+#include <fstream>
+#include <sstream>
+
+#include "json_helper.h"
+#include "../../../../../lib/rapidjson/stringbuffer.h"
+#include "../../output/log_helper.h"
+
+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);
+ 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) {
+ string err_msg = "Error in JSON doc: " + msg;
+ LOG_DEBUG(err_msg);
+ throw runtime_error(err_msg);
+}
+
+void json_helper::json_parse_warning(const string& msg) {
+ LOG_WARNING("Error in JSON doc: " + msg);
+}
+
+const string json_helper::schema_err_str(const SchemaValidator& validator) {
+ string ret = "Input JSON failed to match the required schema. Schema Validator Error = ";
+
+ StringBuffer sb;
+ validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);
+ ret += validator.GetInvalidSchemaKeyword();
+ ret += ": ";
+
+ sb.Clear();
+ validator.GetInvalidDocumentPointer().StringifyUriFragment(sb);
+
+ 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/solvitaire/src/main/input-output/input/json-parsing/json_helper.h b/solvitaire/src/main/input-output/input/json-parsing/json_helper.h
new file mode 100644
index 0000000..460911f
--- /dev/null
+++ b/solvitaire/src/main/input-output/input/json-parsing/json_helper.h
@@ -0,0 +1,31 @@
+//
+// Created by thecharlesblake on 11/13/17.
+//
+
+#ifndef SOLVITAIRE_UTIL_H
+#define SOLVITAIRE_UTIL_H
+
+#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:
+ static rapidjson::Document get_file_json(const std::string&);
+ 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&);
+};
+
+
+#endif //SOLVITAIRE_UTIL_H
diff --git a/solvitaire/src/main/input-output/input/json-parsing/rules_parser.cpp b/solvitaire/src/main/input-output/input/json-parsing/rules_parser.cpp
new file mode 100644
index 0000000..7c49577
--- /dev/null
+++ b/solvitaire/src/main/input-output/input/json-parsing/rules_parser.cpp
@@ -0,0 +1,746 @@
+//
+// Created by thecharlesblake on 1/3/18.
+//
+
+#include <vector>
+#include <algorithm>
+
+#include "../../../../../lib/rapidjson/error/en.h"
+
+#include "rules_parser.h"
+#include "json_helper.h"
+#include "../sol_preset_types.h"
+#include "../../../game/card.h"
+#include "../../../game/sol_rules.h"
+
+using namespace std;
+using namespace rapidjson;
+
+////////////
+// Static //
+////////////
+
+typedef sol_rules::build_policy pol;
+typedef sol_rules::spaces_policy s_pol;
+typedef sol_rules::accordion_policy acc_pol;
+typedef sol_rules::stock_deal_type sdt;
+typedef sol_rules::face_up_policy fu;
+typedef sol_rules::direction dir;
+typedef sol_rules::built_group_type bgt;
+typedef sol_rules::foundations_init_type fit;
+
+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) {
+ // There are two stages to reading in a rules document. It is both put
+ // through the rule schema validator, and also parsed into
+ // the game rules object (with some further checking done). The parsing is
+ // done first because the errors it detects tend to give clearer error
+ // messages than the schema validator
+
+ 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<uint8_t>(d["tableau piles"]["count"].GetInt());
+ if (sr.tableau_pile_count > 52) {
+ json_helper::json_parse_warning("[tableau piles][count] may be too high");
+ }
+ } else {
+ json_helper::json_parse_err("[tableau piles][count] must be an integer");
+ }
+ }
+
+ 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 if (sp_str == "kings") {
+ sr.spaces_pol = s_pol::KINGS;
+ } else if (sp_str == "auto-reserve-then-waste") {
+ sr.spaces_pol = s_pol::AUTO_RESERVE_THEN_WASTE;
+ } else if (sp_str == "auto-waste-then-stock") {
+ sr.spaces_pol = s_pol::AUTO_WASTE_THEN_STOCK;
+ } else {
+ json_helper::json_parse_err("[tableau piles][spaces policy] is invalid");
+ }
+
+ } else {
+ json_helper::json_parse_err("[tableau piles][spaces policy] must be a string");
+ }
+ }
+
+ if (d["tableau piles"].HasMember("move built group")) {
+ if (d["tableau piles"]["move built group"].IsString()) {
+ string mbg_str = d["tableau piles"]["move built group"].GetString();
+
+ if (mbg_str == "yes") {
+ sr.move_built_group = bgt::YES;
+ } else if (mbg_str == "no") {
+ sr.move_built_group = bgt::NO;
+ } else if (mbg_str == "whole-pile") {
+ sr.move_built_group = bgt::WHOLE_PILE;
+ } else if (mbg_str == "maximal-group") {
+ sr.move_built_group = bgt::MAXIMAL_GROUP;
+ } else {
+ json_helper::json_parse_err("[tableau piles][move built group] is invalid");
+ }
+ } else {
+ json_helper::json_parse_err("[tableau piles][move built group] must be a string");
+ }
+ }
+
+ string mbgp_str;
+ if (d["tableau piles"].HasMember("move built group policy")) {
+ if (d["tableau piles"]["move built group policy"].IsString()) {
+ mbgp_str = d["tableau piles"]["move built group policy"].GetString();
+ } else {
+ json_helper::json_parse_err("[tableau piles][move built group policy] must be a string");
+ }
+ } else {
+ mbgp_str = "same-as-build";
+ }
+ if (mbgp_str == "same-as-build") {
+ sr.built_group_pol = sr.build_pol;
+ } else if (mbgp_str == "any-suit") {
+ sr.built_group_pol = pol::ANY_SUIT;
+ } else if (mbgp_str == "red-black") {
+ sr.built_group_pol = pol::RED_BLACK;
+ } else if (mbgp_str == "same-suit") {
+ sr.built_group_pol = pol::SAME_SUIT;
+ } else if (mbgp_str == "no-build") {
+ sr.built_group_pol = pol::NO_BUILD;
+ } else {
+ json_helper::json_parse_err("[tableau piles][move built group policy] is invalid");
+ }
+
+ 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");
+ }
+ }
+
+ string face_up_str;
+ if (d["tableau piles"].HasMember("face up cards")) {
+ if (d["tableau piles"]["face up cards"].IsString()) {
+ face_up_str = d["tableau piles"]["face up cards"].GetString();
+
+ if (face_up_str == "all") {
+ sr.face_up = fu::ALL;
+ } else if (face_up_str == "top") {
+ sr.face_up = fu::TOP_CARDS;
+ } else {
+ string err = "[tableau piles][face up cards] is invalid: " + face_up_str;
+ json_helper::json_parse_err(err);
+ }
+
+ } else {
+ json_helper::json_parse_err("[tableau piles][face up cards] must be a string");
+ }
+ }
+
+ } else {
+ json_helper::json_parse_err("[tableau piles] must be an object");
+ }
+ }
+
+ if (d.HasMember("foundations")) {
+ if (d["foundations"].IsObject()) {
+ if (d["foundations"].HasMember("present")) {
+ if (d["foundations"]["present"].IsBool()) {
+ sr.foundations_present = d["foundations"]["present"].GetBool();
+ } else {
+ json_helper::json_parse_err("[foundations][present] must be a boolean");
+ }
+ }
+
+ if (d["foundations"].HasMember("initial cards")) {
+ if (d["foundations"]["initial cards"].IsString()) {
+ string ic_str = d["foundations"]["initial cards"].GetString();
+
+ if (ic_str == "none") {
+ sr.foundations_init_cards = fit::NONE;
+ } else if (ic_str == "one") {
+ sr.foundations_init_cards = fit::ONE;
+ } else if (ic_str == "all") {
+ sr.foundations_init_cards = fit::ALL;
+ } else {
+ string err = "[foundations][initial cards] is invalid: " + ic_str;
+ json_helper::json_parse_err(err);
+ }
+ } else {
+ json_helper::json_parse_err("[foundations][initial cards] must be a string");
+ }
+ }
+
+ if (d["foundations"].HasMember("base card")) {
+ if (d["foundations"]["base card"].IsString()) {
+ string base_card_str = d["foundations"]["base card"].GetString();
+
+ if (base_card_str == "random") {
+ sr.foundations_base = boost::none;
+ } else {
+ base_card_str += "H";
+ sr.foundations_base = card(base_card_str.c_str()).get_rank();
+ }
+ } else {
+ json_helper::json_parse_err("[foundations][base card] must be a string");
+ }
+ }
+
+ if (d["foundations"].HasMember("removable")) {
+ if (d["foundations"]["removable"].IsBool()) {
+ sr.foundations_removable = d["foundations"]["removable"].GetBool();
+ } else {
+ json_helper::json_parse_err("[foundations][removable] must be a boolean");
+ }
+ }
+
+ if (d["foundations"].HasMember("complete piles")) {
+ if (d["foundations"]["complete piles"].IsBool()) {
+ sr.foundations_only_comp_piles = d["foundations"]["complete piles"].GetBool();
+ } else {
+ json_helper::json_parse_err("[foundations][complete piles] must be a boolean");
+ }
+ }
+
+ } else {
+ json_helper::json_parse_err("[foundations] must be an object");
+ }
+ }
+
+ 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("cells")) {
+ if (d["cells"].IsObject()) {
+ if (d["cells"].HasMember("count")) {
+ if (d["cells"]["count"].IsInt()) {
+ sr.cells = static_cast<uint8_t>(d["cells"]["count"].GetInt());
+ } else {
+ json_helper::json_parse_err("[cells][count] must be an integer");
+ }
+ }
+
+ if (d["cells"].HasMember("pre-filled")) {
+ if (d["cells"]["pre-filled"].IsInt()) {
+ sr.cells_pre_filled = static_cast<uint8_t>(d["cells"]["pre-filled"].GetInt());
+ } else {
+ json_helper::json_parse_err("[cells][pre-filled] must be an integer");
+ }
+ }
+ } else {
+ json_helper::json_parse_err("[cells] must be an object");
+ }
+ }
+
+ if (d.HasMember("stock")) {
+ if (d["stock"].IsObject()) {
+ if (d["stock"].HasMember("size")) {
+ if (d["stock"]["size"].IsInt()) {
+ sr.stock_size = static_cast<uint8_t>(d["stock"]["size"].GetInt());
+ } else {
+ json_helper::json_parse_err("[stock][size] must be an integer");
+ }
+ }
+
+ if (d["stock"].HasMember("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["stock"].HasMember("deal count")) {
+ if (d["stock"]["deal count"].IsInt()) {
+ sr.stock_deal_count = static_cast<uint8_t>(d["stock"]["deal count"].GetInt());
+ } else {
+ json_helper::json_parse_err("[stock][deal count] must be an integer");
+ }
+ }
+
+ if (d["stock"].HasMember("redeal")) {
+ if (d["stock"]["redeal"].IsBool()) {
+ sr.stock_redeal = static_cast<uint8_t>(d["stock"]["redeal"].GetBool());
+ } else {
+ json_helper::json_parse_err("[stock][redeal] must be a boolean");
+ }
+ }
+ } else {
+ json_helper::json_parse_err("[stock] must be an object");
+ }
+ }
+
+ if (d.HasMember("reserve")) {
+ if (d["reserve"].IsObject()) {
+ if (d["reserve"].HasMember("size")) {
+ if (d["reserve"]["size"].IsInt()) {
+ sr.reserve_size = static_cast<uint8_t>(d["reserve"]["size"].GetInt());
+ } else {
+ json_helper::json_parse_err("[size] must be an integer");
+ }
+ }
+
+ if (d["reserve"].HasMember("stacked")) {
+ if (d["reserve"]["stacked"].IsBool()) {
+ sr.reserve_stacked = d["reserve"]["stacked"].GetBool();
+ } else {
+ json_helper::json_parse_err("[stacked] must be a boolean");
+ }
+ }
+ } else {
+ json_helper::json_parse_err("[reserve] must be an object");
+ }
+ }
+
+ if (d.HasMember("accordion")) {
+ if (d["accordion"].IsObject()) {
+ if (d["accordion"].HasMember("size")) {
+ if (d["accordion"]["size"].IsInt()) {
+ sr.accordion_size = static_cast<uint8_t>(d["accordion"]["size"].GetInt());
+ } else {
+ json_helper::json_parse_err("[size] must be an integer");
+ }
+ }
+
+ if (d["accordion"].HasMember("moves")) {
+ if (d["accordion"]["moves"].IsArray()) {
+ for (auto& json_move : d["accordion"]["moves"].GetArray()) {
+ string move_str = json_move.GetString();
+
+ sol_rules::direction move_dir;
+ if (move_str[0] == 'L') {
+ move_dir = sol_rules::direction::LEFT;
+ } else if (move_str[0] == 'R') {
+ move_dir = sol_rules::direction::RIGHT;
+ } else {
+ json_helper::json_parse_err(R"([accordion][moves] must begin 'L' or 'R')");
+ }
+
+ auto count_str = move_str.substr(1, move_str.size() - 1);
+ auto count = static_cast<uint8_t>(stoi(count_str));
+
+ sr.accordion_moves.emplace_back(move_dir, count);
+ }
+ } else {
+ json_helper::json_parse_err("[accordion][moves] must be an array");
+ }
+ }
+
+ if (d["accordion"].HasMember("build policies")) {
+ if (d["accordion"]["build policies"].IsArray()) {
+ for (auto& json_policy : d["accordion"]["build policies"].GetArray()) {
+ string policy_str = json_policy.GetString();
+
+ if (policy_str == "any-suit") {
+ sr.accordion_pol.emplace_back(acc_pol::ANY_SUIT);
+ } else if (policy_str == "red-black") {
+ sr.accordion_pol.emplace_back(acc_pol::RED_BLACK);
+ } else if (policy_str == "same-suit") {
+ sr.accordion_pol.emplace_back(acc_pol::SAME_SUIT);
+ } else if (policy_str == "same-rank") {
+ sr.accordion_pol.emplace_back(acc_pol::SAME_RANK);
+ } else {
+ json_helper::json_parse_err("[accordion][build policies] is invalid");
+ }
+ }
+ } else {
+ json_helper::json_parse_err("[accordion][build policies] must be an array");
+ }
+ }
+ } else {
+ json_helper::json_parse_err("[accordion] must be an object");
+ }
+ }
+
+ if (d.HasMember("sequences")) {
+ if (d["sequences"].IsObject()) {
+ if (d["sequences"].HasMember("count")) {
+ if (d["sequences"]["count"].IsInt()) {
+ sr.sequence_count = static_cast<uint8_t>(d["sequences"]["count"].GetInt());
+ } else {
+ json_helper::json_parse_err("[count] must be an integer");
+ }
+ }
+
+ if (d["sequences"].HasMember("build policy")) {
+ if (d["sequences"]["build policy"].IsString()) {
+ string bp_str = d["sequences"]["build policy"].GetString();
+
+ if (bp_str == "any-suit") {
+ sr.sequence_build_pol = pol::ANY_SUIT;
+ } else if (bp_str == "red-black") {
+ sr.sequence_build_pol = pol::RED_BLACK;
+ } else if (bp_str == "same-suit") {
+ sr.sequence_build_pol = pol::SAME_SUIT;
+ } else {
+ json_helper::json_parse_err("[sequences][build policy] is invalid");
+ }
+ } else {
+ json_helper::json_parse_err("[sequences][build policy] must be an string");
+ }
+ }
+
+ if (d["sequences"].HasMember("fixed suit")) {
+ if (d["sequences"]["fixed suit"].IsBool()) {
+ sr.sequence_fixed_suit = d["sequences"]["fixed suit"].GetBool();
+ } else {
+ json_helper::json_parse_err("[fixed suit] must be a boolean");
+ }
+ }
+
+ if (d["sequences"].HasMember("direction")) {
+ if (d["sequences"]["direction"].IsString()) {
+ string dir_str = d["sequences"]["direction"].GetString();
+
+ if (dir_str == "L") {
+ sr.sequence_direction = dir::LEFT;
+ } else if (dir_str == "R") {
+ sr.sequence_direction = dir::RIGHT;
+ } else if (dir_str == "LR") {
+ sr.sequence_direction = dir::BOTH;
+ } else {
+ json_helper::json_parse_err("[sequences][direction] is invalid");
+ }
+ } else {
+ json_helper::json_parse_err("[sequences][direction] must be an string");
+ }
+ }
+ } else {
+ json_helper::json_parse_err("[sequences] must be an object");
+ }
+ }
+
+ if (d.HasMember("max rank")) {
+ if (d["max rank"].IsInt()) {
+ sr.max_rank = static_cast<card::rank_t>(d["max rank"].GetInt());
+ if (sr.max_rank > 13) {
+ json_helper::json_parse_err("[max rank] must be a valid rank");
+ }
+ } else {
+ json_helper::json_parse_err("[max rank] must be an integer");
+ }
+ }
+
+ 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");
+ }
+ }
+
+ int solution_types = 0;
+ if (sr.hole) solution_types++;
+ if (sr.foundations_present) solution_types++;
+ if (sr.sequence_count > 0) solution_types++;
+ if (sr.accordion_size > 0) solution_types++;
+ if (solution_types != 1) {
+ json_helper::json_parse_err("one and only one of [hole], [foundations], [accordion][size] > 0 and [sequences][count] > 0 "
+ "must be true");
+ }
+
+ Document vd;
+ vd.Parse(rules_schema_json().c_str());
+ assert(!vd.HasParseError());
+ SchemaDocument sd(vd);
+ SchemaValidator validator(sd);
+ if(!d.Accept(validator)) {
+ throw runtime_error(json_helper::schema_err_str(validator));
+ }
+}
+
+string rules_parser::rules_schema_json() {
+ string schema_json = R"(
+{
+ "$schema": "http://json-schema.org/draft-04/schema#",
+ "description": "JSON Schema representing a generic solitaire game",
+ "type": "object",
+ "properties": {
+ "tableau piles": {
+ "type": "object",
+ "properties": {
+ "count": {
+ "type": "integer",
+ "minimum": 0
+ },
+ "build policy": {
+ "type": "string",
+ "enum": [
+ "any-suit",
+ "red-black",
+ "same-suit",
+ "no-build"
+ ]
+ },
+ "spaces policy": {
+ "type": "string",
+ "enum": [
+ "any",
+ "no-build",
+ "kings",
+ "auto-reserve-then-waste",
+ "auto-waste-then-stock"
+ ]
+ },
+ "diagonal deal": {
+ "type": "boolean"
+ },
+ "move built group": {
+ "type": "string",
+ "enum": [
+ "yes",
+ "no",
+ "whole-pile",
+ "maximal-group"
+ ]
+ },
+ "move built group policy": {
+ "type": "string",
+ "enum": [
+ "same-as-build",
+ "any-suit",
+ "red-black",
+ "same-suit",
+ "no-build"
+ ]
+ },
+ "face up cards": {
+ "type": "string",
+ "enum": [
+ "all",
+ "top"
+ ]
+ }
+ },
+ "additionalProperties": false
+ },
+ "foundations": {
+ "type": "object",
+ "properties": {
+ "present": {
+ "type": "boolean"
+ },
+ "initial cards": {
+ "type": "string",
+ "enum": [
+ "none",
+ "one",
+ "all"
+ ]
+ },
+ "base card": {
+ "type": "string",
+ "oneOf":[
+ {"pattern": "^(([0-9]|1[0-3]|a|A|j|J|q|Q|k|K))$"},
+ {"enum": ["random"]}
+ ]
+ },
+ "removable": {
+ "type": "boolean"
+ },
+ "only complete pile moves": {
+ "type": "boolean"
+ }
+ },
+ "additionalProperties": false
+ },
+ "hole": {
+ "type": "boolean"
+ },
+ "cells": {
+ "type": "object",
+ "properties": {
+ "count": {
+ "type": "integer",
+ "minimum": 0
+ },
+ "pre-filled": {
+ "type": "integer",
+ "minimum": 0
+ }
+ },
+ "additionalProperties": false
+ },
+ "stock": {
+ "type": "object",
+ "properties": {
+ "size": {
+ "type": "integer",
+ "minimum": 0
+ },
+ "deal type": {
+ "type": "string",
+ "enum": [
+ "waste",
+ "tableau piles"
+ ]
+ },
+ "deal count": {
+ "type": "integer",
+ "minimum": 1
+ },
+ "redeal": {
+ "type": "boolean"
+ }
+ },
+ "additionalProperties": false
+ },
+ "reserve": {
+ "type": "object",
+ "properties": {
+ "size": {
+ "type": "integer",
+ "minimum": 0
+ },
+ "stacked": {
+ "type": "boolean"
+ }
+ },
+ "additionalProperties": false
+ },
+ "accordion": {
+ "type": "object",
+ "properties": {
+ "size": {
+ "type": "integer",
+ "minimum": 0
+ },
+ "moves": {
+ "items": {
+ "type": "string",
+ "pattern": "^((L|R)([1-9]|[1-4][0-9]|5[0-2]))$"
+ }
+ },
+ "build policies": {
+ "type": "array",
+ "items": {
+ "type": "string",
+ "enum": [
+ "same-suit",
+ "red-black",
+ "any-suit",
+ "same-rank"
+ ]
+ }
+ }
+ },
+ "additionalProperties": false
+ },
+ "sequences": {
+ "type": "object",
+ "properties": {
+ "count": {
+ "type": "integer",
+ "minimum": 0
+ },
+ "direction": {
+ "type": "string",
+ "enum": [
+ "L",
+ "R",
+ "LR"
+ ]
+ },
+ "fixed suit": {
+ "type": "boolean"
+ },
+ "build policy": {
+ "type": "string",
+ "enum": [
+ "any-suit",
+ "red-black",
+ "same-suit"
+ ]
+ }
+ },
+ "additionalProperties": false
+ },
+ "max rank": {
+ "type": "integer",
+ "minimum": 1,
+ "maximum": 13
+ },
+ "two decks": {
+ "type": "boolean"
+ }
+ },
+ "additionalProperties": false
+}
+)";
+ return schema_json;
+}
diff --git a/solvitaire/src/main/input-output/input/json-parsing/rules_parser.h b/solvitaire/src/main/input-output/input/json-parsing/rules_parser.h
new file mode 100644
index 0000000..fdfe018
--- /dev/null
+++ b/solvitaire/src/main/input-output/input/json-parsing/rules_parser.h
@@ -0,0 +1,22 @@
+//
+// 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);
+ static std::string rules_schema_json();
+
+private:
+ static sol_rules get_default();
+ static void modify_sol_rules(sol_rules&, rapidjson::Document&);
+};
+
+
+#endif //SOLVITAIRE_RULES_PARSER_H
diff --git a/solvitaire/src/main/input-output/input/sol_preset_types.cpp b/solvitaire/src/main/input-output/input/sol_preset_types.cpp
new file mode 100644
index 0000000..b169370
--- /dev/null
+++ b/solvitaire/src/main/input-output/input/sol_preset_types.cpp
@@ -0,0 +1,1630 @@
+//
+// Created by thecharlesblake on 11/14/17.
+//
+
+#include <iostream>
+#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;
+}
+
+map<string, string> sol_preset_types::mp = {
+ {
+ "default",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 8,
+ "build policy": "any-suit",
+ "spaces policy": "any",
+ "diagonal deal": false,
+ "move built group": "no",
+ "move built group policy": "same-as-build",
+ "face up cards": "all"
+ },
+ "foundations": {
+ "present": true,
+ "initial cards": "none",
+ "base card": "A",
+ "removable": false,
+ "only complete pile moves": false
+ },
+ "hole": false,
+ "cells": {
+ "count": 0
+ },
+ "stock": {
+ "size": 0,
+ "deal type": "waste",
+ "deal count": 1,
+ "redeal": false
+ },
+ "reserve": {
+ "size": 0,
+ "stacked": false
+ },
+ "accordion": {
+ "size": 0,
+ "moves": [],
+ "build policies": []
+ },
+ "sequences": {
+ "count": 0,
+ "direction": "L",
+ "build policy": "same-suit",
+ "fixed suit": false
+ },
+ "max rank": 13,
+ "two decks": false
+}
+)"
+
+ },
+ {
+ "spanish-patience",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 13
+ },
+ "foundations": {
+ "removable": true
+ }
+}
+)"
+
+ },
+ {
+ "simple-spanish-patience",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 3
+ },
+ "foundations": {
+ "removable": true
+ },
+ "max rank": 2
+}
+)"
+
+ },
+ {
+ "free-cell",
+
+ R"(
+{
+ "tableau piles": {
+ "build policy": "red-black"
+ },
+ "cells": {
+ "count": 4
+ }
+}
+)"
+
+ },
+ {
+ "simple-free-cell",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 4,
+ "build policy": "red-black"
+ },
+ "cells": {
+ "count": 1
+ },
+ "max rank": 4
+}
+)"
+
+ },
+ {
+ "bakers-dozen",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 13,
+ "spaces policy": "no-build"
+ },
+ "foundations": {
+ "removable": true
+ }
+}
+)"
+
+ },
+ {
+ "simple-bakers-dozen",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 3,
+ "spaces policy": "no-build"
+ },
+ "foundations": {
+ "removable": true
+ },
+ "max rank": 3
+}
+)"
+
+ },
+ {
+ "black-hole",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 17,
+ "build policy": "no-build"
+ },
+ "foundations": {
+ "present": false
+ },
+ "hole": true
+}
+)"
+
+ },
+ {
+ "simple-black-hole",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 4,
+ "build policy": "no-build"
+ },
+ "foundations": {
+ "present": false
+ },
+ "hole": true,
+ "max rank": 5
+}
+)"
+
+ },
+ {
+ "fortunes-favor",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 12,
+ "build policy": "same-suit",
+ "spaces policy": "auto-waste-then-stock"
+ },
+ "foundations": {
+ "initial cards": "all"
+ },
+ "stock": {
+ "size": 36
+ }
+}
+)"
+
+ },
+ {
+ "simple-fortunes-favor",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 3,
+ "build policy": "same-suit"
+ },
+ "foundations": {
+ "initial cards": "all"
+ },
+ "stock": {
+ "size": 3
+ },
+ "max rank": 4
+}
+)"
+
+ },
+ {
+ "flower-garden",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 6
+ },
+ "reserve": {
+ "size": 16
+ },
+ "foundations": {
+ "removable": true
+ }
+}
+)"
+
+ },
+ {
+ "simple-flower-garden",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 3
+ },
+ "reserve": {
+ "size": 3
+ },
+ "foundations": {
+ "removable": true
+ },
+ "max rank": 3
+}
+)"
+
+ },
+ {
+ "canfield",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 4,
+ "build policy": "red-black",
+ "move built group": "whole-pile",
+ "spaces policy": "auto-reserve-then-waste"
+ },
+ "foundations": {
+ "initial cards": "one",
+ "base card": "random"
+ },
+ "stock": {
+ "size": 34,
+ "deal count": 3,
+ "redeal": true
+ },
+ "reserve": {
+ "size": 13,
+ "stacked": true
+ }
+}
+)"
+
+ },
+ {
+ "simple-canfield",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 3,
+ "build policy": "red-black",
+ "move built group": "whole-pile",
+ "spaces policy": "auto-reserve-then-waste"
+ },
+ "foundations": {
+ "initial cards": "one",
+ "base card": "random"
+ },
+ "stock": {
+ "size": 6,
+ "deal count": 3,
+ "redeal": true
+ },
+ "reserve": {
+ "size": 2,
+ "stacked": true
+ },
+ "max rank": 3
+}
+)"
+
+ },
+ {
+ "somerset",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 10,
+ "build policy": "red-black",
+ "diagonal deal": true
+ },
+ "foundations": {
+ "removable": true
+ }
+}
+)"
+
+ },
+ {
+ "simple-somerset",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 4,
+ "build policy": "red-black",
+ "diagonal deal": true
+ },
+ "foundations": {
+ "removable": true
+ },
+ "max rank": 2
+}
+)"
+
+ },
+ {
+ "alpha-star",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 12,
+ "build policy": "same-suit",
+ "move built group": "yes"
+ },
+ "foundations": {
+ "initial cards": "all"
+ }
+}
+)"
+
+ },
+ {
+ "simple-alpha-star",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 3,
+ "build policy": "same-suit",
+ "move built group": "yes"
+ },
+ "foundations": {
+ "initial cards": "all"
+ },
+ "max rank": 6
+}
+)"
+
+ },
+ {
+ "spider",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 10,
+ "build policy": "any-suit",
+ "move built group": "yes",
+ "move built group policy": "same-suit",
+ "face up cards": "top"
+ },
+ "foundations": {
+ "only complete pile moves": true
+ },
+ "stock": {
+ "size": 50,
+ "deal type": "tableau piles"
+ },
+ "two decks": true
+}
+)"
+
+ },
+ {
+ "simple-spider",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 4,
+ "build policy": "any-suit",
+ "move built group": "yes",
+ "move built group policy": "same-suit",
+ "face up cards": "top"
+ },
+ "foundations": {
+ "only complete pile moves": true
+ },
+ "stock": {
+ "size": 12,
+ "deal type": "tableau piles"
+ },
+ "two decks": true,
+ "max rank": 3
+}
+)"
+
+ },
+
+ {
+ "simple-klondike",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 4,
+ "build policy": "red-black",
+ "move built group": "yes",
+ "diagonal deal": true,
+ "face up cards": "top"
+ },
+ "stock": {
+ "size": 6,
+ "deal count": 3,
+ "redeal": true
+ },
+ "max rank": 4
+}
+)"
+ },
+ // --- END OF 'CANONICAL' GAMES ---
+
+ {
+ "raglan",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 9,
+ "build policy": "red-black",
+ "diagonal deal": true
+ },
+ "reserve": {
+ "size": 6
+ },
+ "foundations": {
+ "removable": true,
+ "initial cards": "all"
+ }
+}
+)"
+
+ },
+
+ {
+ "klondike-deal-1",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 7,
+ "build policy": "red-black",
+ "spaces policy": "kings",
+ "move built group": "yes",
+ "diagonal deal": true,
+ "face up cards": "top"
+ },
+ "foundations": {
+ "removable": true
+ },
+ "stock": {
+ "size": 24,
+ "deal count": 1,
+ "redeal": true
+ }
+}
+)"
+
+ },
+
+ {
+ "klondike",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 7,
+ "build policy": "red-black",
+ "spaces policy": "kings",
+ "move built group": "yes",
+ "diagonal deal": true,
+ "face up cards": "top"
+ },
+ "foundations": {
+ "removable": true
+ },
+ "stock": {
+ "size": 24,
+ "deal count": 3,
+ "redeal": true
+ }
+}
+)"
+
+ },
+ {
+ "alina",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 13,
+ "build policy": "any-suit",
+ "spaces policy": "no-build"
+ },
+ "foundations": {
+ "removable": true
+ },
+ "cells": {
+ "count": 1
+ }
+}
+)"
+
+ },
+ {
+ "castles-of-spain",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 13,
+ "build policy": "red-black"
+ },
+ "foundations": {
+ "removable": true
+ }
+}
+)"
+
+ },
+ {
+ "delta-star",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 12,
+ "build policy": "same-suit"
+ },
+ "foundations": {
+ "initial cards": "all"
+ }
+}
+)"
+
+ },
+ {
+ "martha",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 12,
+ "build policy": "red-black"
+ },
+ "foundations": {
+ "initial cards": "all",
+ "removable": true
+ }
+}
+)"
+
+ },
+ {
+ "one-cell",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 13,
+ "build policy": "red-black"
+ },
+ "foundations": {
+ "initial cards": "all",
+ "removable": true
+ },
+ "cells": {
+ "count": 1
+ }
+}
+)"
+
+ },
+ {
+ "two-cell",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 13,
+ "build policy": "red-black"
+ },
+ "foundations": {
+ "initial cards": "all",
+ "removable": true
+ },
+ "cells": {
+ "count": 2
+ }
+}
+)"
+
+ },
+ {
+ "free-cell-3-cell",
+
+ R"(
+{
+ "tableau piles": {
+ "build policy": "red-black"
+ },
+ "cells": {
+ "count": 3
+ }
+}
+)"
+
+ },
+ {
+ "free-cell-2-cell",
+
+ R"(
+{
+ "tableau piles": {
+ "build policy": "red-black"
+ },
+ "cells": {
+ "count": 2
+ }
+}
+)"
+
+ },
+ {
+ "free-cell-1-cell",
+
+ R"(
+{
+ "tableau piles": {
+ "build policy": "red-black"
+ },
+ "cells": {
+ "count": 1
+ }
+}
+)"
+
+ },
+ {
+ "free-cell-0-cell",
+
+ R"(
+{
+ "tableau piles": {
+ "build policy": "red-black"
+ }
+}
+)"
+
+ },
+ {
+ "free-cell-7-pile",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 7,
+ "build policy": "red-black"
+ },
+ "cells": {
+ "count": 4
+ }
+}
+)"
+
+ },
+ {
+ "free-cell-6-pile",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 6,
+ "build policy": "red-black"
+ },
+ "cells": {
+ "count": 4
+ }
+}
+)"
+
+ },
+ {
+ "free-cell-5-pile",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 5,
+ "build policy": "red-black"
+ },
+ "cells": {
+ "count": 4
+ }
+}
+)"
+
+ },
+ {
+ "free-cell-4-pile",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 4,
+ "build policy": "red-black"
+ },
+ "cells": {
+ "count": 4
+ }
+}
+)"
+
+ },
+ {
+ "bakers-game",
+
+ R"(
+{
+ "tableau piles": {
+ "build policy": "same-suit"
+ },
+ "cells": {
+ "count": 4
+ }
+}
+)"
+
+ },
+ {
+ "eight-off",
+
+ R"(
+{
+ "tableau piles": {
+ "build policy": "same-suit",
+ "spaces policy": "kings"
+ },
+ "cells": {
+ "count": 8,
+ "pre-filled": 4
+ }
+}
+)"
+
+ },
+ {
+ "eight-off-any-card-spaces",
+
+ R"(
+{
+ "tableau piles": {
+ "build policy": "same-suit"
+ },
+ "cells": {
+ "count": 8
+ }
+}
+)"
+
+ },
+ {
+ "fore-cell",
+
+ R"(
+{
+ "tableau piles": {
+ "build policy": "red-black",
+ "spaces policy": "kings"
+ },
+ "cells": {
+ "count": 4,
+ "pre-filled": 4
+ }
+}
+)"
+
+ },
+ {
+ "fore-cell-same-suit",
+
+ R"(
+{
+ "tableau piles": {
+ "build policy": "same-suit",
+ "spaces policy": "kings"
+ },
+ "cells": {
+ "count": 4,
+ "pre-filled": 4
+ }
+}
+)"
+
+ },
+ {
+ "penguin",
+ R"(
+
+{
+ "tableau piles": {
+ "count": 7,
+ "build policy": "same-suit",
+ "move built group": "yes",
+ "spaces policy": "kings"
+ },
+ "cells": {
+ "count": 7
+ },
+ "foundations": {
+ "initial cards": "all"
+ }
+}
+)"
+
+ },
+ {
+ "sea-towers",
+ R"(
+
+{
+ "tableau piles": {
+ "count": 10,
+ "build policy": "same-suit",
+ "move built group": "yes",
+ "spaces policy": "kings"
+ },
+ "cells": {
+ "count": 4
+ }
+}
+)"
+
+ },
+ {
+ "duchess",
+ R"(
+
+{
+ "tableau piles": {
+ "count": 4,
+ "build policy": "red-black",
+ "move built group": "yes"
+ },
+ "foundations": {
+ "initial cards": "all"
+ },
+ "reserve": {
+ "size": 12
+ },
+ "stock": {
+ "size": 36
+ }
+}
+)"
+
+ },
+ {
+ "chameleon",
+ R"(
+
+{
+ "tableau piles": {
+ "count": 3,
+ "build policy": "any-suit",
+ "move built group": "yes"
+ },
+ "reserve": {
+ "size": 13,
+ "stacked": true
+ },
+ "stock": {
+ "size": 36
+ }
+}
+)"
+
+ },
+ {
+ "fan",
+ R"(
+
+{
+ "tableau piles": {
+ "count": 18,
+ "build policy": "same-suit",
+ "spaces policy": "kings"
+ },
+ "foundations": {
+ "removable": true
+ }
+}
+)"
+
+ },
+ {
+ "scotch-patience",
+ R"(
+
+{
+ "tableau piles": {
+ "count": 18,
+ "build policy": "red-black",
+ "spaces policy": "no-build"
+ },
+ "foundations": {
+ "removable": true
+ }
+}
+)"
+
+ },
+ {
+ "blind-alleys",
+ R"(
+
+{
+ "tableau piles": {
+ "count": 6,
+ "build policy": "red-black",
+ "move built group": "yes"
+ },
+ "foundations": {
+ "initial cards": "all",
+ "removable": true
+ },
+ "stock": {
+ "size": 30
+ }
+}
+)"
+
+ },
+ {
+ "east-haven",
+ R"(
+
+{
+ "tableau piles": {
+ "count": 7,
+ "build policy": "red-black",
+ "move built group": "yes",
+ "face up cards": "top"
+ },
+ "foundations": {
+ "removable": true
+ },
+ "stock": {
+ "size": 31,
+ "deal type": "tableau piles"
+ }
+}
+)"
+
+ },
+ {
+ "northwest-territory",
+ R"(
+
+{
+ "tableau piles": {
+ "build policy": "red-black",
+ "move built group": "yes",
+ "spaces policy": "kings",
+ "diagonal deal": true
+ },
+ "foundations": {
+ "removable": true
+ },
+ "reserve": {
+ "size": 16
+ }
+}
+)"
+
+ },
+ {
+ "king-albert",
+ R"(
+
+{
+ "tableau piles": {
+ "count": 9,
+ "build policy": "red-black",
+ "diagonal deal": true
+ },
+ "foundations": {
+ "removable": true
+ },
+ "reserve": {
+ "size": 7
+ }
+}
+)"
+
+ },
+ {
+ "spiderette",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 7,
+ "build policy": "any-suit",
+ "move built group": "yes",
+ "move built group policy": "same-suit",
+ "diagonal deal": true,
+ "face up cards": "top"
+ },
+ "foundations": {
+ "only complete pile moves": true
+ },
+ "stock": {
+ "size": 24,
+ "deal type": "tableau piles"
+ }
+}
+)"
+
+ },
+ {
+ "will-o-the-wisp",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 7,
+ "build policy": "any-suit",
+ "move built group": "yes",
+ "move built group policy": "same-suit"
+ },
+ "foundations": {
+ "only complete pile moves": true
+ },
+ "stock": {
+ "size": 31,
+ "deal type": "tableau piles"
+ }
+}
+)"
+
+ },
+ {
+ "simple-simon",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 10,
+ "diagonal deal": true,
+ "move built group": "yes",
+ "move built group policy": "same-suit"
+ },
+ "foundations": {
+ "only complete pile moves": true
+ }
+}
+)"
+
+ },
+ {
+ "gaps",
+
+ R"(
+{
+ "foundations": {
+ "present": false
+ },
+ "tableau piles": {
+ "count": 0
+ },
+ "sequences": {
+ "count": 4,
+ "direction": "L",
+ "build policy": "same-suit",
+ "fixed suit": false
+ }
+}
+)"
+
+ },
+ {
+ "gaps-basic-variant",
+
+ R"(
+{
+ "foundations": {
+ "present": false
+ },
+ "tableau piles": {
+ "count": 0
+ },
+ "sequences": {
+ "count": 4,
+ "direction": "L",
+ "build policy": "same-suit",
+ "fixed suit": true
+ }
+}
+)"
+
+ },
+ {
+ "simple-gaps",
+
+ R"(
+{
+ "foundations": {
+ "present": false
+ },
+ "tableau piles": {
+ "count": 0
+ },
+ "sequences": {
+ "count": 4,
+ "direction": "L",
+ "build policy": "same-suit"
+ },
+ "max rank": 4
+}
+)"
+
+ },
+ {
+ "simple-accordion",
+
+ R"(
+{
+ "foundations": {
+ "present": false
+ },
+ "tableau piles": {
+ "count": 0
+ },
+ "accordion": {
+ "size": 10,
+ "moves": ["L1", "L3"],
+ "build policies": ["same-suit", "same-rank"]
+ }
+}
+)"
+
+ },
+ {
+ "accordion-knuth",
+
+ R"(
+{
+ "foundations": {
+ "present": false
+ },
+ "tableau piles": {
+ "count": 0
+ },
+ "accordion": {
+ "size": 18,
+ "moves": ["L1", "L3"],
+ "build policies": ["same-suit", "same-rank"]
+ }
+}
+)"
+
+ },
+ {
+ "accordion",
+
+ R"(
+{
+ "foundations": {
+ "present": false
+ },
+ "tableau piles": {
+ "count": 0
+ },
+ "accordion": {
+ "size": 52,
+ "moves": ["L1", "L3"],
+ "build policies": ["same-suit", "same-rank"]
+ }
+}
+)"
+
+ },
+ {
+ "forty-theives",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 10,
+ "build policy": "same-suit"
+ },
+ "stock": {
+ "size": 64,
+ "redeal": false
+ },
+ "two decks": true
+}
+)"
+
+ },
+ {
+ "lucas",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 13,
+ "build policy": "same-suit"
+ },
+ "stock": {
+ "size": 61,
+ "redeal": false
+ },
+ "foundations": {
+ "initial cards": "all"
+ },
+ "two decks": true
+}
+)"
+
+ },
+ {
+ "maria",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 9,
+ "build policy": "red-black"
+ },
+ "stock": {
+ "size": 68,
+ "redeal": false
+ },
+ "two decks": true
+}
+)"
+
+ },
+ {
+ "limited",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 12,
+ "build policy": "same-suit"
+ },
+ "stock": {
+ "size": 68,
+ "redeal": false
+ },
+ "two decks": true
+}
+)"
+
+ },
+ {
+ "streets",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 10,
+ "build policy": "red-black"
+ },
+ "stock": {
+ "size": 64,
+ "redeal": false
+ },
+ "two decks": true
+}
+)"
+
+ },
+ {
+ "rank-and-file",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 10,
+ "build policy": "red-black",
+ "face up cards": "top",
+ "move built group": "yes"
+ },
+ "stock": {
+ "size": 64,
+ "redeal": false
+ },
+ "two decks": true
+}
+)"
+
+ },
+ {
+ "american-toad",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 8,
+ "build policy": "same-suit",
+ "move built group": "whole-pile",
+ "spaces policy": "auto-reserve-then-waste"
+ },
+ "foundations": {
+ "initial cards": "one",
+ "base card": "random"
+ },
+ "stock": {
+ "size": 75,
+ "deal count": 1,
+ "redeal": false
+ },
+ "reserve": {
+ "size": 20,
+ "stacked": true
+ },
+ "two decks": true
+}
+)"
+
+ },
+
+ {
+ "gargantua",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 9,
+ "build policy": "red-black",
+ "spaces policy": "kings",
+ "move built group": "yes",
+ "diagonal deal": true,
+ "face up cards": "top"
+ },
+ "foundations": {
+ "removable": true
+ },
+ "stock": {
+ "size": 59,
+ "deal count": 1,
+ "redeal": false
+ },
+ "two decks": true
+}
+)"
+
+ },
+
+ {
+ "gargantua-redeal",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 9,
+ "build policy": "red-black",
+ "spaces policy": "kings",
+ "move built group": "yes",
+ "diagonal deal": true,
+ "face up cards": "top"
+ },
+ "foundations": {
+ "removable": true
+ },
+ "stock": {
+ "size": 59,
+ "deal count": 1,
+ "redeal": true
+ },
+ "two decks": true
+}
+)"
+
+ },
+
+ {
+ "ultra-klondike-2-deck",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 12,
+ "build policy": "red-black",
+ "spaces policy": "kings",
+ "move built group": "yes",
+ "diagonal deal": true,
+ "face up cards": "top"
+ },
+ "foundations": {
+ "removable": true
+ },
+ "stock": {
+ "size": 26,
+ "deal count": 3,
+ "redeal": true
+ },
+ "two decks": true
+}
+)"
+
+ },
+
+ {
+ "american-canister",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 8,
+ "build policy": "red-black",
+ "move built group": "yes",
+ "spaces policy": "any"
+ }
+}
+)"
+
+ },
+
+ {
+ "british-canister",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 8,
+ "build policy": "red-black",
+ "move built group": "no",
+ "spaces policy": "kings"
+ }
+}
+)"
+
+ },
+
+ {
+ "canister-warfield",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 8,
+ "build policy": "any-suit",
+ "move built group": "yes",
+ "spaces policy": "any"
+ }
+}
+)"
+
+ },
+
+ {
+ "beleaguered-castle",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 8,
+ "build policy": "any-suit"
+ },
+ "foundations": {
+ "initial cards": "all"
+ }
+}
+)"
+
+ },
+
+ {
+ "klondike-two-deck-9",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 9,
+ "build policy": "red-black",
+ "spaces policy": "kings",
+ "move built group": "yes",
+ "diagonal deal": true,
+ "face up cards": "top"
+ },
+ "foundations": {
+ "removable": true
+ },
+ "stock": {
+ "size": 59,
+ "deal count": 3,
+ "redeal": true
+ },
+ "two decks": true
+}
+)"
+
+ },
+
+ {
+ "streets-and-alleys",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 8,
+ "build policy": "any-suit"
+ },
+ "foundations": {
+ "initial cards": "none"
+ }
+}
+)"
+
+ },
+
+ {
+ "worm-hole",
+
+ R"(
+{
+ "tableau piles": {
+ "count": 17,
+ "build policy": "no-build"
+ },
+ "foundations": {
+ "present": false
+ },
+ "cells": {
+ "count": 1
+ },
+ "hole": true
+}
+)"
+
+ }
+};
+
+void sol_preset_types::print_available_games() {
+ cout << "The following game types can be supplied to solvitaire using the "
+ "'--type [name of game]' option:\n\n";
+ for (auto const& x : mp) {
+ cout << x.first << "\n";
+ }
+ cout << "\nUse the '--describe-game-rules [name of game]' option for an "
+ "overview of the rules of each game:\n";
+}
+
+void sol_preset_types::describe_game_rules(string game) {
+ auto rules_it = mp.find(game);
+ if (rules_it == end(mp)) {
+ cout << "'" << game << "' is not a valid game type. "
+ "Enter '--available-game-types' for a "
+ "list of the games presets that can be solved\n";
+ } else {
+ cout << "The rules of '" << game << "' are as follows:"
+ << rules_it->second;
+
+ if (game != "default") {
+ cout << "\nTo see the default values of the JSON fields not covered "
+ "here, enter '--describe-game-rules default'\n";
+ }
+ }
+}
diff --git a/solvitaire/src/main/input-output/input/sol_preset_types.h b/solvitaire/src/main/input-output/input/sol_preset_types.h
new file mode 100644
index 0000000..c5b27d3
--- /dev/null
+++ b/solvitaire/src/main/input-output/input/sol_preset_types.h
@@ -0,0 +1,22 @@
+//
+// Created by thecharlesblake on 11/14/17.
+//
+
+#ifndef SOLVITAIRE_SOL_PRESET_TYPES_H
+#define SOLVITAIRE_SOL_PRESET_TYPES_H
+
+#include <string>
+#include <map>
+
+class sol_preset_types {
+public:
+ static std::string get(const std::string& s);
+ static bool is_valid_preset(const std::string& s);
+ static void print_available_games();
+ static void describe_game_rules(std::string);
+
+private:
+ static std::map<std::string, std::string> mp;
+};
+
+#endif //SOLVITAIRE_SOL_PRESET_TYPES_H
diff --git a/solvitaire/src/main/input-output/output/log_helper.h b/solvitaire/src/main/input-output/output/log_helper.h
new file mode 100644
index 0000000..7762250
--- /dev/null
+++ b/solvitaire/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 <iostream>
+
+#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(msg) clog << "[warning] " << msg << "\n"
+#define LOG_ERROR(msg) cerr << "[error] " << msg << "\n"
+
+#endif //SOLVITAIRE_LOG_HELPER_H
diff --git a/solvitaire/src/main/input-output/output/state_printer.cpp b/solvitaire/src/main/input-output/output/state_printer.cpp
new file mode 100644
index 0000000..3f1978b
--- /dev/null
+++ b/solvitaire/src/main/input-output/output/state_printer.cpp
@@ -0,0 +1,194 @@
+//
+// Created by thecharlesblake on 1/3/18.
+//
+
+#include "state_printer.h"
+
+using std::ostream;
+using std::vector;
+using std::list;
+
+typedef sol_rules::stock_deal_type sdt;
+
+ostream& state_printer::print(ostream& stream, const game_state& gs) {
+ if (gs.rules.foundations_present) {
+ 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.original_cells, gs);
+ }
+ if (gs.rules.tableau_pile_count > 0) {
+ state_printer::print_header(stream, "Tableau Piles");
+ state_printer::print_piles(stream, gs.original_tableau_piles, gs);
+ }
+ if (gs.rules.reserve_size > 0) {
+ 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.original_reserve, gs);
+ }
+ if (gs.rules.stock_size > 0) {
+ 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");
+ state_printer::print_top_of_pile(stream, gs.hole, gs);
+ }
+ if (gs.rules.sequence_count > 0) {
+ state_printer::print_header(stream, "Sequences");
+ state_printer::print_sequences(stream, gs.sequences, gs);
+ }
+ if (gs.rules.accordion_size > 0) {
+ state_printer::print_header(stream, "Accordion");
+ state_printer::print_accordion(stream, gs.accordion, 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::ref>& pile_rs,
+ const game_state& gs) {
+ bool empty_row = false;
+ pile::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 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 pile::ref pile_r : pile_rs) {
+ 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 << "[]";
+ }
+ stream << "\t";
+ }
+
+ stream << "\n";
+ row_idx++;
+ }
+ }
+}
+
+void state_printer::print_sequences(ostream& stream,
+ const vector<pile::ref>& seq_rs,
+ const game_state& gs) {
+ for (auto s : seq_rs) {
+ stream << "\n";
+ auto& pile = gs.piles[s];
+ for (auto i = pile.size(); i --> 0 ;) {
+ card c = pile[i];
+ if (c == "AS")
+ stream << "__";
+ else
+ print_card(stream, pile[i]);
+ stream << " ";
+ }
+ stream << "\n";
+ }
+ stream << "\n";
+}
+
+void state_printer::print_top_of_piles(ostream& stream,
+ const vector<pile::ref>& vp,
+ const game_state& gs) {
+ vector<pile::ref> top(vp);
+
+ for (pile::ref p : top) {
+ if (gs.piles[p].empty()) {
+ stream << "[]";
+ } else {
+ print_card(stream, gs.piles[p].top_card());
+ }
+ stream << "\t";
+ }
+ stream << "\n";
+}
+
+void state_printer::print_accordion(ostream& stream,
+ const list<pile::ref>& vp,
+ const game_state& gs) {
+ for (pile::ref p : vp) {
+ print_card(stream, gs.piles[p].top_card());
+ stream << "\t";
+ }
+ stream << "\n";
+}
+
+void state_printer::print_top_of_pile(ostream& stream,
+ const pile::ref pile_r,
+ const game_state& gs) {
+ print_top_of_piles(stream, {pile_r}, gs);
+}
+
+void state_printer::print_card(std::ostream& s, const card c) {
+ s << c.to_string();
+}
+
+void state_printer::print_move(std::ostream& s, const move m) {
+ s << "move(";
+ switch (m.type) {
+ case move::mtype::regular:
+ s << "regular";
+ break;
+ case move::mtype::dominance:
+ s << "dominance";
+ break;
+ case move::mtype::built_group:
+ s << "built group";
+ break;
+ case move::mtype::stock_k_plus:
+ s << "stock k-plus";
+ break;
+ case move::mtype::stock_to_all_tableau:
+ s << "stock-to-all-tableau";
+ break;
+ case move::mtype::sequence:
+ s << "sequence";
+ break;
+ case move::mtype::accordion:
+ s << "accordion";
+ break;
+ case move::mtype::null:
+ s << "null";
+ break;
+ }
+ s << ", "
+ << int(m.from)
+ << ", "
+ << int(m.to)
+ << ", "
+ << int(m.count)
+ << ", "
+ << (m.reveal_move ? "true" : "false")
+ << ")\n";
+}
diff --git a/solvitaire/src/main/input-output/output/state_printer.h b/solvitaire/src/main/input-output/output/state_printer.h
new file mode 100644
index 0000000..575b973
--- /dev/null
+++ b/solvitaire/src/main/input-output/output/state_printer.h
@@ -0,0 +1,35 @@
+//
+// Created by thecharlesblake on 1/3/18.
+//
+
+#ifndef SOLVITAIRE_STATE_PRINT_H
+#define SOLVITAIRE_STATE_PRINT_H
+
+#include <ostream>
+
+#include "../../game/search-state/game_state.h"
+
+class state_printer {
+public:
+ static std::ostream& print(std::ostream&, const game_state&);
+ static void print_move(std::ostream&, move);
+
+private:
+ static void print_header(std::ostream&, const char*);
+ static void print_piles(std::ostream&,
+ const std::vector<pile::ref>&,
+ const game_state&);
+ static void print_sequences(std::ostream&,
+ const std::vector<pile::ref>&,
+ const game_state&);
+ static void print_top_of_piles(std::ostream&,
+ const std::vector<pile::ref>&,
+ const game_state&);
+ static void print_top_of_pile(std::ostream&, pile::ref,
+ const game_state&);
+ static void print_accordion(std::ostream&, const std::list<pile::ref>&,
+ const game_state&);
+ static void print_card(std::ostream&, card);
+};
+
+#endif //SOLVITAIRE_STATE_PRINT_H