From d472356d4e01c72e3b51deb28f694b9d0dd4fd6e Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 15:15:49 +0100 Subject: Add rules class to represent solitaire type --- src/main/command_line_helper.cpp | 11 ++++---- src/main/command_line_helper.h | 2 -- src/main/main.cpp | 24 ++++++++++------ src/main/sol_rules.cpp | 61 ++++++++++++++++++++++++++++++++++++++++ src/main/sol_rules.h | 27 ++++++++++++++++++ 5 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 src/main/sol_rules.cpp create mode 100644 src/main/sol_rules.h (limited to 'src') diff --git a/src/main/command_line_helper.cpp b/src/main/command_line_helper.cpp index 22678af..71eaf43 100644 --- a/src/main/command_line_helper.cpp +++ b/src/main/command_line_helper.cpp @@ -9,14 +9,12 @@ #include #include "command_line_helper.h" +#include "sol_rules.h" using namespace std; namespace po = boost::program_options; -vector command_line_helper::valid_sol_types = - {"black-hole", "simple-black-hole", "spanish-patience"}; - command_line_helper::command_line_helper() : main_options("options"), random_deal(-1) { @@ -94,13 +92,14 @@ bool command_line_helper::assess_errors() { } bool command_line_helper::assess_sol_type() { - if (find(valid_sol_types.begin(), valid_sol_types.end(), solitaire_type) != - valid_sol_types.end()) { + if (find(sol_rules::valid_sol_strs.begin(), + sol_rules::valid_sol_strs.end(), + solitaire_type) != sol_rules::valid_sol_strs.end()) { return true; } else { cerr << "Error: Solitaire type is not valid: " << solitaire_type << "\nValid solitaire types are: "; - copy(begin(valid_sol_types), end(valid_sol_types), + copy(begin(sol_rules::valid_sol_strs), end(sol_rules::valid_sol_strs), ostream_iterator(cerr, ", ")); cerr << "\n"; print_help(); diff --git a/src/main/command_line_helper.h b/src/main/command_line_helper.h index 47eef4c..8cc67cb 100644 --- a/src/main/command_line_helper.h +++ b/src/main/command_line_helper.h @@ -16,8 +16,6 @@ public: int get_random_deal(); bool get_help(); private: - static std::vector valid_sol_types; - bool assess_errors(); bool assess_sol_type(); void print_help(); diff --git a/src/main/main.cpp b/src/main/main.cpp index 245b737..d39d35c 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -7,6 +7,7 @@ #include "deal_parser.h" #include "game_state.h" #include "solver.h" +#include "sol_rules.h" using namespace rapidjson; @@ -14,6 +15,8 @@ using namespace std; namespace po = boost::program_options; +void solve_game(const game_state&, const sol_rules&); + int main(int argc, const char* argv[]) { // Parses the command-line options command_line_helper clh; @@ -21,14 +24,14 @@ int main(int argc, const char* argv[]) { return 1; } + // Generates the rules of the solitaire from the game type + const sol_rules rules(clh.get_solitaire_type()); + int seed = clh.get_random_deal(); if (seed >= 0) { - game_state gs(seed, clh.get_solitaire_type()); - solver sol(gs); - cout << "Attempting to solve with seed: " << seed << "...\n"; - sol.run(); - cout << sol; + game_state gs(seed, sol_rules); + solve_game(gs, sol_rules); } for (auto input_json : clh.get_input_files()) { @@ -39,11 +42,9 @@ int main(int argc, const char* argv[]) { // Creates a game state object from the json, plus a solver game_state gs(doc, clh.get_solitaire_type()); - solver sol(gs); cout << "Attempting to solve " << input_json << "...\n"; - sol.run(); - cout << sol; + solve_game(gs, sol_rules); } catch (runtime_error& e) { cerr << "Error: " << e.what() << "\n"; @@ -52,3 +53,10 @@ int main(int argc, const char* argv[]) { return 0; } + +void solve_game(const game_state& gs, const sol_rules& rules) { + solver sol(gs, rules); + + sol.run(); + cout << sol; +} diff --git a/src/main/sol_rules.cpp b/src/main/sol_rules.cpp new file mode 100644 index 0000000..a53a8dd --- /dev/null +++ b/src/main/sol_rules.cpp @@ -0,0 +1,61 @@ +// +// Created by thecharlesblake on 10/28/17. +// + +#include +#include +#include +#include + +#include "sol_rules.h" + +using namespace std; + +enum class sol_rules::valid_sol { + BLACK_HOLE, + SIMPLE_BLACK_HOLE, + SPANISH_PATIENCE +}; + +vector sol_rules::valid_sol_strs = { + "black-hole", + "simple-black-hole", + "simple-patience" +}; + +sol_rules::sol_rules(std::string sol_type) { + valid_sol vs = valid_sol_enum(sol_type); + + hole = false; + foundations = true; + + switch(vs) { + case valid_sol::BLACK_HOLE: + tableau_pile_count = 17; + max_rank = 13; + hole = true; + foundations = false; + break; + case valid_sol::SIMPLE_BLACK_HOLE: + tableau_pile_count = 9; + max_rank = 7; + hole = true; + foundations = false; + break; + case valid_sol::SPANISH_PATIENCE: + tableau_pile_count = 13; + max_rank = 13; + break; + } +} + +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); + + assert(sol_type_it != valid_sol_strs.end()); + + int idx = sol_type_it - valid_sol_strs.begin(); + + return sol_rules::valid_sol(idx); +} \ No newline at end of file diff --git a/src/main/sol_rules.h b/src/main/sol_rules.h new file mode 100644 index 0000000..83bbfb2 --- /dev/null +++ b/src/main/sol_rules.h @@ -0,0 +1,27 @@ +// +// Created by thecharlesblake on 10/28/17. +// + +#ifndef SOLVITAIRE_SOL_RULES_H +#define SOLVITAIRE_SOL_RULES_H + +#include +#include + +struct sol_rules { + static enum class valid_sol; + static std::vector valid_sol_strs; + + sol_rules(std::string); + + int tableau_pile_count; + int max_rank; + bool hole; + bool foundations; + +private: + static valid_sol valid_sol_enum(const std::string&); +}; + + +#endif //SOLVITAIRE_SOL_RULES_H -- cgit v1.2.1