aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorthecharlesblake2017-10-28 15:29:17 +0100
committerthecharlesblake2017-10-28 15:29:17 +0100
commit6a5a840f678aa78659b51ec647dcced4544c4a0b (patch)
treeb44dd9add90efd0586b48f4d6f1adecdd78b10eb /src/main
parentAdd rules class to represent solitaire type (diff)
downloadmetasol-6a5a840f678aa78659b51ec647dcced4544c4a0b.tar.gz
metasol-6a5a840f678aa78659b51ec647dcced4544c4a0b.zip

Modify game state generation to work across sols

Diffstat (limited to 'src/main')
-rw-r--r--src/main/game_state.cpp31
-rw-r--r--src/main/game_state.h6
-rw-r--r--src/main/main.cpp2
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<card> 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<card> deck = shuffled_deck(seed, max_rank);
+game_state::game_state(int seed, const sol_rules& rules) {
+ vector<card> deck = shuffled_deck(seed, rules.max_rank);
+ // Deal to the tableau piles (row-by-row)
for (vector<card>::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<card>());
+ }
// 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(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 <rapidjson/document.h>
#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<game_state> 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<std::vector<card> > 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);