aboutsummaryrefslogtreecommitdiff
path: root/src/main/game
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/game')
-rw-r--r--src/main/game/game_state.cpp81
-rw-r--r--src/main/game/game_state.h11
-rw-r--r--src/main/game/sol_rules.cpp48
3 files changed, 54 insertions, 86 deletions
diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp
index 2b77fec..b851f2a 100644
--- a/src/main/game/game_state.cpp
+++ b/src/main/game/game_state.cpp
@@ -12,6 +12,7 @@
#include <boost/functional/hash.hpp>
#include "game_state.h"
+#include "../input-output/deal_parser.h"
using namespace std;
using namespace rapidjson;
@@ -21,44 +22,42 @@ 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)
- : reserve(true, ord::NO_BUILD, pol::ANY_SUIT, false),
- stock(true, ord::NO_BUILD, pol::ANY_SUIT, false),
- waste(true, ord::NO_BUILD, pol::ANY_SUIT, false),
- hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank),
- rules(sol_rules::from_preset("simple-black-hole")) {
- // 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()) {
- pile tableau_pile(true, rules.build_ord, 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.place(c);
+game_state::game_state(const sol_rules& s_rules) :
+ rules(s_rules),
+ reserve(pile::reserve_factory()),
+ stock(pile::stock_factory()),
+ waste(pile::waste_factory()),
+ hole(pile::hole_factory(s_rules.max_rank)) {
+
+ // Creates the tableau piles
+ for (unsigned int i = 0; i < rules.tableau_pile_count; i++) {
+ tableau_piles.push_back(pile::tableau_factory(rules.build_ord));
+ }
+
+ // If there are foundation piles, create the relevant pile vectors
+ if (rules.foundations) {
+ for (int i = 0; i < 4; i++) {
+ foundations.push_back(pile::foundation_factory(pol(i)));
}
+ }
- tableau_piles.push_back(tableau_pile);
+ // If there are cell piles, create the relevant cell vectors
+ if (rules.cells > 0) {
+ for (unsigned int i = 0; i < rules.cells; i++) {
+ cells.push_back(pile::cell_factory());
+ }
}
+}
- // Assign hole card
- assert(doc.HasMember("hole card"));
- const Value& json_h_card = doc["hole card"];
- assert(json_h_card.IsString());
- hole.place(json_h_card.GetString());
+// Construct an initial game state from a JSON doc
+game_state::game_state(const sol_rules& s_rules, const Document& doc) :
+ game_state(s_rules) {
+ deal_parser::parse(*this, doc);
}
// Construct an initial game state from a seed
-game_state::game_state(int seed, const sol_rules& s_rules) :
- reserve(pile::reserve_factory()),
- stock(pile::stock_factory()),
- waste(pile::waste_factory()),
- hole(pile::hole_factory(s_rules.max_rank)),
- rules(s_rules) {
+game_state::game_state(const sol_rules& s_rules, int seed) :
+ game_state(s_rules) {
vector<card> deck = shuffled_deck(seed, rules.max_rank);
@@ -84,11 +83,6 @@ game_state::game_state(int seed, const sol_rules& s_rules) :
}
}
- // Creates the tableau piles
- for (unsigned int i = 0; i < rules.tableau_pile_count; i++) {
- tableau_piles.push_back(pile::tableau_factory(rules.build_ord));
- }
-
// Deal to the tableau piles (row-by-row)
unsigned int t = 0;
while (!deck.empty()) {
@@ -98,20 +92,6 @@ game_state::game_state(int seed, const sol_rules& s_rules) :
// Add the randomly generated card to the tableau piles
tableau_piles[t++ % 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++) {
- foundations.push_back(pile::foundation_factory(pol(i)));
- }
- }
-
- // If there are cell piles, create the relevant cell vectors
- if (rules.cells > 0) {
- for (unsigned int i = 0; i < rules.cells; i++) {
- cells.push_back(pile::cell_factory());
- }
- }
}
// Generate a randomly ordered vector of cards
@@ -357,4 +337,3 @@ size_t hash_value(game_state const& gs) {
size_t hash_value(vector<pile> const& vp) {
return hash_range(begin(vp), end(vp));
}
-
diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h
index 873ca6f..4b96b72 100644
--- a/src/main/game/game_state.h
+++ b/src/main/game/game_state.h
@@ -15,12 +15,15 @@
#include "sol_rules.h"
#include "pile.h"
+class deal_parser;
+
class game_state {
+ friend class deal_parser;
public:
// Create a game state representation from a JSON doc
- game_state(const rapidjson::Document&);
+ game_state(const sol_rules&, const rapidjson::Document&);
// Do the same from a 'rules' object
- game_state(int seed, const sol_rules&);
+ game_state(const sol_rules&, int seed);
const std::vector<game_state> get_next_legal_states() const;
bool is_solved() const;
@@ -33,6 +36,8 @@ public:
friend std::size_t hash_value(std::vector<pile> const&);
private:
+ explicit game_state(const sol_rules&);
+
static std::vector<card> shuffled_deck(int, int);
const game_state move(game_state&, pile*, pile*) const;
@@ -44,6 +49,7 @@ private:
void print_top_of_piles(std::ostream&, const std::vector<pile>&) const;
void print_top_of_pile(std::ostream&, const pile&) const;
+ sol_rules rules;
std::vector<pile> foundations;
std::vector<pile> cells;
std::vector<pile> tableau_piles;
@@ -51,7 +57,6 @@ private:
pile stock;
pile waste;
pile hole;
- sol_rules rules;
};
diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp
index d2b0253..91b00c1 100644
--- a/src/main/game/sol_rules.cpp
+++ b/src/main/game/sol_rules.cpp
@@ -5,13 +5,12 @@
#include <string>
#include <vector>
#include <algorithm>
-#include <input-output/log_helper.h>
#include "rapidjson/error/en.h"
-#include "util/util.h"
#include "sol_rules.h"
-#include "input-output/sol_preset_types.h"
+#include "../util/util.h"
+#include "../input-output/sol_preset_types.h"
using namespace std;
using namespace boost;
@@ -38,18 +37,7 @@ int sol_rules::suit_val(pol bp) {
const sol_rules sol_rules::from_file(const string rules_file) {
sol_rules sr = get_default();
-
- const optional<string> file_contents = util::read_file(rules_file);
- if (!file_contents) {
- throw runtime_error("Could not read rules file");
- }
-
- Document d;
- if (d.Parse(file_contents->c_str()).HasParseError()) {
- string errmsg = "Error parsing rules file JSON: ";
- errmsg += GetParseError_En(d.GetParseError());
- throw runtime_error(errmsg);
- }
+ Document d = util::get_file_json(rules_file);
modify_sol_rules(sr, d);
@@ -84,7 +72,7 @@ sol_rules sol_rules::get_default() {
void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) {
if (!d.IsObject()) {
- parse_err("JSON doc must be object");
+ util::json_parse_err("JSON doc must be object");
}
if (d.HasMember("tableau piles")) {
@@ -94,7 +82,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) {
if (d["tableau piles"]["count"].IsInt()) {
sr.tableau_pile_count = d["tableau piles"]["count"].GetInt();
} else {
- parse_err("[tableau piles][count] must be an integer");
+ util::json_parse_err("[tableau piles][count] must be an integer");
}
}
@@ -107,11 +95,11 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) {
} else if (bo_str == "descending") {
sr.build_ord = build_order::DESCENDING;
} else {
- parse_err("[tableau piles][build order] is invalid");
+ util::json_parse_err("[tableau piles][build order] is invalid");
}
} else {
- parse_err("[tableau piles][build order] must be an string");
+ util::json_parse_err("[tableau piles][build order] must be an string");
}
}
@@ -124,16 +112,16 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) {
} else if (bo_str == "red-black") {
sr.build_pol = build_policy::RED_BLACK;
} else {
- parse_err("[tableau piles][build policy] is invalid");
+ util::json_parse_err("[tableau piles][build policy] is invalid");
}
} else {
- parse_err("[tableau piles][build policy] must be an string");
+ util::json_parse_err("[tableau piles][build policy] must be an string");
}
}
} else {
- parse_err("[tableau piles] must be an object");
+ util::json_parse_err("[tableau piles] must be an object");
}
}
@@ -141,7 +129,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) {
if (d["max rank"].IsInt()) {
sr.max_rank = d["max rank"].GetInt();
} else {
- parse_err("[max rank] must be an integer");
+ util::json_parse_err("[max rank] must be an integer");
}
}
@@ -149,7 +137,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) {
if (d["hole"].IsBool()) {
sr.hole = d["hole"].GetBool();
} else {
- parse_err("[hole] must be a boolean");
+ util::json_parse_err("[hole] must be a boolean");
}
}
@@ -157,7 +145,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) {
if (d["foundations"].IsBool()) {
sr.foundations = d["foundations"].GetBool();
} else {
- parse_err("[foundations] must be a boolean");
+ util::json_parse_err("[foundations] must be a boolean");
}
}
@@ -165,7 +153,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) {
if (d["cells"].IsInt()) {
sr.cells = d["cells"].GetInt();
} else {
- parse_err("[cells] must be an integer");
+ util::json_parse_err("[cells] must be an integer");
}
}
@@ -173,7 +161,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) {
if (d["reserve size"].IsInt()) {
sr.reserve_size = d["reserve size"].GetInt();
} else {
- parse_err("[reserve size] must be an integer");
+ util::json_parse_err("[reserve size] must be an integer");
}
}
@@ -181,11 +169,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) {
if (d["stock size"].IsInt()) {
sr.stock_size = d["stock size"].GetInt();
} else {
- parse_err("[stock size] must be an integer");
+ util::json_parse_err("[stock size] must be an integer");
}
}
}
-
-void sol_rules::parse_err(const string& msg) {
- throw runtime_error("Error in JSON doc: " + msg);
-}