aboutsummaryrefslogtreecommitdiff
path: root/src/main/input-output/input
diff options
context:
space:
mode:
authorCharlie Blake2018-01-10 18:16:34 +0000
committerGitHub2018-01-10 18:16:34 +0000
commit2a62143fb9edb929f410b1b9b3aed16a16ceba39 (patch)
treed5d7839783cebb097af9f071751d693f47e3af6d /src/main/input-output/input
parentMerge pull request #28 from thecharlesblake/all-canonical-games (diff)
parentAdd asserts to program and fix bugs they raise (diff)
downloadmetasol-2a62143fb9edb929f410b1b9b3aed16a16ceba39.tar.gz
metasol-2a62143fb9edb929f410b1b9b3aed16a16ceba39.zip

Merge pull request #29 from thecharlesblake/refactor-improve-debug

Refactor improve debug

Diffstat (limited to 'src/main/input-output/input')
-rw-r--r--src/main/input-output/input/json-parsing/deal_parser.cpp31
-rw-r--r--src/main/input-output/input/json-parsing/json_helper.cpp9
-rw-r--r--src/main/input-output/input/json-parsing/json_helper.h1
-rw-r--r--src/main/input-output/input/json-parsing/rules_parser.cpp28
4 files changed, 33 insertions, 36 deletions
diff --git a/src/main/input-output/input/json-parsing/deal_parser.cpp b/src/main/input-output/input/json-parsing/deal_parser.cpp
index 6f0694d..b2a4c9d 100644
--- a/src/main/input-output/input/json-parsing/deal_parser.cpp
+++ b/src/main/input-output/input/json-parsing/deal_parser.cpp
@@ -9,7 +9,6 @@
using namespace std;
using namespace rapidjson;
-typedef sol_rules::build_order ord;
typedef sol_rules::build_policy pol;
void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) {
@@ -49,7 +48,7 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document&
const Value& json_tab_piles = doc["tableau piles"];
assert(json_tab_piles.IsArray());
- if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) {
+ if (json_tab_piles.Size() != gs.rules.tableau_pile_count ) {
json_helper::json_parse_err("Incorrect number of tableau piles");
}
@@ -57,6 +56,7 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document&
p.second != end(gs.tableau_piles);
++p.first, ++p.second) {
+ assert(p.first->IsArray());
for (auto& json_card : p.first->GetArray()) {
assert(json_card.IsString());
gs.piles[*p.second].place(card(json_card.GetString()));
@@ -81,7 +81,7 @@ void deal_parser::parse_cells(game_state &gs, const Document& doc) {
const auto json_cell_arr = json_cells.GetArray();
if (json_cell_arr.Empty()) return;
- else if (gs.rules.cells != json_cell_arr.Size()) {
+ else if (json_cell_arr.Size() != gs.rules.cells) {
json_helper::json_parse_err("Incorrect number of cells");
}
@@ -101,11 +101,12 @@ void deal_parser::parse_stock(game_state &gs, const Document& doc) {
const Value &json_stock = doc["stock"];
assert(json_stock.IsArray());
- if (gs.rules.stock_size != json_stock.Size()) {
+ if (json_stock.Size() != gs.rules.stock_size) {
json_helper::json_parse_err("Incorrect stock size");
}
for (const Value& json_card : json_stock.GetArray()) {
+ assert(json_card.IsString());
gs.piles[gs.stock].place(card(json_card.GetString()));
}
}
@@ -116,21 +117,14 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) {
const Value& json_reserve_piles = doc["reserve"];
assert(json_reserve_piles.IsArray());
+ const auto& json_card_arr = json_reserve_piles.GetArray();
+
// We treat a regular reserve like multiple single-card piles,
// but a stacked reserve as a single multiple-card pile
- if (gs.rules.reserve_stacked) {
- if (json_reserve_piles.Size() != gs.rules.reserve_size
- || json_reserve_piles.Size() != gs.rules.reserve_size) {
- json_helper::json_parse_err("Incorrect reserve size");
- }
- } else {
- if (gs.reserve.size() != gs.rules.reserve_size
- || json_reserve_piles.Size() != gs.rules.reserve_size) {
- json_helper::json_parse_err("Incorrect reserve size");
- }
+ if (json_reserve_piles.Size() != gs.rules.reserve_size) {
+ json_helper::json_parse_err("Incorrect reserve size");
}
- const auto& json_card_arr = json_reserve_piles.GetArray();
for (game_state::pile_ref i = 0; i < json_card_arr.Size(); i++) {
assert(json_card_arr[i].IsString());
game_state::pile_ref pr = gs.reserve[0];
@@ -140,9 +134,12 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) {
}
void deal_parser::fill_foundations(game_state &gs) {
- for (uint8_t f_idx = 0; f_idx < 4; f_idx++) {
+ auto foundations_count = uint8_t(4 * (gs.rules.two_decks ? 2:1));
+ assert(gs.foundations.size() == foundations_count);
+
+ for (uint8_t f_idx = 0; f_idx < foundations_count; f_idx++) {
gs.piles[gs.foundations[f_idx]].place(
- card(card::to_suit(f_idx), 1)
+ card(card::to_suit(f_idx % uint8_t(4)), 1)
);
}
}
diff --git a/src/main/input-output/input/json-parsing/json_helper.cpp b/src/main/input-output/input/json-parsing/json_helper.cpp
index 89f4564..f53da6c 100644
--- a/src/main/input-output/input/json-parsing/json_helper.cpp
+++ b/src/main/input-output/input/json-parsing/json_helper.cpp
@@ -6,6 +6,7 @@
#include <sstream>
#include "json_helper.h"
+#include "../../output/log_helper.h"
using namespace std;
using namespace rapidjson;
@@ -31,5 +32,11 @@ Document json_helper::get_file_json(const string& filename) {
}
void json_helper::json_parse_err(const string& msg) {
- throw runtime_error("Error in JSON doc: " + msg);
+ string err_msg = "Error in JSON doc: " + msg;
+ LOG_DEBUG(err_msg);
+ throw runtime_error(err_msg);
+}
+
+void json_helper::json_parse_warning(const string& msg) {
+ LOG_WARNING("Error in JSON doc: " + msg);
}
diff --git a/src/main/input-output/input/json-parsing/json_helper.h b/src/main/input-output/input/json-parsing/json_helper.h
index 5ab24bc..c9c971c 100644
--- a/src/main/input-output/input/json-parsing/json_helper.h
+++ b/src/main/input-output/input/json-parsing/json_helper.h
@@ -12,6 +12,7 @@
class json_helper {
public:
static rapidjson::Document get_file_json(const std::string&);
+ static void json_parse_warning(const std::string& msg);
static void json_parse_err(const std::string& msg);
};
diff --git a/src/main/input-output/input/json-parsing/rules_parser.cpp b/src/main/input-output/input/json-parsing/rules_parser.cpp
index 948eb39..9154ffd 100644
--- a/src/main/input-output/input/json-parsing/rules_parser.cpp
+++ b/src/main/input-output/input/json-parsing/rules_parser.cpp
@@ -16,7 +16,6 @@ using namespace rapidjson;
// Static //
////////////
-typedef sol_rules::build_order ord;
typedef sol_rules::build_policy pol;
typedef sol_rules::spaces_policy s_pol;
typedef sol_rules::stock_deal_type sdt;
@@ -67,25 +66,11 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
if (d["tableau piles"].HasMember("count")) {
if (d["tableau piles"]["count"].IsInt()) {
sr.tableau_pile_count = static_cast<uint8_t>(d["tableau piles"]["count"].GetInt());
- } else {
- json_helper::json_parse_err("[tableau piles][count] must be an integer");
- }
- }
-
- if (d["tableau piles"].HasMember("build order")) {
- if (d["tableau piles"]["build order"].IsString()) {
- string bo_str = d["tableau piles"]["build order"].GetString();
-
- if (bo_str == "ascending") {
- sr.build_ord = ord::ASCENDING;
- } else if (bo_str == "descending") {
- sr.build_ord = ord::DESCENDING;
- } else {
- json_helper::json_parse_err("[tableau piles][build order] is invalid");
+ if (sr.tableau_pile_count > 52) {
+ json_helper::json_parse_warning("[tableau piles][count] may be too high");
}
-
} else {
- json_helper::json_parse_err("[tableau piles][build order] must be an string");
+ json_helper::json_parse_err("[tableau piles][count] must be an integer");
}
}
@@ -151,6 +136,9 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
if (d.HasMember("max rank")) {
if (d["max rank"].IsInt()) {
sr.max_rank = static_cast<card::rank_t>(d["max rank"].GetInt());
+ if (sr.max_rank > 13) {
+ json_helper::json_parse_err("[max rank] must be a valid rank");
+ }
} else {
json_helper::json_parse_err("[max rank] must be an integer");
}
@@ -180,6 +168,10 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
}
}
+ if (!sr.hole && !sr.foundations) {
+ json_helper::json_parse_err("one of [hole] and [foundations] must be true");
+ }
+
if (d.HasMember("foundations initial card")) {
if (d["foundations initial card"].IsBool()) {
sr.foundations_init_card = d["foundations initial card"].GetBool();