aboutsummaryrefslogtreecommitdiff
path: root/src/main/input-output/input/json-parsing/deal_parser.cpp
diff options
context:
space:
mode:
authorthecharlesblake2018-01-10 18:15:14 +0000
committerthecharlesblake2018-01-10 18:15:14 +0000
commit3ce4c07a3d78082590629d36d4039d68f0a84663 (patch)
treed5d7839783cebb097af9f071751d693f47e3af6d /src/main/input-output/input/json-parsing/deal_parser.cpp
parentAdd debug mode to benchmark script (diff)
downloadmetasol-3ce4c07a3d78082590629d36d4039d68f0a84663.tar.gz
metasol-3ce4c07a3d78082590629d36d4039d68f0a84663.zip

Add asserts to program and fix bugs they raise

Diffstat (limited to 'src/main/input-output/input/json-parsing/deal_parser.cpp')
-rw-r--r--src/main/input-output/input/json-parsing/deal_parser.cpp30
1 files changed, 14 insertions, 16 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)
);
}
}