From 25d3f7591e2b7ca5303593e1da4bc880174e189a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 2 Jan 2018 11:28:47 +0000 Subject: Add rules parsing for bakers dozen --- src/main/game/sol_rules.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'src/main/game/sol_rules.cpp') diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index 353c8db..87d556f 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -111,11 +111,11 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["tableau piles"].HasMember("build policy")) { if (d["tableau piles"]["build policy"].IsString()) { - string bo_str = d["tableau piles"]["build policy"].GetString(); + string bp_str = d["tableau piles"]["build policy"].GetString(); - if (bo_str == "any-suit") { + if (bp_str == "any-suit") { sr.build_pol = build_policy::ANY_SUIT; - } else if (bo_str == "red-black") { + } else if (bp_str == "red-black") { sr.build_pol = build_policy::RED_BLACK; } else { util::json_parse_err("[tableau piles][build policy] is invalid"); @@ -126,6 +126,23 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d["tableau piles"].HasMember("spaces policy")) { + if (d["tableau piles"]["spaces policy"].IsString()) { + string sp_str = d["tableau piles"]["spaces policy"].GetString(); + + if (sp_str == "any") { + sr.spaces_pol = spaces_policy::ANY; + } else if (sp_str == "no-build") { + sr.spaces_pol = spaces_policy::NO_BUILD; + } else { + util::json_parse_err("[tableau piles][spaces policy] is invalid"); + } + + } else { + util::json_parse_err("[tableau piles][spaces policy] must be an string"); + } + } + } else { util::json_parse_err("[tableau piles] must be an object"); } -- cgit v1.2.1 From 77a7aeba7854d15b140cc6dc3c69b15ed5f3b936 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 12:30:14 +0000 Subject: Major refactor, putting pile logic into game_state Much of the logic that was in the pile class was beginning to seem out of place. A lot of the rules have fairly arbitrary interactons *between* piles, which weren't best represented in the pile classes themselves, which were getting unweildy and inefficient. Hence, much of this logic has been moved to the game state class. --- src/main/game/sol_rules.cpp | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) (limited to 'src/main/game/sol_rules.cpp') diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index 87d556f..a1567f7 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -22,25 +22,6 @@ using namespace rapidjson; typedef sol_rules::build_policy pol; -bool sol_rules::is_suit(pol bp) { - switch (bp) { - case pol::CLUBS : - case pol::DIAMONDS : - case pol::HEARTS : - case pol::SPADES : return true; - default : return false; - } -} -card::suit_t sol_rules::suit_val(pol bp) { - switch (bp) { - case pol::CLUBS : return card::suit_t::Clubs; - case pol::DIAMONDS : return card::suit_t::Diamonds; - case pol::HEARTS : return card::suit_t::Hearts; - case pol::SPADES : return card::suit_t::Spades; - default: assert(false); return card::suit_t::Spades; - } -} - const sol_rules sol_rules::from_file(const string rules_file) { sol_rules sr = get_default(); Document d = util::get_file_json(rules_file); @@ -96,8 +77,8 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["tableau piles"]["build order"].IsString()) { string bo_str = d["tableau piles"]["build order"].GetString(); - if (bo_str == "no-build") { - sr.build_ord = build_order::NO_BUILD; + if (bo_str == "ascending") { + sr.build_ord = build_order::ASCENDING; } else if (bo_str == "descending") { sr.build_ord = build_order::DESCENDING; } else { @@ -117,6 +98,10 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { sr.build_pol = build_policy::ANY_SUIT; } else if (bp_str == "red-black") { sr.build_pol = build_policy::RED_BLACK; + } else if (bp_str == "same-suit") { + sr.build_pol = build_policy::SAME_SUIT; + } else if (bp_str == "no-build") { + sr.build_pol = build_policy::NO_BUILD; } else { util::json_parse_err("[tableau piles][build policy] is invalid"); } @@ -180,14 +165,6 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } } - if (d.HasMember("reserve size")) { - if (d["reserve size"].IsInt()) { - sr.reserve_size = static_cast(d["reserve size"].GetInt()); - } else { - util::json_parse_err("[reserve size] must be an integer"); - } - } - if (d.HasMember("stock size")) { if (d["stock size"].IsInt()) { sr.stock_size = static_cast(d["stock size"].GetInt()); -- cgit v1.2.1 From fa40f6facc5c500cae8bf890e66a7bf5d490387e Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 12:39:06 +0000 Subject: Rename util to json_helper --- src/main/game/sol_rules.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/main/game/sol_rules.cpp') diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index a1567f7..28c2af7 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -9,7 +9,7 @@ #include "rapidjson/error/en.h" #include "sol_rules.h" -#include "../util/util.h" +#include "../input-output/json_helper.h" #include "../input-output/sol_preset_types.h" using namespace std; @@ -24,7 +24,7 @@ typedef sol_rules::build_policy pol; const sol_rules sol_rules::from_file(const string rules_file) { sol_rules sr = get_default(); - Document d = util::get_file_json(rules_file); + Document d = json_helper::get_file_json(rules_file); modify_sol_rules(sr, d); @@ -59,7 +59,7 @@ sol_rules sol_rules::get_default() { void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (!d.IsObject()) { - util::json_parse_err("JSON doc must be object"); + json_helper::json_parse_err("JSON doc must be object"); } if (d.HasMember("tableau piles")) { @@ -69,7 +69,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["tableau piles"]["count"].IsInt()) { sr.tableau_pile_count = static_cast(d["tableau piles"]["count"].GetInt()); } else { - util::json_parse_err("[tableau piles][count] must be an integer"); + json_helper::json_parse_err("[tableau piles][count] must be an integer"); } } @@ -82,11 +82,11 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } else if (bo_str == "descending") { sr.build_ord = build_order::DESCENDING; } else { - util::json_parse_err("[tableau piles][build order] is invalid"); + json_helper::json_parse_err("[tableau piles][build order] is invalid"); } } else { - util::json_parse_err("[tableau piles][build order] must be an string"); + json_helper::json_parse_err("[tableau piles][build order] must be an string"); } } @@ -103,11 +103,11 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } else if (bp_str == "no-build") { sr.build_pol = build_policy::NO_BUILD; } else { - util::json_parse_err("[tableau piles][build policy] is invalid"); + json_helper::json_parse_err("[tableau piles][build policy] is invalid"); } } else { - util::json_parse_err("[tableau piles][build policy] must be an string"); + json_helper::json_parse_err("[tableau piles][build policy] must be an string"); } } @@ -120,16 +120,16 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } else if (sp_str == "no-build") { sr.spaces_pol = spaces_policy::NO_BUILD; } else { - util::json_parse_err("[tableau piles][spaces policy] is invalid"); + json_helper::json_parse_err("[tableau piles][spaces policy] is invalid"); } } else { - util::json_parse_err("[tableau piles][spaces policy] must be an string"); + json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); } } } else { - util::json_parse_err("[tableau piles] must be an object"); + json_helper::json_parse_err("[tableau piles] must be an object"); } } @@ -137,7 +137,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["max rank"].IsInt()) { sr.max_rank = static_cast(d["max rank"].GetInt()); } else { - util::json_parse_err("[max rank] must be an integer"); + json_helper::json_parse_err("[max rank] must be an integer"); } } @@ -145,7 +145,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["hole"].IsBool()) { sr.hole = d["hole"].GetBool(); } else { - util::json_parse_err("[hole] must be a boolean"); + json_helper::json_parse_err("[hole] must be a boolean"); } } @@ -153,7 +153,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["foundations"].IsBool()) { sr.foundations = d["foundations"].GetBool(); } else { - util::json_parse_err("[foundations] must be a boolean"); + json_helper::json_parse_err("[foundations] must be a boolean"); } } @@ -161,7 +161,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["cells"].IsInt()) { sr.cells = static_cast(d["cells"].GetInt()); } else { - util::json_parse_err("[cells] must be an integer"); + json_helper::json_parse_err("[cells] must be an integer"); } } @@ -169,7 +169,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["stock size"].IsInt()) { sr.stock_size = static_cast(d["stock size"].GetInt()); } else { - util::json_parse_err("[stock size] must be an integer"); + json_helper::json_parse_err("[stock size] must be an integer"); } } } -- cgit v1.2.1 From 758d919cb5d600a421636ce54df9f28b6fc832ba Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 12:51:47 +0000 Subject: Refactor sol_rules into seperate parser class --- src/main/game/sol_rules.cpp | 175 -------------------------------------------- 1 file changed, 175 deletions(-) delete mode 100644 src/main/game/sol_rules.cpp (limited to 'src/main/game/sol_rules.cpp') diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp deleted file mode 100644 index 28c2af7..0000000 --- a/src/main/game/sol_rules.cpp +++ /dev/null @@ -1,175 +0,0 @@ -// -// Created by thecharlesblake on 10/28/17. -// - -#include -#include -#include - -#include "rapidjson/error/en.h" - -#include "sol_rules.h" -#include "../input-output/json_helper.h" -#include "../input-output/sol_preset_types.h" - -using namespace std; -using namespace boost; -using namespace rapidjson; - -//////////// -// Static // -//////////// - -typedef sol_rules::build_policy pol; - -const sol_rules sol_rules::from_file(const string rules_file) { - sol_rules sr = get_default(); - Document d = json_helper::get_file_json(rules_file); - - modify_sol_rules(sr, d); - - return sr; -} - -const sol_rules sol_rules::from_preset(const string sol_type) { - sol_rules sr = get_default(); - - const string preset_json = sol_preset_types::get(sol_type); - - Document d; - d.Parse(preset_json.c_str()); - assert(!d.HasParseError()); - - modify_sol_rules(sr, d); - return sr; -} - -sol_rules sol_rules::get_default() { - sol_rules sr; - const string default_json = sol_preset_types::get("default"); - - Document d; - d.Parse(default_json.c_str()); - assert(!d.HasParseError()); - - modify_sol_rules(sr, d); - return sr; -} - -void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { - - if (!d.IsObject()) { - json_helper::json_parse_err("JSON doc must be object"); - } - - if (d.HasMember("tableau piles")) { - if (d["tableau piles"].IsObject()) { - - if (d["tableau piles"].HasMember("count")) { - if (d["tableau piles"]["count"].IsInt()) { - sr.tableau_pile_count = static_cast(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 = build_order::ASCENDING; - } else if (bo_str == "descending") { - sr.build_ord = build_order::DESCENDING; - } else { - json_helper::json_parse_err("[tableau piles][build order] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][build order] must be an string"); - } - } - - if (d["tableau piles"].HasMember("build policy")) { - if (d["tableau piles"]["build policy"].IsString()) { - string bp_str = d["tableau piles"]["build policy"].GetString(); - - if (bp_str == "any-suit") { - sr.build_pol = build_policy::ANY_SUIT; - } else if (bp_str == "red-black") { - sr.build_pol = build_policy::RED_BLACK; - } else if (bp_str == "same-suit") { - sr.build_pol = build_policy::SAME_SUIT; - } else if (bp_str == "no-build") { - sr.build_pol = build_policy::NO_BUILD; - } else { - json_helper::json_parse_err("[tableau piles][build policy] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][build policy] must be an string"); - } - } - - if (d["tableau piles"].HasMember("spaces policy")) { - if (d["tableau piles"]["spaces policy"].IsString()) { - string sp_str = d["tableau piles"]["spaces policy"].GetString(); - - if (sp_str == "any") { - sr.spaces_pol = spaces_policy::ANY; - } else if (sp_str == "no-build") { - sr.spaces_pol = spaces_policy::NO_BUILD; - } else { - json_helper::json_parse_err("[tableau piles][spaces policy] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); - } - } - - } else { - json_helper::json_parse_err("[tableau piles] must be an object"); - } - } - - if (d.HasMember("max rank")) { - if (d["max rank"].IsInt()) { - sr.max_rank = static_cast(d["max rank"].GetInt()); - } else { - json_helper::json_parse_err("[max rank] must be an integer"); - } - } - - if (d.HasMember("hole")) { - if (d["hole"].IsBool()) { - sr.hole = d["hole"].GetBool(); - } else { - json_helper::json_parse_err("[hole] must be a boolean"); - } - } - - if (d.HasMember("foundations")) { - if (d["foundations"].IsBool()) { - sr.foundations = d["foundations"].GetBool(); - } else { - json_helper::json_parse_err("[foundations] must be a boolean"); - } - } - - if (d.HasMember("cells")) { - if (d["cells"].IsInt()) { - sr.cells = static_cast(d["cells"].GetInt()); - } else { - json_helper::json_parse_err("[cells] must be an integer"); - } - } - - if (d.HasMember("stock size")) { - if (d["stock size"].IsInt()) { - sr.stock_size = static_cast(d["stock size"].GetInt()); - } else { - json_helper::json_parse_err("[stock size] must be an integer"); - } - } -} -- cgit v1.2.1