From 81fdc3f73a5d89e613c8e19f385c2144b35f6204 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 12:19:20 +0100 Subject: Add Spanish Paitence to cmd parse and refactor --- src/main/command_line_helper.cpp | 20 ++++++++++++-------- src/main/command_line_helper.h | 4 +++- src/main/main.cpp | 1 - 3 files changed, 15 insertions(+), 10 deletions(-) (limited to 'src/main') diff --git a/src/main/command_line_helper.cpp b/src/main/command_line_helper.cpp index fdf6c9c..22678af 100644 --- a/src/main/command_line_helper.cpp +++ b/src/main/command_line_helper.cpp @@ -14,6 +14,9 @@ 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) { @@ -78,12 +81,12 @@ bool command_line_helper::assess_errors() { bool valid_sol_type = assess_sol_type(); if (!valid_sol_type) return false; - if (random_deal >= 0 && !input_files.empty()) { + if (random_deal != -1 && !input_files.empty()) { print_rand_plus_input_err(); return false; } - if (input_files.empty() && random_deal >= 0) { + if (input_files.empty() && random_deal == -1) { print_no_input_error(); return false; } @@ -91,21 +94,22 @@ bool command_line_helper::assess_errors() { } bool command_line_helper::assess_sol_type() { - if (solitaire_type == "black-hole") { - return true; - } else if (solitaire_type == "simple-black-hole") { + if (find(valid_sol_types.begin(), valid_sol_types.end(), solitaire_type) != + valid_sol_types.end()) { return true; } else { cerr << "Error: Solitaire type is not valid: " << solitaire_type - << "\nValid solitaire types are: 'black-hole' and " - "'simple-black-hole'"; + << "\nValid solitaire types are: "; + copy(begin(valid_sol_types), end(valid_sol_types), + ostream_iterator(cerr, ", ")); + cerr << "\n"; print_help(); return false; } } void command_line_helper::print_help() { - cout << "Usage: solvitaire [options] input-file1 input-file2 ...\n" + cerr << "Usage: solvitaire [options] input-file1 input-file2 ...\n" << main_options << "\n"; } diff --git a/src/main/command_line_helper.h b/src/main/command_line_helper.h index 5694830..47eef4c 100644 --- a/src/main/command_line_helper.h +++ b/src/main/command_line_helper.h @@ -16,13 +16,15 @@ 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(); void print_no_input_error(); void print_no_sol_type_error(); void print_rand_plus_input_err(); - + boost::program_options::options_description cmdline_options; boost::program_options::options_description main_options; boost::program_options::positional_options_description p; diff --git a/src/main/main.cpp b/src/main/main.cpp index cc1df9f..245b737 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -4,7 +4,6 @@ #include #include "command_line_helper.h" -#include "card.h" #include "deal_parser.h" #include "game_state.h" #include "solver.h" -- cgit v1.2.1 From 9c018b08ed775eda03e4507e33a20742659683ea Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 14:03:54 +0100 Subject: Refactor game generation to make more generic --- src/main/card.cpp | 4 ++++ src/main/card.h | 1 + src/main/game_state.cpp | 44 ++++++++++++++++++++++++++++---------------- src/main/game_state.h | 4 +++- 4 files changed, 36 insertions(+), 17 deletions(-) (limited to 'src/main') diff --git a/src/main/card.cpp b/src/main/card.cpp index 0b7c0b8..0510304 100644 --- a/src/main/card.cpp +++ b/src/main/card.cpp @@ -51,6 +51,10 @@ card::card(const char* c) : rank(-1), suit(-1) { } } +bool operator==(const card& l, const card& r) { + return l.get_rank() == r.get_rank() && l.get_suit() == r.get_suit(); +} + int card::get_rank() const { return rank; } diff --git a/src/main/card.h b/src/main/card.h index 13507f0..1569a5b 100644 --- a/src/main/card.h +++ b/src/main/card.h @@ -21,6 +21,7 @@ private: }; std::ostream & operator<<(std::ostream &, card const &); +bool operator==(const card&, const card&); #endif //SOLITAIRE_PIP_CARD_H diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index ef9db97..28f6d3b 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -18,7 +18,7 @@ using namespace std; // Construct an initial game state from a JSON doc game_state::game_state(const Document& doc, string sol_type) : max_rank(1) { - simple = sol_type == "simple-black-hole"; + max_rank = sol_type == "simple-black-hole" ? 7 : 13; // Construct tableau piles assert(doc.HasMember("tableau piles")); @@ -46,15 +46,27 @@ game_state::game_state(const Document& doc, string sol_type) // Construct an initial game state from a seed game_state::game_state(int seed, string sol_type) { - simple = sol_type == "simple-black-hole"; - max_rank = simple ? 7 : 13; + max_rank = sol_type == "simple-black-hole" ? 7 : 13; - int start = 1; // Ignore the first (hole) card - int end = max_rank * 4; + vector deck = shuffled_deck(seed, max_rank); - // Create a vector of pointers to ints from [start, end) + for (vector::size_type i = 0; i < deck.size(); i++) { + // Don't add the Ace of Spades to the hole card + card c = deck[i]; + if (c == "AS") continue; + + // Add the randomly generated card to the tableau piles + if (i % 3 == 0) tableau_piles.push_back(vector()); + tableau_piles[i / 3].push_back(deck[i]); + } + + hole_card = "AS"; +} + +// Generate a randomly ordered vector of cards +vector game_state::shuffled_deck(int seed, int max_rank = 13) { vector v; - for (int i = start; i < end; i++) { + for (int i = 0; i < max_rank * 4; i++) { v.push_back(new int(i)); } @@ -62,23 +74,23 @@ game_state::game_state(int seed, string sol_type) { auto rng = std::default_random_engine(seed); std::shuffle(std::begin(v), std::end(v), rng); - for (vector::size_type i = 0; i < v.size(); i++) { - int r = ((*v[i]) % max_rank); - int s = (*v[i]) / max_rank; + vector deck; + for (int *i : v) { + int r = ((*i) % max_rank) + 1; + int s = (*i) / max_rank; - // Add the randomly generated card to the tableau piles - if (i % 3 == 0) tableau_piles.push_back(vector()); - tableau_piles[i / 3].push_back(card(r + 1, s)); + deck.push_back(card(r, s)); } - hole_card = "AS"; - // release memory - for (int i = start; i < max_rank; i++) { + for (int i = 0; i < max_rank * 4; i++) { delete v[i]; } + + return deck; } + vector game_state::get_next_legal_states() const { vector next; diff --git a/src/main/game_state.h b/src/main/game_state.h index ff92247..88e8937 100644 --- a/src/main/game_state.h +++ b/src/main/game_state.h @@ -17,6 +17,7 @@ class game_state { public: game_state(const rapidjson::Document&, std::string); game_state(int seed, std::string); + std::vector get_next_legal_states() const; bool is_solved() const; std::ostream& print(std::ostream&) const; @@ -24,9 +25,10 @@ public: friend std::ostream& operator<< (std::ostream&, const game_state&); private: + static std::vector shuffled_deck(int, int); + void move_to_hole(int); bool adjacent(const card& a, const card& b) const; - bool simple; std::vector > tableau_piles; int max_rank; -- cgit v1.2.1 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/main') 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 From 6a5a840f678aa78659b51ec647dcced4544c4a0b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 15:29:17 +0100 Subject: Modify game state generation to work across sols --- src/main/game_state.cpp | 31 +++++++++++++++++-------------- src/main/game_state.h | 6 +++--- src/main/main.cpp | 2 +- 3 files changed, 21 insertions(+), 18 deletions(-) (limited to 'src/main') diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index 28f6d3b..9d0349d 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -16,15 +16,13 @@ using namespace rapidjson; using namespace std; // Construct an initial game state from a JSON doc -game_state::game_state(const Document& doc, string sol_type) - : max_rank(1) { - max_rank = sol_type == "simple-black-hole" ? 7 : 13; - +game_state::game_state(const Document& doc) { // Construct tableau piles assert(doc.HasMember("tableau piles")); const Value& json_tab_piles = doc["tableau piles"]; assert(json_tab_piles.IsArray()); + int max_rank = 1; for (auto& json_tab : json_tab_piles.GetArray()) { vector tableau_pile; @@ -45,22 +43,27 @@ game_state::game_state(const Document& doc, string sol_type) } // Construct an initial game state from a seed -game_state::game_state(int seed, string sol_type) { - max_rank = sol_type == "simple-black-hole" ? 7 : 13; - - vector deck = shuffled_deck(seed, max_rank); +game_state::game_state(int seed, const sol_rules& rules) { + vector deck = shuffled_deck(seed, rules.max_rank); + // Deal to the tableau piles (row-by-row) for (vector::size_type i = 0; i < deck.size(); i++) { - // Don't add the Ace of Spades to the hole card card c = deck[i]; - if (c == "AS") continue; + + // If there is a hole card, don't deal the ace of spades + if (rules.hole && c == "AS") { + hole_card = "AS"; + continue; + } + + // For the first row we must create all the tableau pile vectors + if (i / rules.tableau_pile_count == 0) { + tableau_piles.push_back(vector()); + } // Add the randomly generated card to the tableau piles - if (i % 3 == 0) tableau_piles.push_back(vector()); - tableau_piles[i / 3].push_back(deck[i]); + tableau_piles[i % rules.tableau_pile_count].push_back(c); } - - hole_card = "AS"; } // Generate a randomly ordered vector of cards diff --git a/src/main/game_state.h b/src/main/game_state.h index 88e8937..ebcc15f 100644 --- a/src/main/game_state.h +++ b/src/main/game_state.h @@ -12,11 +12,12 @@ #include #include "card.h" +#include "sol_rules.h" class game_state { public: - game_state(const rapidjson::Document&, std::string); - game_state(int seed, std::string); + game_state(const rapidjson::Document&); + game_state(int seed, const sol_rules&); std::vector get_next_legal_states() const; bool is_solved() const; @@ -31,7 +32,6 @@ private: bool adjacent(const card& a, const card& b) const; std::vector > tableau_piles; - int max_rank; card hole_card; mutable bool solved; }; diff --git a/src/main/main.cpp b/src/main/main.cpp index d39d35c..65730d2 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -41,7 +41,7 @@ int main(int argc, const char* argv[]) { deal_parser::parse(doc, input_json); // Creates a game state object from the json, plus a solver - game_state gs(doc, clh.get_solitaire_type()); + game_state gs(doc); cout << "Attempting to solve " << input_json << "...\n"; solve_game(gs, sol_rules); -- cgit v1.2.1 From c2e0bbad760bb95b7967d6df065c46e1aac1270d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 15:50:45 +0100 Subject: Add generic state printing --- src/main/game_state.cpp | 53 +++++++++++++++++++++++++++++++++++++++---------- src/main/game_state.h | 6 ++++++ src/main/main.cpp | 7 +++---- 3 files changed, 51 insertions(+), 15 deletions(-) (limited to 'src/main') diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index 9d0349d..45bea21 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -16,7 +16,7 @@ using namespace rapidjson; using namespace std; // Construct an initial game state from a JSON doc -game_state::game_state(const Document& doc) { +game_state::game_state(const Document& doc) : rules("simple-black-hole") { // Construct tableau piles assert(doc.HasMember("tableau piles")); const Value& json_tab_piles = doc["tableau piles"]; @@ -43,7 +43,7 @@ game_state::game_state(const Document& doc) { } // Construct an initial game state from a seed -game_state::game_state(int seed, const sol_rules& rules) { +game_state::game_state(int seed, const sol_rules& rules) : rules(rules) { vector deck = shuffled_deck(seed, rules.max_rank); // Deal to the tableau piles (row-by-row) @@ -131,8 +131,8 @@ bool game_state::adjacent(const card& a, const card& b) const { return x == y + 1 || x + 1 == y - || (x == 1 && y == max_rank) - || (x == max_rank && y == 1); + || (x == 1 && y == rules.max_rank) + || (x == rules.max_rank && y == 1); } void game_state::move_to_hole(int tab_idx) { @@ -145,6 +145,40 @@ bool game_state::is_solved() const { } ostream& game_state::print(ostream& stream) const { + const char *sep = "------------------------------\n"; + + if (rules.foundations) { + print_header(stream, "Foundations"); + print_foundations(stream); + } + if (rules.tableau_pile_count > 0) { + print_header(stream, "Tableau Piles"); + print_tableau_piles(stream); + } + if (rules.hole) { + print_header(stream, "Hole Card"); + print_hole(stream); + } + return stream << "///////////////////////////////////\n\n"; +} + +void game_state::print_header(ostream& stream, const char* header) const { + stream << "--- " << header << " "; + int pad = 20 - strlen(header); + for (int i = 0; i < pad; i++) { + stream << '-'; + } + stream << "\n"; +} + +void game_state::print_foundations(ostream& stream) const { + stream << foundations[0] << "\t" + << foundations[1] << "\t" + << foundations[2] << "\t" + << foundations[3] << "\n"; +}; + +void game_state::print_tableau_piles(ostream& stream) const { bool empty_row = false; vector::size_type row_idx = 0; @@ -159,17 +193,14 @@ ostream& game_state::print(ostream& stream) const { stream << "\t"; } - if (!empty_row) stream << "|"; - if (row_idx == 0) { - stream << " Hole card: " << hole_card; - } - stream << "\n"; row_idx++; } +}; - return stream; -} +void game_state::print_hole(ostream& stream) const { + stream << hole_card << "\n"; +}; ostream& operator <<(ostream& stream, const game_state& gs) { return gs.print(stream); diff --git a/src/main/game_state.h b/src/main/game_state.h index ebcc15f..974fd2a 100644 --- a/src/main/game_state.h +++ b/src/main/game_state.h @@ -30,8 +30,14 @@ private: void move_to_hole(int); bool adjacent(const card& a, const card& b) const; + void print_header(std::ostream&, const char*) const; + void print_foundations(std::ostream&) const; + void print_tableau_piles(std::ostream&) const; + void print_hole(std::ostream&) const; std::vector > tableau_piles; + std::vector foundations; + sol_rules rules; card hole_card; mutable bool solved; }; diff --git a/src/main/main.cpp b/src/main/main.cpp index 65730d2..5e11537 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -7,7 +7,6 @@ #include "deal_parser.h" #include "game_state.h" #include "solver.h" -#include "sol_rules.h" using namespace rapidjson; @@ -30,8 +29,8 @@ int main(int argc, const char* argv[]) { int seed = clh.get_random_deal(); if (seed >= 0) { cout << "Attempting to solve with seed: " << seed << "...\n"; - game_state gs(seed, sol_rules); - solve_game(gs, sol_rules); + game_state gs(seed, rules); + solve_game(gs, rules); } for (auto input_json : clh.get_input_files()) { @@ -44,7 +43,7 @@ int main(int argc, const char* argv[]) { game_state gs(doc); cout << "Attempting to solve " << input_json << "...\n"; - solve_game(gs, sol_rules); + solve_game(gs, rules); } catch (runtime_error& e) { cerr << "Error: " << e.what() << "\n"; -- cgit v1.2.1 From 215496d51c6663dd12c4ffb51521f00a8bf9fcbe Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 16:07:13 +0100 Subject: Add build order to game rules class --- src/main/sol_rules.cpp | 9 +++------ src/main/sol_rules.h | 12 +++++++++++- src/main/solver.cpp | 4 ++-- src/main/solver.h | 4 +++- 4 files changed, 19 insertions(+), 10 deletions(-) (limited to 'src/main') diff --git a/src/main/sol_rules.cpp b/src/main/sol_rules.cpp index a53a8dd..75c79b1 100644 --- a/src/main/sol_rules.cpp +++ b/src/main/sol_rules.cpp @@ -11,12 +11,6 @@ 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", @@ -28,16 +22,19 @@ sol_rules::sol_rules(std::string sol_type) { hole = false; foundations = true; + build_order = tab_build_order::DESCENDING; switch(vs) { case valid_sol::BLACK_HOLE: tableau_pile_count = 17; + build_order = tab_build_order::NO_BUILD; max_rank = 13; hole = true; foundations = false; break; case valid_sol::SIMPLE_BLACK_HOLE: tableau_pile_count = 9; + build_order = tab_build_order::NO_BUILD; max_rank = 7; hole = true; foundations = false; diff --git a/src/main/sol_rules.h b/src/main/sol_rules.h index 83bbfb2..56701c7 100644 --- a/src/main/sol_rules.h +++ b/src/main/sol_rules.h @@ -9,12 +9,22 @@ #include struct sol_rules { - static enum class valid_sol; + enum class valid_sol { + BLACK_HOLE, + SIMPLE_BLACK_HOLE, + SPANISH_PATIENCE + }; + enum class tab_build_order { + NO_BUILD, + DESCENDING, + }; + static std::vector valid_sol_strs; sol_rules(std::string); int tableau_pile_count; + tab_build_order build_order; int max_rank; bool hole; bool foundations; diff --git a/src/main/solver.cpp b/src/main/solver.cpp index 396a9dc..ac8ffb8 100644 --- a/src/main/solver.cpp +++ b/src/main/solver.cpp @@ -12,8 +12,8 @@ #include using namespace std; -solver::solver(const game_state gs) : root(gs), solution(gs), - solution_found(false) {} +solver::solver(const game_state gs, const sol_rules& sr) + : root(gs), rules(sr), solution(gs), solution_found(false) {} solver::node::node(const game_state gs) : state(gs) {} diff --git a/src/main/solver.h b/src/main/solver.h index 0520cc7..cff1e47 100644 --- a/src/main/solver.h +++ b/src/main/solver.h @@ -11,10 +11,11 @@ #include #include "game_state.h" +#include "sol_rules.h" class solver { public: - solver(const game_state); + solver(const game_state, const sol_rules&); bool run(); friend std::ostream& operator<< (std::ostream&, const solver&); @@ -26,6 +27,7 @@ private: game_state state; }; + const sol_rules rules; const node root; bool been_run; node solution; -- cgit v1.2.1 From c3e6f892599f145c14317e5712263b3e5562b785 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 22:21:03 +0100 Subject: Add generic solving and pile class --- src/main/card.cpp | 20 +++++---- src/main/card.h | 1 + src/main/game_state.cpp | 115 ++++++++++++++++++++++++++++++------------------ src/main/game_state.h | 8 ++-- src/main/pile.cpp | 92 ++++++++++++++++++++++++++++++++++++++ src/main/pile.h | 36 +++++++++++++++ src/main/sol_rules.cpp | 6 +-- src/main/sol_rules.h | 15 +++++-- src/main/solver.cpp | 11 ++++- 9 files changed, 243 insertions(+), 61 deletions(-) create mode 100644 src/main/pile.cpp create mode 100644 src/main/pile.h (limited to 'src/main') diff --git a/src/main/card.cpp b/src/main/card.cpp index 0510304..583814d 100644 --- a/src/main/card.cpp +++ b/src/main/card.cpp @@ -40,13 +40,13 @@ card::card(const char* c) : rank(-1), suit(-1) { s = tolower(s); - if (s == 's') { + if (s == 'c') { suit = 0; - } else if (s == 'h') { - suit = 1; } else if (s == 'd') { + suit = 1; + } else if (s == 'h') { suit = 2; - } else if (s == 'c') { + } else if (s == 's') { suit = 3; } } @@ -61,14 +61,18 @@ int card::get_rank() const { char card::get_suit() const { switch (suit) { - case 0: return 'S'; - case 1: return 'H'; - case 2: return 'D'; - case 3: return 'C'; + case 0: return 'C'; + case 1: return 'D'; + case 2: return 'H'; + case 3: return 'S'; default: return 'X'; } } +int card::get_suit_val() const { + return suit; +} + std::ostream & operator<<(std::ostream & s, card const & c) { std::string r; if (c.get_rank() == 1) { diff --git a/src/main/card.h b/src/main/card.h index 1569a5b..7c28a28 100644 --- a/src/main/card.h +++ b/src/main/card.h @@ -14,6 +14,7 @@ public: card(const char*); int get_rank() const; char get_suit() const; + int get_suit_val() const; private: int rank; diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index 45bea21..93ecb02 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -7,16 +7,23 @@ #include #include #include +#include #include #include "game_state.h" +#include "pile.h" using namespace rapidjson; using namespace std; +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; + // Construct an initial game state from a JSON doc -game_state::game_state(const Document& doc) : rules("simple-black-hole") { +game_state::game_state(const Document& doc) + : rules("simple-black-hole"), + hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank) { // Construct tableau piles assert(doc.HasMember("tableau piles")); const Value& json_tab_piles = doc["tableau piles"]; @@ -24,12 +31,12 @@ game_state::game_state(const Document& doc) : rules("simple-black-hole") { int max_rank = 1; for (auto& json_tab : json_tab_piles.GetArray()) { - vector tableau_pile; + pile tableau_pile(true, rules.build_order, pol::ANY_SUIT, false); for (auto& json_card : json_tab.GetArray()) { card c(json_card.GetString()); if (c.get_rank() > max_rank) max_rank = c.get_rank(); - tableau_pile.push_back(c); + tableau_pile.place(c); } tableau_piles.push_back(tableau_pile); @@ -39,11 +46,13 @@ game_state::game_state(const Document& doc) : rules("simple-black-hole") { assert(doc.HasMember("hole card")); const Value& json_h_card = doc["hole card"]; assert(json_h_card.IsString()); - hole_card = json_h_card.GetString(); + hole.place(json_h_card.GetString()); } // Construct an initial game state from a seed -game_state::game_state(int seed, const sol_rules& rules) : rules(rules) { +game_state::game_state(int seed, const sol_rules& rules) + : rules(rules), + hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank) { vector deck = shuffled_deck(seed, rules.max_rank); // Deal to the tableau piles (row-by-row) @@ -52,17 +61,26 @@ game_state::game_state(int seed, const sol_rules& rules) : rules(rules) { // If there is a hole card, don't deal the ace of spades if (rules.hole && c == "AS") { - hole_card = "AS"; + hole.place("AS"); continue; } // For the first row we must create all the tableau pile vectors if (i / rules.tableau_pile_count == 0) { - tableau_piles.push_back(vector()); + pile p(true, rules.build_order, pol::ANY_SUIT, false); + tableau_piles.push_back(p); } // Add the randomly generated card to the tableau piles - tableau_piles[i % rules.tableau_pile_count].push_back(c); + tableau_piles[i % rules.tableau_pile_count].place(c); + } + + // If there are foundation piles, create the relevant pile vectors + if (rules.foundations) { + for (int i = 0; i < 4; i++) { + pile p(true, ord::ASCENDING, pol(i), false); + foundations.push_back(p); + } } } @@ -93,38 +111,51 @@ vector game_state::shuffled_deck(int seed, int max_rank = 13) { return deck; } - vector game_state::get_next_legal_states() const { + // Generates vectors of references to all the piles from which + // cards can be removed, and to which they can be added + vector can_remove; + vector can_add; + + for (pile p : tableau_piles) { + can_remove.push_back(p); + if (rules.build_order != ord::NO_BUILD) { + can_add.push_back(p); + } + } + if (rules.foundations) { + for (pile p : foundations) { + can_remove.push_back(p); + can_add.push_back(p); + } + } + if (rules.hole) { + can_add.push_back(hole); + } + + // The next legal states vector next; - // Searches through each column for a top card that can be moved to the hole - bool all_tableaux_empty = true; - for (vector>::size_type i = 0; i < tableau_piles.size(); i++) { - vector tableau_pile = tableau_piles[i]; + for (pile &rem_pile : can_remove) { + if (rem_pile.empty()) continue; - // Ignore empty piles - if (tableau_pile.empty()) { - continue; - } else { - all_tableaux_empty = false; - } - card top_card = tableau_pile.back(); - - // If a card at the top of a tableau pile is adjacent to the hole card, - // move it to the hole and add that state - if (adjacent(top_card, hole_card)) { - game_state s = *this; - s.move_to_hole(i); - next.push_back(s); + for (pile &add_pile : can_add) { + if (&add_pile != &rem_pile + && add_pile.can_place(rem_pile.top_card())) { + + move(rem_pile, add_pile); // remove + game_state s = *this; + next.push_back(s); + move(add_pile, rem_pile); // restore + } } } - // If there are no cards left in play, deal solved - solved = all_tableaux_empty; - return next; } + + bool game_state::adjacent(const card& a, const card& b) const { int x = a.get_rank(); int y = b.get_rank(); @@ -135,13 +166,13 @@ bool game_state::adjacent(const card& a, const card& b) const { || (x == rules.max_rank && y == 1); } -void game_state::move_to_hole(int tab_idx) { - hole_card = tableau_piles[tab_idx].back(); - tableau_piles[tab_idx].pop_back(); -} - bool game_state::is_solved() const { - return solved; + for (auto p : tableau_piles) { + if (!p.empty()) { + return false; + } + } + return true; } ostream& game_state::print(ostream& stream) const { @@ -172,10 +203,10 @@ void game_state::print_header(ostream& stream, const char* header) const { } void game_state::print_foundations(ostream& stream) const { - stream << foundations[0] << "\t" - << foundations[1] << "\t" - << foundations[2] << "\t" - << foundations[3] << "\n"; + stream << foundations[0].top_card() << "\t" + << foundations[1].top_card() << "\t" + << foundations[2].top_card() << "\t" + << foundations[3].top_card() << "\n"; }; void game_state::print_tableau_piles(ostream& stream) const { @@ -185,7 +216,7 @@ void game_state::print_tableau_piles(ostream& stream) const { while (!empty_row) { empty_row = true; - for (vector tableau_pile : tableau_piles) { + for (pile tableau_pile : tableau_piles) { if (tableau_pile.size() > row_idx) { empty_row = false; stream << tableau_pile[row_idx]; @@ -199,7 +230,7 @@ void game_state::print_tableau_piles(ostream& stream) const { }; void game_state::print_hole(ostream& stream) const { - stream << hole_card << "\n"; + stream << hole.top_card() << "\n"; }; ostream& operator <<(ostream& stream, const game_state& gs) { diff --git a/src/main/game_state.h b/src/main/game_state.h index 974fd2a..c5d4f96 100644 --- a/src/main/game_state.h +++ b/src/main/game_state.h @@ -13,6 +13,7 @@ #include "card.h" #include "sol_rules.h" +#include "pile.h" class game_state { public: @@ -35,11 +36,10 @@ private: void print_tableau_piles(std::ostream&) const; void print_hole(std::ostream&) const; - std::vector > tableau_piles; - std::vector foundations; + std::vector tableau_piles; + std::vector foundations; + pile hole; sol_rules rules; - card hole_card; - mutable bool solved; }; diff --git a/src/main/pile.cpp b/src/main/pile.cpp new file mode 100644 index 0000000..e19d96c --- /dev/null +++ b/src/main/pile.cpp @@ -0,0 +1,92 @@ +// +// Created by thecharlesblake on 10/28/17. +// + +#include "pile.h" +#include "sol_rules.h" +#include "card.h" + +pile::pile(bool c, sol_rules::build_order bo, sol_rules::build_policy bp, + bool l, int m) : can_remove(c), build_order(bo), + build_policy(bp), build_order_loops(l), + max_rank(m) {} + +bool pile::can_place(const card& c) const { + typedef sol_rules::build_order ord; + typedef sol_rules::build_policy pol; + + if (build_order == ord::NO_BUILD) { + return false; + } + + if (empty()) { + if (build_policy < 4) { + return c.get_suit_val() == build_policy; + } else { + return true; + } + } + + card top_card = top_card(); + + // Check suit + if (build_policy == pol::SAME_SUIT && c.get_suit_val() != top_card.get_suit_val()) { + return false; + } + if (build_policy < 4 && c.get_suit_val() != build_policy) { + return false; + } + + int tc_r = top_card.get_rank(); + int comp_r = c.get_rank(); + + if (build_order == ord::ASCENDING) { + return one_lt(tc_r, comp_r); + } else if (build_order == ord::DESCENDING) { + return one_lt(comp_r, tc_r); + } else if (build_order == ord::BOTH) { + return one_lt(tc_r, comp_r) || one_lt(comp_r, tc_r); + } +} + +bool pile::one_lt(int a, int b) { + return (a == b - 1) || (loop && a == max_rank && b == 1); +} + +void pile::place(const card& c) { + pile_vec.push_back(c); +} + +card pile::top_card() const { + return pile_vec.back(); +} + +card pile::take() { + card c = top_card(); + pile_vec.pop_back(); + return c; +} + +bool pile::can_remove() const { + return can_remove; +} + +bool pile::empty() const { + return pile_vec.empty(); +} + +int pile::size() const { + return pile_vec.size(); +} + +card& pile::operator[] (std::vector::size_type i) { + return pile_vec[i]; +} +card pile::operator[] (std::vector::size_type i) const { + return pile_vec[i]; +} + + +void move(const pile& a, const pile& b) { + b.place(a.take()); +} diff --git a/src/main/pile.h b/src/main/pile.h new file mode 100644 index 0000000..669c540 --- /dev/null +++ b/src/main/pile.h @@ -0,0 +1,36 @@ +// +// Created by thecharlesblake on 10/28/17. +// + +#ifndef SOLVITAIRE_PILE_H +#define SOLVITAIRE_PILE_H + +#import "sol_rules.h" + +class pile { +public: + pile(); + pile(bool, sol_rules::build_order, sol_rules::build_policy, bool, int m = 13); + bool can_place(const card&) const; + void place(const card&); + card take(); + card top_card() const; + bool can_remove() const; + bool empty() const; + int size() const; + card& operator[] (std::vector::size_type); + card operator[] (std::vector::size_type) const; +private: + vector pile_vec; + const bool can_remove; + const bool build_order_loops; + const int max_rank; + const sol_rules::build_order build_order; + const sol_rules::build_policy build_policy; + bool one_lt(int, int); +}; + +void move(const pile&, const pile&); + + +#endif //SOLVITAIRE_PILE_H diff --git a/src/main/sol_rules.cpp b/src/main/sol_rules.cpp index 75c79b1..7d01902 100644 --- a/src/main/sol_rules.cpp +++ b/src/main/sol_rules.cpp @@ -22,19 +22,19 @@ sol_rules::sol_rules(std::string sol_type) { hole = false; foundations = true; - build_order = tab_build_order::DESCENDING; + build_order = build_order::DESCENDING; switch(vs) { case valid_sol::BLACK_HOLE: tableau_pile_count = 17; - build_order = tab_build_order::NO_BUILD; + build_order = build_order::NO_BUILD; max_rank = 13; hole = true; foundations = false; break; case valid_sol::SIMPLE_BLACK_HOLE: tableau_pile_count = 9; - build_order = tab_build_order::NO_BUILD; + build_order = build_order::NO_BUILD; max_rank = 7; hole = true; foundations = false; diff --git a/src/main/sol_rules.h b/src/main/sol_rules.h index 56701c7..2a442b8 100644 --- a/src/main/sol_rules.h +++ b/src/main/sol_rules.h @@ -14,9 +14,19 @@ struct sol_rules { SIMPLE_BLACK_HOLE, SPANISH_PATIENCE }; - enum class tab_build_order { + enum class build_order { NO_BUILD, DESCENDING, + ASCENDING, + BOTH + }; + enum class build_policy { + CLUBS, + DIAMONDS, + HEARTS, + SPADES, + ANY_SUIT, + SAME_SUIT }; static std::vector valid_sol_strs; @@ -24,7 +34,7 @@ struct sol_rules { sol_rules(std::string); int tableau_pile_count; - tab_build_order build_order; + build_order build_order; int max_rank; bool hole; bool foundations; @@ -33,5 +43,4 @@ private: static valid_sol valid_sol_enum(const std::string&); }; - #endif //SOLVITAIRE_SOL_RULES_H diff --git a/src/main/solver.cpp b/src/main/solver.cpp index ac8ffb8..c706a01 100644 --- a/src/main/solver.cpp +++ b/src/main/solver.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "solver.h" #include "log_helper.h" @@ -34,9 +35,17 @@ bool solver::run() { // Create new nodes for each of the children vector new_children = current.state.get_next_legal_states(); for (auto it = new_children.rbegin(); it != new_children.rend(); it++) { + game_state new_state = *it; + + // If we have seen the state before, ignore it (loop detection) + if (find(begin(current.history), end(current.history), new_state) + != end(current.history)) { + continue; + } + node n = current; n.history.push_back(n.state); - n.state = *it; + n.state = new_state; frontier.push_back(n); } -- cgit v1.2.1 From 578f9c9f92d05b5907b91f07a4c732839d4dfca5 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 6 Nov 2017 14:14:38 +0000 Subject: Add counting of states searched --- src/main/card.cpp | 2 -- src/main/card.h | 1 - src/main/game_state.cpp | 87 +++++++++++++++++++++++-------------------------- src/main/game_state.h | 11 ++++--- src/main/pile.cpp | 74 +++++++++++++++++++++++++++++------------ src/main/pile.h | 37 ++++++++++++++------- src/main/sol_rules.cpp | 43 ++++++++++++++++++++---- src/main/sol_rules.h | 10 ++++-- src/main/solver.cpp | 33 +++++++++++-------- src/main/solver.h | 3 +- 10 files changed, 190 insertions(+), 111 deletions(-) (limited to 'src/main') diff --git a/src/main/card.cpp b/src/main/card.cpp index 583814d..14ba92d 100644 --- a/src/main/card.cpp +++ b/src/main/card.cpp @@ -9,8 +9,6 @@ using namespace std; -card::card() : rank(-1), suit(-1) {} - card::card(int r, int s) : rank(r), suit(s) {} card::card(const char* c) : rank(-1), suit(-1) { diff --git a/src/main/card.h b/src/main/card.h index 7c28a28..ef6ae58 100644 --- a/src/main/card.h +++ b/src/main/card.h @@ -9,7 +9,6 @@ class card { public: - card(); card(int, int); card(const char*); int get_rank() const; diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index 93ecb02..29ccdc3 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -31,7 +31,7 @@ game_state::game_state(const Document& doc) int max_rank = 1; for (auto& json_tab : json_tab_piles.GetArray()) { - pile tableau_pile(true, rules.build_order, pol::ANY_SUIT, false); + pile tableau_pile(true, rules.build_ord, pol::ANY_SUIT, false); for (auto& json_card : json_tab.GetArray()) { card c(json_card.GetString()); @@ -50,24 +50,24 @@ game_state::game_state(const Document& doc) } // Construct an initial game state from a seed -game_state::game_state(int seed, const sol_rules& rules) - : rules(rules), +game_state::game_state(int seed, const sol_rules& s_rules) + : rules(s_rules), hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank) { vector deck = shuffled_deck(seed, rules.max_rank); + // If there is a hole, move the ace to spades to it + if (rules.hole) { + deck.erase(find(begin(deck), end(deck), card("AS"))); + hole.place("AS"); + } + // Deal to the tableau piles (row-by-row) for (vector::size_type i = 0; i < deck.size(); i++) { card c = deck[i]; - // If there is a hole card, don't deal the ace of spades - if (rules.hole && c == "AS") { - hole.place("AS"); - continue; - } - // For the first row we must create all the tableau pile vectors if (i / rules.tableau_pile_count == 0) { - pile p(true, rules.build_order, pol::ANY_SUIT, false); + pile p(true, rules.build_ord, pol::ANY_SUIT, false); tableau_piles.push_back(p); } @@ -111,37 +111,37 @@ vector game_state::shuffled_deck(int seed, int max_rank = 13) { return deck; } -vector game_state::get_next_legal_states() const { +vector game_state::get_next_legal_states() { // Generates vectors of references to all the piles from which // cards can be removed, and to which they can be added - vector can_remove; - vector can_add; + vector can_remove; + vector can_add; - for (pile p : tableau_piles) { - can_remove.push_back(p); - if (rules.build_order != ord::NO_BUILD) { - can_add.push_back(p); + for (vector::size_type i = 0; i < tableau_piles.size(); i++) { + can_remove.push_back(&tableau_piles[i]); + if (rules.build_ord != ord::NO_BUILD) { + can_add.push_back(&tableau_piles[i]); } } if (rules.foundations) { - for (pile p : foundations) { - can_remove.push_back(p); - can_add.push_back(p); + for (vector::size_type i = 0; i < foundations.size(); i++) { + can_remove.push_back(&foundations[i]); + can_add.push_back(&foundations[i]); } } if (rules.hole) { - can_add.push_back(hole); + can_add.push_back(&hole); } // The next legal states vector next; - for (pile &rem_pile : can_remove) { - if (rem_pile.empty()) continue; + for (pile *rem_pile : can_remove) { + if (rem_pile->empty()) continue; - for (pile &add_pile : can_add) { - if (&add_pile != &rem_pile - && add_pile.can_place(rem_pile.top_card())) { + for (pile *add_pile : can_add) { + if (add_pile != rem_pile + && add_pile->can_place(rem_pile->top_card())) { move(rem_pile, add_pile); // remove game_state s = *this; @@ -155,17 +155,6 @@ vector game_state::get_next_legal_states() const { } - -bool game_state::adjacent(const card& a, const card& b) const { - int x = a.get_rank(); - int y = b.get_rank(); - - return x == y + 1 - || x + 1 == y - || (x == 1 && y == rules.max_rank) - || (x == rules.max_rank && y == 1); -} - bool game_state::is_solved() const { for (auto p : tableau_piles) { if (!p.empty()) { @@ -176,8 +165,6 @@ bool game_state::is_solved() const { } ostream& game_state::print(ostream& stream) const { - const char *sep = "------------------------------\n"; - if (rules.foundations) { print_header(stream, "Foundations"); print_foundations(stream); @@ -190,7 +177,7 @@ ostream& game_state::print(ostream& stream) const { print_header(stream, "Hole Card"); print_hole(stream); } - return stream << "///////////////////////////////////\n\n"; + return stream << "===================================\n"; } void game_state::print_header(ostream& stream, const char* header) const { @@ -203,11 +190,11 @@ void game_state::print_header(ostream& stream, const char* header) const { } void game_state::print_foundations(ostream& stream) const { - stream << foundations[0].top_card() << "\t" - << foundations[1].top_card() << "\t" - << foundations[2].top_card() << "\t" - << foundations[3].top_card() << "\n"; -}; + stream << foundations[0] << "\t" + << foundations[1] << "\t" + << foundations[2] << "\t" + << foundations[3] << "\n"; +} void game_state::print_tableau_piles(ostream& stream) const { bool empty_row = false; @@ -227,11 +214,17 @@ void game_state::print_tableau_piles(ostream& stream) const { stream << "\n"; row_idx++; } -}; +} void game_state::print_hole(ostream& stream) const { stream << hole.top_card() << "\n"; -}; +} + +bool operator==(const game_state& a, const game_state& b) { + return a.tableau_piles == b.tableau_piles + && a.foundations == b.foundations + && a.hole == b.hole; +} ostream& operator <<(ostream& stream, const game_state& gs) { return gs.print(stream); diff --git a/src/main/game_state.h b/src/main/game_state.h index c5d4f96..311c97f 100644 --- a/src/main/game_state.h +++ b/src/main/game_state.h @@ -17,20 +17,23 @@ class game_state { public: + // Create a game state representation from a JSON doc game_state(const rapidjson::Document&); + // Do the same from a 'rules' object game_state(int seed, const sol_rules&); - std::vector get_next_legal_states() const; + std::vector get_next_legal_states(); + // NOTE: will only be evaluated after get_next_legal_states() is called bool is_solved() const; + std::ostream& print(std::ostream&) const; + friend bool operator==(const game_state&, const game_state&); friend std::ostream& operator<< (std::ostream&, const game_state&); private: static std::vector shuffled_deck(int, int); - void move_to_hole(int); - bool adjacent(const card& a, const card& b) const; void print_header(std::ostream&, const char*) const; void print_foundations(std::ostream&) const; void print_tableau_piles(std::ostream&) const; @@ -38,8 +41,8 @@ private: std::vector tableau_piles; std::vector foundations; - pile hole; sol_rules rules; + pile hole; }; diff --git a/src/main/pile.cpp b/src/main/pile.cpp index e19d96c..cceda50 100644 --- a/src/main/pile.cpp +++ b/src/main/pile.cpp @@ -2,14 +2,31 @@ // Created by thecharlesblake on 10/28/17. // +#include +#include + #include "pile.h" -#include "sol_rules.h" -#include "card.h" -pile::pile(bool c, sol_rules::build_order bo, sol_rules::build_policy bp, - bool l, int m) : can_remove(c), build_order(bo), - build_policy(bp), build_order_loops(l), - max_rank(m) {} +using namespace std; + +pile::pile() { + set_max_rank(13); +} + +pile::pile(bool r, sol_rules::build_order bo, sol_rules::build_policy bp, bool l, + int mr) { + set_remove(r); + set_build_order(bo); + set_build_policy(bp); + set_loops(l); + set_max_rank(mr); +} + +void pile::set_remove(bool r) {remove = r;} +void pile::set_loops(bool l) {build_order_loops = l;} +void pile::set_max_rank(int mr) {max_rank = mr;} +void pile::set_build_order(sol_rules::build_order bo) {build_order = bo;} +void pile::set_build_policy(sol_rules::build_policy bp) {build_policy = bp;} bool pile::can_place(const card& c) const { typedef sol_rules::build_order ord; @@ -20,24 +37,27 @@ bool pile::can_place(const card& c) const { } if (empty()) { - if (build_policy < 4) { - return c.get_suit_val() == build_policy; + if (sol_rules::is_suit(build_policy)) { + return c.get_suit_val() == sol_rules::suit_val(build_policy) + && c.get_rank() == 1; } else { return true; } } - card top_card = top_card(); + card top_c = top_card(); // Check suit - if (build_policy == pol::SAME_SUIT && c.get_suit_val() != top_card.get_suit_val()) { + if (build_policy == pol::SAME_SUIT && c.get_suit_val() + != top_c.get_suit_val()) { return false; } - if (build_policy < 4 && c.get_suit_val() != build_policy) { + if (sol_rules::is_suit(build_policy) && c.get_suit_val() + != sol_rules::suit_val(build_policy)) { return false; } - int tc_r = top_card.get_rank(); + int tc_r = top_c.get_rank(); int comp_r = c.get_rank(); if (build_order == ord::ASCENDING) { @@ -46,11 +66,13 @@ bool pile::can_place(const card& c) const { return one_lt(comp_r, tc_r); } else if (build_order == ord::BOTH) { return one_lt(tc_r, comp_r) || one_lt(comp_r, tc_r); + } else { + return false; } } -bool pile::one_lt(int a, int b) { - return (a == b - 1) || (loop && a == max_rank && b == 1); +bool pile::one_lt(int a, int b) const { + return (a == b - 1) || (build_order_loops && a == max_rank && b == 1); } void pile::place(const card& c) { @@ -68,25 +90,37 @@ card pile::take() { } bool pile::can_remove() const { - return can_remove; + return remove; } bool pile::empty() const { return pile_vec.empty(); } -int pile::size() const { +unsigned long pile::size() const { return pile_vec.size(); } -card& pile::operator[] (std::vector::size_type i) { +card& pile::operator[] (vector::size_type i) { return pile_vec[i]; } -card pile::operator[] (std::vector::size_type i) const { +card pile::operator[] (vector::size_type i) const { return pile_vec[i]; } -void move(const pile& a, const pile& b) { - b.place(a.take()); +void move(pile *a, pile *b) { + b->place(a->take()); +} + +bool operator==(const pile& a, const pile& b) { + return a.pile_vec == b.pile_vec; +} + +ostream & operator<<(ostream & stream, pile const & p) { + if (p.empty()) { + return stream << "[]"; + } else { + return stream << p.top_card(); + } } diff --git a/src/main/pile.h b/src/main/pile.h index 669c540..4bc6e71 100644 --- a/src/main/pile.h +++ b/src/main/pile.h @@ -5,32 +5,45 @@ #ifndef SOLVITAIRE_PILE_H #define SOLVITAIRE_PILE_H -#import "sol_rules.h" +#include + +#include "sol_rules.h" +#include "card.h" class pile { public: pile(); pile(bool, sol_rules::build_order, sol_rules::build_policy, bool, int m = 13); + + void set_remove(bool); + void set_loops(bool); + void set_max_rank(int); + void set_build_order(sol_rules::build_order); + void set_build_policy(sol_rules::build_policy); + bool can_place(const card&) const; void place(const card&); card take(); card top_card() const; bool can_remove() const; bool empty() const; - int size() const; - card& operator[] (std::vector::size_type); - card operator[] (std::vector::size_type) const; + unsigned long size() const; + card& operator[] (std::vector::size_type); + card operator[] (std::vector::size_type) const; + + friend bool operator==(const pile&, const pile&); + friend std::ostream & operator<<(std::ostream &, pile const &); private: - vector pile_vec; - const bool can_remove; - const bool build_order_loops; - const int max_rank; - const sol_rules::build_order build_order; - const sol_rules::build_policy build_policy; - bool one_lt(int, int); + std::vector pile_vec; + bool remove; + bool build_order_loops; + int max_rank; + sol_rules::build_order build_order; + sol_rules::build_policy build_policy; + bool one_lt(int, int) const; }; -void move(const pile&, const pile&); +void move(pile*, pile*); #endif //SOLVITAIRE_PILE_H diff --git a/src/main/sol_rules.cpp b/src/main/sol_rules.cpp index 7d01902..eded5eb 100644 --- a/src/main/sol_rules.cpp +++ b/src/main/sol_rules.cpp @@ -11,38 +11,67 @@ using namespace std; +// Static + +typedef sol_rules::build_policy pol; + vector sol_rules::valid_sol_strs = { "black-hole", "simple-black-hole", - "simple-patience" + "spanish-patience", + "simple-spanish-patience" }; +bool sol_rules::is_suit(pol bp) { + return suit_val(bp) != -1; +} +int sol_rules::suit_val(pol bp) { + switch (bp) { + case pol::CLUBS: return 0; + case pol::DIAMONDS: return 1; + case pol::HEARTS: return 2; + case pol::SPADES: return 3; + default: return -1; + } +} + +// Non-static + sol_rules::sol_rules(std::string sol_type) { valid_sol vs = valid_sol_enum(sol_type); - hole = false; - foundations = true; - build_order = build_order::DESCENDING; switch(vs) { case valid_sol::BLACK_HOLE: tableau_pile_count = 17; - build_order = build_order::NO_BUILD; + build_ord = build_order::NO_BUILD; max_rank = 13; hole = true; foundations = false; break; case valid_sol::SIMPLE_BLACK_HOLE: tableau_pile_count = 9; - build_order = build_order::NO_BUILD; + build_ord = build_order::NO_BUILD; max_rank = 7; hole = true; foundations = false; break; case valid_sol::SPANISH_PATIENCE: tableau_pile_count = 13; + build_ord = build_order::DESCENDING; max_rank = 13; + hole = false; + foundations = true; + break; + case valid_sol::SIMPLE_SPANISH_PATIENCE: + tableau_pile_count = 5; + build_ord = build_order::DESCENDING; + max_rank = 5; + hole = false; + foundations = true; break; + default: + assert(false); } } @@ -52,7 +81,7 @@ sol_rules::valid_sol sol_rules::valid_sol_enum(const std::string& s) { assert(sol_type_it != valid_sol_strs.end()); - int idx = sol_type_it - valid_sol_strs.begin(); + long 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 index 2a442b8..671e8b7 100644 --- a/src/main/sol_rules.h +++ b/src/main/sol_rules.h @@ -8,11 +8,13 @@ #include #include -struct sol_rules { +class sol_rules { +public: enum class valid_sol { BLACK_HOLE, SIMPLE_BLACK_HOLE, - SPANISH_PATIENCE + SPANISH_PATIENCE, + SIMPLE_SPANISH_PATIENCE }; enum class build_order { NO_BUILD, @@ -30,11 +32,13 @@ struct sol_rules { }; static std::vector valid_sol_strs; + static bool is_suit(build_policy); + static int suit_val(build_policy); sol_rules(std::string); int tableau_pile_count; - build_order build_order; + build_order build_ord; int max_rank; bool hole; bool foundations; diff --git a/src/main/solver.cpp b/src/main/solver.cpp index c706a01..1db92f9 100644 --- a/src/main/solver.cpp +++ b/src/main/solver.cpp @@ -14,7 +14,8 @@ using namespace std; solver::solver(const game_state gs, const sol_rules& sr) - : root(gs), rules(sr), solution(gs), solution_found(false) {} + : root(gs), rules(sr), solution(gs), solution_found(false), + states_searched(0) {} solver::node::node(const game_state gs) : state(gs) {} @@ -34,44 +35,48 @@ bool solver::run() { // Create new nodes for each of the children vector new_children = current.state.get_next_legal_states(); - for (auto it = new_children.rbegin(); it != new_children.rend(); it++) { - game_state new_state = *it; + states_searched++; + + // NOTE: get_next_legal_states() makes the is_solved() function work + if (current.state.is_solved()) { + solution = current; + solution_found = true; + break; + } - // If we have seen the state before, ignore it (loop detection) - if (find(begin(current.history), end(current.history), new_state) + for (auto it = new_children.rbegin(); it != new_children.rend(); it++) { + // If we have seen the state before, ignore it (loop detection)) + if (find(begin(current.history), end(current.history), *it) != end(current.history)) { continue; } node n = current; n.history.push_back(n.state); - n.state = new_state; + n.state = *it; frontier.push_back(n); } - - if (current.state.is_solved()) { - solution = current; - solution_found = true; - break; - } } return solution_found; } std::ostream& operator<< (std::ostream& stream, const solver& sol) { + if (sol.solution_found) { stream << "Solution:\n"; for (auto state : sol.solution.history) { stream << state << "\n"; } - stream << sol.solution.state << "\n"; + stream << sol.solution.state << "\nSolved!\n"; } else if (sol.been_run) { - stream << "No Possible Solution\n"; + stream << "Deal:\n" << sol.root.state << "\n" + << "No Possible Solution\n"; } else { stream << "Solver has not been run yet\n"; } + stream << "States Searched: " << sol.states_searched << "\n"; return stream; } \ No newline at end of file diff --git a/src/main/solver.h b/src/main/solver.h index cff1e47..4215989 100644 --- a/src/main/solver.h +++ b/src/main/solver.h @@ -27,11 +27,12 @@ private: game_state state; }; - const sol_rules rules; const node root; + const sol_rules rules; bool been_run; node solution; bool solution_found; + int states_searched; }; -- cgit v1.2.1