From 6939069019b91f8a36febd1640cc5124651d7093 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 13 Nov 2017 10:20:49 +0000 Subject: Add command line argument for rules file --- src/main/input-output/command_line_helper.cpp | 44 +++++++++++++++++++-------- src/main/input-output/command_line_helper.h | 4 ++- 2 files changed, 34 insertions(+), 14 deletions(-) (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 index e61c546..37908a2 100644 --- a/src/main/input-output/command_line_helper.cpp +++ b/src/main/input-output/command_line_helper.cpp @@ -21,9 +21,14 @@ command_line_helper::command_line_helper() main_options.add_options() ("help", "produce help message") ("type", po::value(), - "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(), + "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"); + "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 +67,10 @@ bool command_line_helper::parse(int argc, const char* argv[]) { solitaire_type = vm["type"].as(); } + if (vm.count("rules")) { + rules_file = vm["type"].as(); + } + if (vm.count("random")) { random_deal = vm["random"].as(); } else { @@ -78,14 +87,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,7 +98,19 @@ 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 @@ -129,8 +142,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 +162,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 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 input_files; std::string solitaire_type; + std::string rules_file; int random_deal; bool help; }; -- cgit v1.2.1 From 882afd91325d03ae924fef648d7485295a450b59 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 14 Nov 2017 01:47:55 +0000 Subject: Add rules presets to resources --- src/main/input-output/sol_preset_types.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/input-output/sol_preset_types.h (limited to 'src/main/input-output') 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 +#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 284c1ca6b22056d008b9f91a558ce3d3e944a549 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 14 Nov 2017 01:52:30 +0000 Subject: Add new valid presets logic to cmd helper --- src/main/input-output/command_line_helper.cpp | 17 +-- src/main/input-output/sol_preset_types.cpp | 152 ++++++++++++++++++++++++++ 2 files changed, 158 insertions(+), 11 deletions(-) create mode 100644 src/main/input-output/sol_preset_types.cpp (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 index 37908a2..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 #include +#include #include "command_line_helper.h" #include "game/sol_rules.h" @@ -68,7 +69,7 @@ bool command_line_helper::parse(int argc, const char* argv[]) { } if (vm.count("rules")) { - rules_file = vm["type"].as(); + rules_file = vm["rules"].as(); } if (vm.count("random")) { @@ -100,8 +101,8 @@ bool command_line_helper::assess_errors() { } // 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()) { + if ((solitaire_type.empty() && rules_file.empty()) + || (!solitaire_type.empty() && !rules_file.empty())) { print_sol_type_rules_error(); return false; } @@ -115,17 +116,11 @@ bool command_line_helper::assess_errors() { // 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(cerr, ", ")); - cerr << "\n"; + LOG_ERROR ("Error: Solitaire type is not valid: " << solitaire_type); print_help(); return false; } 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 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 -- cgit v1.2.1