diff options
| author | thecharlesblake | 2018-01-10 18:15:14 +0000 |
|---|---|---|
| committer | thecharlesblake | 2018-01-10 18:15:14 +0000 |
| commit | 3ce4c07a3d78082590629d36d4039d68f0a84663 (patch) | |
| tree | d5d7839783cebb097af9f071751d693f47e3af6d /src/main/input-output | |
| parent | Add debug mode to benchmark script (diff) | |
| download | metasol-3ce4c07a3d78082590629d36d4039d68f0a84663.tar.gz metasol-3ce4c07a3d78082590629d36d4039d68f0a84663.zip | |
Add asserts to program and fix bugs they raise
Diffstat (limited to 'src/main/input-output')
5 files changed, 35 insertions, 19 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 eb59d83..b2a4c9d 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -48,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"); } @@ -56,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())); @@ -80,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"); } @@ -100,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())); } } @@ -115,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]; @@ -139,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 1893ff7..9154ffd 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -66,6 +66,9 @@ 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()); + 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][count] must be an integer"); } @@ -133,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"); } @@ -162,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(); diff --git a/src/main/input-output/output/log_helper.h b/src/main/input-output/output/log_helper.h index 71cc5f4..7762250 100644 --- a/src/main/input-output/output/log_helper.h +++ b/src/main/input-output/output/log_helper.h @@ -13,7 +13,7 @@ #define LOG_DEBUG(msg) clog << "[debug]\n" << msg << "\n" #endif #define LOG_INFO(msg) clog << "[info] " << msg << "\n" -#define LOG_WARNING clog(msg) << "[warning] " << msg << "\n" -#define LOG_ERROR(msg) cerr << "[ERROR] " << msg << "\n" +#define LOG_WARNING(msg) clog << "[warning] " << msg << "\n" +#define LOG_ERROR(msg) cerr << "[error] " << msg << "\n" #endif //SOLVITAIRE_LOG_HELPER_H |
