diff options
Diffstat (limited to 'src/main/input-output')
| -rw-r--r-- | src/main/input-output/command_line_helper.cpp | 55 | ||||
| -rw-r--r-- | src/main/input-output/command_line_helper.h | 4 | ||||
| -rw-r--r-- | src/main/input-output/sol_preset_types.cpp | 152 | ||||
| -rw-r--r-- | src/main/input-output/sol_preset_types.h | 19 |
4 files changed, 208 insertions, 22 deletions
diff --git a/src/main/input-output/command_line_helper.cpp b/src/main/input-output/command_line_helper.cpp index e61c546..49bb300 100644 --- a/src/main/input-output/command_line_helper.cpp +++ b/src/main/input-output/command_line_helper.cpp @@ -6,6 +6,7 @@ #include <vector> #include <boost/program_options.hpp> +#include <input-output/sol_preset_types.h> #include "command_line_helper.h" #include "game/sol_rules.h" @@ -21,9 +22,14 @@ command_line_helper::command_line_helper() main_options.add_options() ("help", "produce help message") ("type", po::value<string>(), - "specify the type of the solitaire game to be solved (REQUIRED)") + "specify the type of the solitaire game to be solved. Must supply " + "either 'type' or 'rules' file") + ("rules", po::value<string>(), + "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<int>(), "create and solve a random solitaire " - "deal based on a seed"); + "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() @@ -62,6 +68,10 @@ bool command_line_helper::parse(int argc, const char* argv[]) { solitaire_type = vm["type"].as<string>(); } + if (vm.count("rules")) { + rules_file = vm["rules"].as<string>(); + } + if (vm.count("random")) { random_deal = vm["random"].as<int>(); } else { @@ -78,14 +88,6 @@ bool command_line_helper::assess_errors() { return false; } - if (solitaire_type.empty()) { - print_no_sol_type_error(); - return false; - } - - bool valid_sol_type = assess_sol_type(); - if (!valid_sol_type) return false; - // The user must either supply input files or a random seed if (random_deal != -1 && !input_files.empty()) { @@ -97,22 +99,28 @@ bool command_line_helper::assess_errors() { print_no_input_error(); return false; } - return true; + + // 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 (find(sol_rules::valid_sol_strs.begin(), - sol_rules::valid_sol_strs.end(), - solitaire_type) != sol_rules::valid_sol_strs.end()) { + if (sol_preset_types::is_valid_preset(solitaire_type)) { return true; } else { - LOG_ERROR ("Error: Solitaire type is not valid: " << solitaire_type - << "\nValid solitaire types are: "); - copy(begin(sol_rules::valid_sol_strs), end(sol_rules::valid_sol_strs), - ostream_iterator<string>(cerr, ", ")); - cerr << "\n"; + LOG_ERROR ("Error: Solitaire type is not valid: " << solitaire_type); print_help(); return false; } @@ -129,8 +137,9 @@ void command_line_helper::print_no_input_error() { print_help(); } -void command_line_helper::print_no_sol_type_error() { - LOG_ERROR ("Error: User must supply a solitaire type"); +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(); } @@ -148,6 +157,10 @@ 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; } diff --git a/src/main/input-output/command_line_helper.h b/src/main/input-output/command_line_helper.h index bb2f698..fd1468c 100644 --- a/src/main/input-output/command_line_helper.h +++ b/src/main/input-output/command_line_helper.h @@ -14,6 +14,7 @@ public: 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_help(); @@ -22,7 +23,7 @@ private: bool assess_sol_type(); void print_help(); void print_no_input_error(); - void print_no_sol_type_error(); + void print_sol_type_rules_error(); void print_rand_plus_input_err(); boost::program_options::options_description cmdline_options; @@ -31,6 +32,7 @@ private: std::vector<std::string> input_files; std::string solitaire_type; + std::string rules_file; int random_deal; bool help; }; diff --git a/src/main/input-output/sol_preset_types.cpp b/src/main/input-output/sol_preset_types.cpp new file mode 100644 index 0000000..bd98893 --- /dev/null +++ b/src/main/input-output/sol_preset_types.cpp @@ -0,0 +1,152 @@ +// +// 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<string, string> sol_preset_types::mp = { + { + "default", + + R"( +{ + "tableau piles": { + "count": 8, + "build order": "descending", + "build policy": "any-suit" + }, + "max rank": 13, + "hole": false, + "foundations": true, + "cells": 0, + "reserve size": 0, + "stock size": 0 +} +)" + + }, + { + "black-hole", + + R"( +{ + "tableau piles": { + "count": 17, + "build order": "no-build" + }, + "hole": true, + "foundations": false +} +)" + + }, + { + "simple-black-hole", + + R"( +{ + "tableau piles": { + "count": 9, + "build order": "no-build" + }, + "max rank": 7, + "hole": true, + "foundations": false +} +)" + + }, + { + "spanish-patience", + + R"( +{ + "tableau piles": { + "count": 13 + } +} +)" + + }, + { + "simple-spanish-patience", + + R"( +{ + "tableau piles": { + "count": 5 + }, + "max rank": 5 +} +)" + + }, + { + "free-cell", + + R"( +{ + "tableau piles": { + "build policy": "red-black" + }, + "cells": 4 +} +)" + + }, + { + "simple-free-cell", + + R"( +{ + "tableau piles": { + "count": 3, + "build policy": "red-black" + }, + "max rank": 3, + "cells": 1 +} +)" + + }, + { + "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 new file mode 100644 index 0000000..02f602d --- /dev/null +++ b/src/main/input-output/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 <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); +private: + static std::map<std::string, std::string> mp; +}; + +#endif //SOLVITAIRE_SOL_PRESET_TYPES_H |
