aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/game/game_state.cpp2
-rw-r--r--src/main/game/sol_rules.cpp239
-rw-r--r--src/main/game/sol_rules.h13
-rw-r--r--src/main/input-output/command_line_helper.cpp55
-rw-r--r--src/main/input-output/command_line_helper.h4
-rw-r--r--src/main/input-output/sol_preset_types.cpp152
-rw-r--r--src/main/input-output/sol_preset_types.h19
-rw-r--r--src/main/main.cpp27
-rw-r--r--src/main/util/util.cpp23
-rw-r--r--src/main/util/util.h18
10 files changed, 436 insertions, 116 deletions
diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp
index 3336020..3e8d129 100644
--- a/src/main/game/game_state.cpp
+++ b/src/main/game/game_state.cpp
@@ -24,7 +24,7 @@ game_state::game_state(const Document& doc)
stock(true, ord::NO_BUILD, pol::ANY_SUIT, false),
waste(true, ord::NO_BUILD, pol::ANY_SUIT, false),
hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank),
- rules("simple-black-hole") {
+ rules(sol_rules::from_preset("simple-black-hole")) {
// Construct tableau piles
assert(doc.HasMember("tableau piles"));
const Value& json_tab_piles = doc["tableau piles"];
diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp
index 003528c..d2b0253 100644
--- a/src/main/game/sol_rules.cpp
+++ b/src/main/game/sol_rules.cpp
@@ -5,11 +5,17 @@
#include <string>
#include <vector>
#include <algorithm>
-#include <assert.h>
+#include <input-output/log_helper.h>
+#include "rapidjson/error/en.h"
+
+#include "util/util.h"
#include "sol_rules.h"
+#include "input-output/sol_preset_types.h"
using namespace std;
+using namespace boost;
+using namespace rapidjson;
////////////
// Static //
@@ -17,17 +23,6 @@ using namespace std;
typedef sol_rules::build_policy pol;
-vector<string> sol_rules::valid_sol_strs = {
- "black-hole",
- "simple-black-hole",
- "spanish-patience",
- "simple-spanish-patience",
- "free-cell",
- "simple-free-cell",
- "canfield",
- "simple-canfield"
-};
-
bool sol_rules::is_suit(pol bp) {
return suit_val(bp) != -1;
}
@@ -41,82 +36,156 @@ int sol_rules::suit_val(pol bp) {
}
}
-////////////////
-// Non-static //
-////////////////
-
-sol_rules::sol_rules(std::string sol_type) {
- valid_sol vs = valid_sol_enum(sol_type);
-
- // Default game values
-
- // tableau_pile_count = no obvious default
- build_ord = build_order::DESCENDING;
- build_pol = build_policy::ANY_SUIT;
- max_rank = 13;
- hole = false;
- foundations = true;
- cells = 0;
- reserve_size = 0;
- stock_size = 0;
-
- // Game-specific values
- switch(vs) {
- case valid_sol::BLACK_HOLE:
- tableau_pile_count = 17;
- build_ord = build_order::NO_BUILD;
- hole = true;
- foundations = false;
- cells = 0;
- break;
- case valid_sol::SIMPLE_BLACK_HOLE:
- tableau_pile_count = 9;
- build_ord = build_order::NO_BUILD;
- max_rank = 7;
- hole = true;
- foundations = false;
- break;
- case valid_sol::SPANISH_PATIENCE:
- tableau_pile_count = 13;
- break;
- case valid_sol::SIMPLE_SPANISH_PATIENCE:
- tableau_pile_count = 5;
- max_rank = 5;
- break;
- case valid_sol::FREE_CELL:
- tableau_pile_count = 8;
- cells = 4;
- break;
- case valid_sol::SIMPLE_FREE_CELL:
- tableau_pile_count = 3;
- max_rank = 3;
- cells = 1;
- break;
- case valid_sol::CANFIELD:
- tableau_pile_count = 4;
- build_pol = build_policy::RED_BLACK;
- reserve_size = 14;
- stock_size = 34;
- break;
- case valid_sol::SIMPLE_CANFIELD:
- tableau_pile_count = 2;
- build_pol = build_policy::RED_BLACK;
- max_rank = 2;
- reserve_size = 2;
- stock_size = 2;
- break;
- default:
- assert(false);
+const sol_rules sol_rules::from_file(const string rules_file) {
+ sol_rules sr = get_default();
+
+ const optional<string> file_contents = util::read_file(rules_file);
+ if (!file_contents) {
+ throw runtime_error("Could not read rules file");
+ }
+
+ Document d;
+ if (d.Parse(file_contents->c_str()).HasParseError()) {
+ string errmsg = "Error parsing rules file JSON: ";
+ errmsg += GetParseError_En(d.GetParseError());
+ throw runtime_error(errmsg);
}
+
+ modify_sol_rules(sr, d);
+
+ return sr;
+}
+
+const sol_rules sol_rules::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 sol_rules::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;
}
-sol_rules::valid_sol sol_rules::valid_sol_enum(const std::string& s) {
- auto sol_type_it =
- find(valid_sol_strs.begin(), valid_sol_strs.end(), s);
+void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) {
- assert(sol_type_it != valid_sol_strs.end());
+ if (!d.IsObject()) {
+ 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 = d["tableau piles"]["count"].GetInt();
+ } else {
+ parse_err("[tableau piles][count] must be an integer");
+ }
+ }
+
+ if (d["tableau piles"].HasMember("build order")) {
+ if (d["tableau piles"]["build order"].IsString()) {
+ string bo_str = d["tableau piles"]["build order"].GetString();
+
+ if (bo_str == "no-build") {
+ sr.build_ord = build_order::NO_BUILD;
+ } else if (bo_str == "descending") {
+ sr.build_ord = build_order::DESCENDING;
+ } else {
+ parse_err("[tableau piles][build order] is invalid");
+ }
+
+ } else {
+ parse_err("[tableau piles][build order] must be an string");
+ }
+ }
+
+ if (d["tableau piles"].HasMember("build policy")) {
+ if (d["tableau piles"]["build policy"].IsString()) {
+ string bo_str = d["tableau piles"]["build policy"].GetString();
+
+ if (bo_str == "any-suit") {
+ sr.build_pol = build_policy::ANY_SUIT;
+ } else if (bo_str == "red-black") {
+ sr.build_pol = build_policy::RED_BLACK;
+ } else {
+ parse_err("[tableau piles][build policy] is invalid");
+ }
+
+ } else {
+ parse_err("[tableau piles][build policy] must be an string");
+ }
+ }
+
+ } else {
+ parse_err("[tableau piles] must be an object");
+ }
+ }
+
+ if (d.HasMember("max rank")) {
+ if (d["max rank"].IsInt()) {
+ sr.max_rank = d["max rank"].GetInt();
+ } else {
+ parse_err("[max rank] must be an integer");
+ }
+ }
+
+ if (d.HasMember("hole")) {
+ if (d["hole"].IsBool()) {
+ sr.hole = d["hole"].GetBool();
+ } else {
+ parse_err("[hole] must be a boolean");
+ }
+ }
+
+ if (d.HasMember("foundations")) {
+ if (d["foundations"].IsBool()) {
+ sr.foundations = d["foundations"].GetBool();
+ } else {
+ parse_err("[foundations] must be a boolean");
+ }
+ }
+
+ if (d.HasMember("cells")) {
+ if (d["cells"].IsInt()) {
+ sr.cells = d["cells"].GetInt();
+ } else {
+ parse_err("[cells] must be an integer");
+ }
+ }
+
+ if (d.HasMember("reserve size")) {
+ if (d["reserve size"].IsInt()) {
+ sr.reserve_size = d["reserve size"].GetInt();
+ } else {
+ parse_err("[reserve size] must be an integer");
+ }
+ }
- long idx = sol_type_it - valid_sol_strs.begin();
+ if (d.HasMember("stock size")) {
+ if (d["stock size"].IsInt()) {
+ sr.stock_size = d["stock size"].GetInt();
+ } else {
+ parse_err("[stock size] must be an integer");
+ }
+ }
+}
- return sol_rules::valid_sol(idx);
-} \ No newline at end of file
+void sol_rules::parse_err(const string& msg) {
+ throw runtime_error("Error in JSON doc: " + msg);
+}
diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h
index b73097d..17dd74a 100644
--- a/src/main/game/sol_rules.h
+++ b/src/main/game/sol_rules.h
@@ -8,6 +8,9 @@
#include <vector>
#include <string>
+#include <boost/optional.hpp>
+#include <rapidjson/document.h>
+
class sol_rules {
public:
enum class valid_sol {
@@ -37,12 +40,12 @@ public:
RED_BLACK
};
- static std::vector<std::string> valid_sol_strs;
+ static const sol_rules from_preset(const std::string);
+ static const sol_rules from_file(const std::string);
+
static bool is_suit(build_policy);
static int suit_val(build_policy);
- explicit sol_rules(std::string);
-
unsigned int tableau_pile_count;
build_order build_ord;
build_policy build_pol;
@@ -54,6 +57,10 @@ public:
unsigned int stock_size;
private:
+ static sol_rules get_default();
+ static void modify_sol_rules(sol_rules&, rapidjson::Document&);
+ static void parse_err(const std::string&);
+
static valid_sol valid_sol_enum(const std::string&);
};
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
diff --git a/src/main/main.cpp b/src/main/main.cpp
index d748c80..1d110ef 100644
--- a/src/main/main.cpp
+++ b/src/main/main.cpp
@@ -15,6 +15,7 @@ using namespace boost;
namespace po = boost::program_options;
+const optional<sol_rules> gen_rules(command_line_helper&);
void solve_random_game(int, const sol_rules&);
void solve_input_files(const vector<string>, const sol_rules);
void solve_game(const game_state&, const sol_rules&);
@@ -23,11 +24,12 @@ int main(int argc, const char* argv[]) {
// Parses the command-line options
command_line_helper clh;
if (!clh.parse(argc, argv)) {
- return 1;
+ return EXIT_FAILURE;
}
// Generates the rules of the solitaire from the game type
- const sol_rules rules(clh.get_solitaire_type());
+ const optional<sol_rules> rules = gen_rules(clh);
+ if (!rules) return EXIT_FAILURE;
// Retrieves the input files to be solved
const vector<string> input_files = clh.get_input_files();
@@ -35,14 +37,29 @@ int main(int argc, const char* argv[]) {
// If there are no input files, solve a random deal based on the
// supplied seed
if (input_files.empty()) {
- solve_random_game(clh.get_random_deal(), rules);
+ solve_random_game(clh.get_random_deal(), *rules);
}
// Otherwise, solve the input files
else {
- solve_input_files(input_files, rules);
+ solve_input_files(input_files, *rules);
}
- return 0;
+ return EXIT_SUCCESS;
+}
+
+const optional<sol_rules> gen_rules(command_line_helper& clh) {
+ try {
+ if (!clh.get_solitaire_type().empty()) {
+ return sol_rules::from_preset(clh.get_solitaire_type());
+ } else {
+ return sol_rules::from_file(clh.get_rules_file());
+ }
+ } catch (const runtime_error& error) {
+ string errmsg = "Error in rules generation: ";
+ errmsg += error.what();
+ LOG_ERROR(errmsg);
+ return none;
+ }
}
void solve_random_game(int seed, const sol_rules& rules) {
diff --git a/src/main/util/util.cpp b/src/main/util/util.cpp
new file mode 100644
index 0000000..46038aa
--- /dev/null
+++ b/src/main/util/util.cpp
@@ -0,0 +1,23 @@
+//
+// Created by thecharlesblake on 11/13/17.
+//
+
+#include <fstream>
+#include <sstream>
+
+#include "util.h"
+
+using namespace std;
+using namespace boost;
+
+const optional<string> util::read_file(const string& filename) {
+ ifstream ifstr(filename);
+ stringstream buf;
+ buf << ifstr.rdbuf();
+
+ if (ifstr.fail()) {
+ return none;
+ } else {
+ return buf.str();
+ }
+}
diff --git a/src/main/util/util.h b/src/main/util/util.h
new file mode 100644
index 0000000..1721daa
--- /dev/null
+++ b/src/main/util/util.h
@@ -0,0 +1,18 @@
+//
+// Created by thecharlesblake on 11/13/17.
+//
+
+#ifndef SOLVITAIRE_UTIL_H
+#define SOLVITAIRE_UTIL_H
+
+#include <string>
+
+#include <boost/optional.hpp>
+
+class util {
+public:
+ static const boost::optional<std::string> read_file(const std::string&);
+};
+
+
+#endif //SOLVITAIRE_UTIL_H