diff options
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/card.cpp | 42 | ||||
| -rw-r--r-- | src/main/card.h | 3 | ||||
| -rw-r--r-- | src/main/command_line_helper.cpp | 11 | ||||
| -rw-r--r-- | src/main/command_line_helper.h | 4 | ||||
| -rw-r--r-- | src/main/game_state.cpp | 35 | ||||
| -rw-r--r-- | src/main/game_state.h | 1 | ||||
| -rw-r--r-- | src/main/log_helper.h | 8 | ||||
| -rw-r--r-- | src/main/main.cpp | 10 | ||||
| -rw-r--r-- | src/main/solver.cpp | 2 |
9 files changed, 98 insertions, 18 deletions
diff --git a/src/main/card.cpp b/src/main/card.cpp index 45de1d1..0b7c0b8 100644 --- a/src/main/card.cpp +++ b/src/main/card.cpp @@ -2,29 +2,53 @@ // Created by thecharlesblake on 10/11/17. // -#include <ctype.h> #include <sstream> +#include <ctype.h> #include "card.h" using namespace std; -card::card() {} +card::card() : rank(-1), suit(-1) {} -card::card(const char* c) { +card::card(int r, int s) : rank(r), suit(s) {} + +card::card(const char* c) : rank(-1), suit(-1) { + bool letter = false; switch(tolower(c[0])) { case 'a': rank = 1; + letter = true; break; case 'j': rank = 11; + letter = true; break; case 'q': rank = 12; + letter = true; break; case 'k': rank = 13; + letter = true; break; - default: rank = c[0] - '0'; + default: rank = atoi(c); + } + + char s; + if (rank >= 10 && !letter) { + s = c[2]; + } else { + s = c[1]; } - suit = c[1]; + s = tolower(s); + + if (s == 's') { + suit = 0; + } else if (s == 'h') { + suit = 1; + } else if (s == 'd') { + suit = 2; + } else if (s == 'c') { + suit = 3; + } } int card::get_rank() const { @@ -32,7 +56,13 @@ int card::get_rank() const { } char card::get_suit() const { - return suit; + switch (suit) { + case 0: return 'S'; + case 1: return 'H'; + case 2: return 'D'; + case 3: return 'C'; + default: return 'X'; + } } std::ostream & operator<<(std::ostream & s, card const & c) { diff --git a/src/main/card.h b/src/main/card.h index 2d9635f..13507f0 100644 --- a/src/main/card.h +++ b/src/main/card.h @@ -10,13 +10,14 @@ class card { public: card(); + card(int, int); card(const char*); int get_rank() const; char get_suit() const; private: int rank; - char suit; + int suit; }; std::ostream & operator<<(std::ostream &, card const &); diff --git a/src/main/command_line_helper.cpp b/src/main/command_line_helper.cpp index 9e389fd..67bf7ff 100644 --- a/src/main/command_line_helper.cpp +++ b/src/main/command_line_helper.cpp @@ -15,13 +15,14 @@ using namespace std; namespace po = boost::program_options; command_line_helper::command_line_helper() - : main_options("options") { + : main_options("options"), random_deal(-1) { main_options.add_options() ("help", "produce help message") ("type", po::value<string>(), "specify the type of the solitaire game to be solved (REQUIRED)") - ("random", "create and solve a random solitaire deal"); + ("random", po::value<int>(), "create and solve a random solitaire " + "deal based on a seed"); po::options_description hidden_options("Hidden options"); hidden_options.add_options() @@ -55,7 +56,9 @@ bool command_line_helper::parse(int argc, const char* argv[]) { solitaire_type = vm["type"].as<string>(); } - random_deal = vm.count("random"); + if (vm.count("random")) { + random_deal = vm["random"].as<int>(); + } // Handle logic error scenarios return assess_errors(); @@ -114,7 +117,7 @@ const string command_line_helper::get_solitaire_type() { return solitaire_type; } -bool command_line_helper::get_random_deal() { +int command_line_helper::get_random_deal() { return random_deal; } diff --git a/src/main/command_line_helper.h b/src/main/command_line_helper.h index 3454bfb..97c522a 100644 --- a/src/main/command_line_helper.h +++ b/src/main/command_line_helper.h @@ -13,7 +13,7 @@ public: bool parse(int argc, const char* argv[]); const std::vector<std::string> get_input_files(); const std::string get_solitaire_type(); - bool get_random_deal(); + int get_random_deal(); bool get_help(); private: bool assess_errors(); @@ -28,7 +28,7 @@ private: std::vector<std::string> input_files; std::string solitaire_type; - bool random_deal; + int random_deal; bool help; }; diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index 0e4f210..527f080 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -4,6 +4,9 @@ #include <vector> #include <ostream> +#include <numeric> +#include <random> +#include <algorithm> #include <rapidjson/document.h> @@ -38,6 +41,38 @@ game_state::game_state(const Document& doc) : max_rank(1) { hole_card = json_h_card.GetString(); } +// Construct an initial game state from a seed +game_state::game_state(int seed) : max_rank(7) { + int start = 1; // Ignore the first (hole) card + int end = max_rank * 4; + + // Create a vector of pointers to ints from [start, end) + vector<int*> v; + for (int i = start; i < end; i++) { + v.push_back(new int(i)); + } + + // Randomly shuffle the pointers + auto rng = std::default_random_engine(seed); + std::shuffle(std::begin(v), std::end(v), rng); + + for (vector<int*>::size_type i = 0; i < v.size(); i++) { + int r = ((*v[i]) % max_rank); + int s = (*v[i]) / max_rank; + + // Add the randomly generated card to the tableau piles + if (i % 3 == 0) tableau_piles.push_back(vector<card>()); + tableau_piles[i / 3].push_back(card(r + 1, s)); + } + + hole_card = "AS"; + + // release memory + for (int i = start; i < max_rank; i++) { + delete v[i]; + } +} + vector<game_state> game_state::get_next_legal_states() const { vector<game_state> next; diff --git a/src/main/game_state.h b/src/main/game_state.h index 3d0d38f..d7a396f 100644 --- a/src/main/game_state.h +++ b/src/main/game_state.h @@ -15,6 +15,7 @@ class game_state { public: game_state(const rapidjson::Document&); + game_state(int seed); std::vector<game_state> get_next_legal_states() const; bool is_solved() const; std::ostream& print(std::ostream&) const; diff --git a/src/main/log_helper.h b/src/main/log_helper.h index 9ac1351..889a5ae 100644 --- a/src/main/log_helper.h +++ b/src/main/log_helper.h @@ -10,10 +10,10 @@ #if defined NDEBUG #define LOG_DEBUG #else - #define LOG_DEBUG clog << "[debug]\n" + #define LOG_DEBUG(msg) clog << "[debug]\n" << msg #endif -#define LOG_INFO(msg) clog << "[info]\n" -#define LOG_WARNING(msg) clog << "[warning]\n" -#define LOG_ERROR(msg) cerr << "[error]\n" +#define LOG_INFO(msg) clog << "[info]\n" << msg +#define LOG_WARNING(msg) clog << "[warning]\n" << msg +#define LOG_ERROR(msg) cerr << "[error]\n" << msg #endif //SOLVITAIRE_LOG_HELPER_H diff --git a/src/main/main.cpp b/src/main/main.cpp index a7e5871..ae5e00b 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -22,6 +22,16 @@ int main(int argc, const char* argv[]) { return 1; } + int seed = clh.get_random_deal(); + if (seed >= 0) { + game_state gs(seed); + solver sol(gs); + + cout << "Attempting to solve with seed: " << seed << "...\n"; + sol.run(); + cout << sol; + } + for (auto input_json : clh.get_input_files()) { try { // Attempts to read the user's json into a document diff --git a/src/main/solver.cpp b/src/main/solver.cpp index 7e7155a..396a9dc 100644 --- a/src/main/solver.cpp +++ b/src/main/solver.cpp @@ -29,7 +29,7 @@ bool solver::run() { node current = frontier.back(); frontier.pop_back(); - LOG_DEBUG << current.state; + LOG_DEBUG(current.state); // Create new nodes for each of the children vector<game_state> new_children = current.state.get_next_legal_states(); |
