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(-) 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