From 7f4712b292632becfc83bc0e887271b25fefd21a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 13:05:31 +0000 Subject: Refactor io dir structure --- .../input/json-parsing/deal_parser.cpp | 82 ++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/main/input-output/input/json-parsing/deal_parser.cpp (limited to 'src/main/input-output/input/json-parsing/deal_parser.cpp') diff --git a/src/main/input-output/input/json-parsing/deal_parser.cpp b/src/main/input-output/input/json-parsing/deal_parser.cpp new file mode 100644 index 0000000..0e89f43 --- /dev/null +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -0,0 +1,82 @@ +// +// Created by thecharlesblake on 10/21/17. +// + +#include "deal_parser.h" +#include "../../../game/game_state.h" +#include "json_helper.h" + +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) { + // Construct tableau piles + if (gs.rules.tableau_pile_count > 0) { + parse_tableau_piles(gs, doc); + } + + // Construct hole card + if (gs.rules.hole) { + parse_hole(gs, doc); + } + + // Construct filled cells + if (gs.rules.cells) { + parse_cells(gs, doc); + } +} + +void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { + assert(doc.HasMember("tableau piles")); + const Value& json_tab_piles = doc["tableau piles"]; + assert(json_tab_piles.IsArray()); + + if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) { + json_helper::json_parse_err("Incorrect number of tableau piles"); + } + + for (auto p = std::make_pair(begin(json_tab_piles.GetArray()), begin(gs.tableau_piles)); + p.second != end(gs.tableau_piles); + ++p.first, ++p.second) { + + for (auto& json_card : p.first->GetArray()) { + assert(json_card.IsString()); + gs.piles[*p.second].place(card(json_card.GetString())); + } + } +} + +void deal_parser::parse_hole(game_state &gs, const Document& doc) { + if (doc.HasMember("hole")) { + const Value &json_hole = doc["hole"]; + assert(json_hole.IsString()); + + gs.piles[gs.hole].place(card(json_hole.GetString())); + } +} + +void deal_parser::parse_cells(game_state &gs, const Document& doc) { + if (doc.HasMember("cells")) { + const Value &json_cells = doc["cells"]; + assert(json_cells.IsArray()); + + const auto json_cell_arr = json_cells.GetArray(); + + if (json_cell_arr.Empty()) return; + else if (gs.rules.cells != json_cell_arr.Size()) { + json_helper::json_parse_err("Incorrect number of cells"); + } + + for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.cells)); + p.second != end(gs.cells); + ++p.first, ++p.second) { + + auto json_card = p.first; + assert(json_card->IsString()); + gs.piles[*p.second].place(card(json_card->GetString())); + } + } +} -- cgit v1.2.1 From 3f93158b539430a7cb08534fff19903acd56ff66 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 11:41:32 +0000 Subject: Add deal parsing for (FF) foundations init card --- .../input/json-parsing/deal_parser.cpp | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/main/input-output/input/json-parsing/deal_parser.cpp') 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 0e89f43..e7f8209 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -27,6 +27,16 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { if (gs.rules.cells) { parse_cells(gs, doc); } + + // Construct stock + if (gs.rules.stock_size > 0) { + parse_stock(gs, doc); + } + + // If the foundations begin with cards in them, fill them + if (gs.rules.foundations_init_card) { + fill_foundations(gs); + } } void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { @@ -80,3 +90,26 @@ void deal_parser::parse_cells(game_state &gs, const Document& doc) { } } } + +void deal_parser::parse_stock(game_state &gs, const Document& doc) { + if (doc.HasMember("stock")) { + const Value &json_stock = doc["stock"]; + assert(json_stock.IsArray()); + + if (gs.rules.stock_size != json_stock.Size()) { + json_helper::json_parse_err("Incorrect stock size"); + } + + for (const Value& json_card : json_stock.GetArray()) { + gs.piles[gs.stock].place(card(json_card.GetString())); + } + } +} + +void deal_parser::fill_foundations(game_state &gs) { + for (uint8_t f_idx = 0; f_idx < 4; f_idx++) { + gs.piles[gs.foundations[f_idx]].place( + card(card::to_suit(f_idx), 1) + ); + } +} -- cgit v1.2.1 From 75f3ac710731890af853e5c45def1b97d49e0a74 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:46:25 +0000 Subject: Add deal parsing for flower garden --- .../input/json-parsing/deal_parser.cpp | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/main/input-output/input/json-parsing/deal_parser.cpp') 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 e7f8209..8bcb3b6 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -33,6 +33,11 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { parse_stock(gs, doc); } + // Construct reserve + if (gs.rules.reserve_size > 0) { + parse_reserve(gs, doc); + } + // If the foundations begin with cards in them, fill them if (gs.rules.foundations_init_card) { fill_foundations(gs); @@ -106,6 +111,24 @@ void deal_parser::parse_stock(game_state &gs, const Document& doc) { } } +void deal_parser::parse_reserve(game_state &gs, const Document& doc) { + assert(doc.HasMember("reserve")); + const Value& json_reserve_piles = doc["reserve"]; + assert(json_reserve_piles.IsArray()); + + if (gs.reserve.size() != gs.rules.reserve_size) { + json_helper::json_parse_err("Incorrect reserve size"); + } + + for (auto p = std::make_pair(begin(json_reserve_piles.GetArray()), begin(gs.reserve)); + p.second != end(gs.reserve); + ++p.first, ++p.second) { + + assert(p.first->IsString()); + gs.piles[*p.second].place(card(p.first->GetString())); + } +} + void deal_parser::fill_foundations(game_state &gs) { for (uint8_t f_idx = 0; f_idx < 4; f_idx++) { gs.piles[gs.foundations[f_idx]].place( -- cgit v1.2.1 From dc5a834c0d2d2a25f3023f812f8d85376855f0be Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 15:55:59 +0000 Subject: Add deal parser for stacked reserve (canfield) --- .../input/json-parsing/deal_parser.cpp | 26 +++++++++++++++------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'src/main/input-output/input/json-parsing/deal_parser.cpp') 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 8bcb3b6..6f0694d 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -116,16 +116,26 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) { const Value& json_reserve_piles = doc["reserve"]; assert(json_reserve_piles.IsArray()); - if (gs.reserve.size() != gs.rules.reserve_size) { - json_helper::json_parse_err("Incorrect reserve size"); + // 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"); + } } - for (auto p = std::make_pair(begin(json_reserve_piles.GetArray()), begin(gs.reserve)); - p.second != end(gs.reserve); - ++p.first, ++p.second) { - - assert(p.first->IsString()); - gs.piles[*p.second].place(card(p.first->GetString())); + 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]; + if (!gs.rules.reserve_stacked) pr += i; + gs.piles[pr].place(card(json_card_arr[i].GetString())); } } -- cgit v1.2.1