From 079c72c083a35c5d22a70e2d54bb1567db98321b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 26 Dec 2017 12:03:35 +0000 Subject: Rework basic deal-parser outline --- src/main/input-output/deal_parser.cpp | 82 +++++++---------------------------- 1 file changed, 16 insertions(+), 66 deletions(-) (limited to 'src/main/input-output/deal_parser.cpp') diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index 8a9cef1..9fcd1d1 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -10,87 +10,37 @@ #include #include #include -#include #include "deal_parser.h" using namespace std; using namespace rapidjson; -using namespace boost; -void deal_parser::parse(Document& doc, const std::string filename) { - const optional in_json = read_file(filename); +deal_parser::deal_parser(const sol_rules &r) : rules(r) {} - if (!in_json) { - throw runtime_error("could not read file " + filename); - } - - Document sd; - gen_schema_doc(sd); - SchemaDocument schema(sd); - SchemaValidator validator(schema); - - if(!to_json(doc, *in_json)) { - throw runtime_error(filename + " not valid json"); - } +game_state deal_parser::parse(const string& filename) const { + const Document doc = read_file(filename); - if (!doc.Accept(validator)) { - throw runtime_error(schema_err_str(validator)); - } + game_state gs; + return gs; } -const optional deal_parser::read_file(const string filename) { +Document deal_parser::read_file(const string& filename) { // Reads the file into a string std::ifstream ifstr(filename); std::stringstream buf; buf << ifstr.rdbuf(); if (ifstr.fail()) { - return none; + throw runtime_error("could not read file " + filename); + } else { + Document d; + + d.Parse(buf.str().c_str()); + if (d.HasParseError()) { + throw runtime_error(filename + " not valid json"); + } else { + return d; + } } - - return buf.str(); -} - -void deal_parser::gen_schema_doc(Document& d) { - const char* schema_json = "{\"$schema\":\"http://json-schema.org/draft-04/sc" - "hema#\",\"definitions\":{\"card\":{\"type\":\"string\",\"pattern" - "\":\"^(?i)([A|[1-9]|1[0-3]|J|Q|K])([C|D|H|S])$\"}},\"type\":\"" - "object\",\"properties\":{\"tableau piles\":{\"type\":\"array\"" - ",\"items\":{\"type\":\"array\",\"items\":{\"$ref\":\"#/definit" - "ions/card\"}}},\"hole card\":{\"$ref\":\"#/definitions/card\"}}" - ",\"additionalProperties\":false,\"anyOf\":[{\"required\":[\"tableau" - " piles\"]}]}"; - - to_json(d, schema_json); -} - -bool deal_parser::to_json(rapidjson::Document& d, const char* in) { - d.Parse(in); - return !d.HasParseError(); -} - -bool deal_parser::to_json(rapidjson::Document& d, const string in) { - return to_json(d, in.c_str()); } - -const string deal_parser::schema_err_str(const SchemaValidator& validator) { - // Input JSON is invalid according to the schema - // Output diagnostic information - string ret = "input JSON failed to match the required schema.\n"; - - StringBuffer sb; - validator.GetInvalidSchemaPointer().StringifyUriFragment(sb); - ret += "Invalid schema keyword: "; - ret += validator.GetInvalidSchemaKeyword(); - ret += "\n"; - - sb.Clear(); - validator.GetInvalidDocumentPointer().StringifyUriFragment(sb); - - ret += "Invalid document: "; - ret += sb.GetString(); - ret += "\n"; - - return ret; -} \ No newline at end of file -- cgit v1.2.1 From bf23d041cb9e7d5f1dcad6b26a93c43a69fce8bc Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 26 Dec 2017 13:00:48 +0000 Subject: Refactor game state to prep for deal parse --- src/main/input-output/deal_parser.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'src/main/input-output/deal_parser.cpp') diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index 9fcd1d1..76da77b 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -21,7 +21,32 @@ deal_parser::deal_parser(const sol_rules &r) : rules(r) {} game_state deal_parser::parse(const string& filename) const { const Document doc = read_file(filename); - game_state gs; + game_state gs(rules); + /* + // Construct tableau piles + assert(doc.HasMember("tableau piles")); + const Value& json_tab_piles = doc["tableau piles"]; + assert(json_tab_piles.IsArray()); + + int max_rank = 1; + for (auto& json_tab : json_tab_piles.GetArray()) { + pile tableau_pile(true, rules.build_ord, pol::ANY_SUIT, false); + + for (auto& json_card : json_tab.GetArray()) { + card c(json_card.GetString()); + if (c.get_rank() > max_rank) max_rank = c.get_rank(); + tableau_pile.place(c); + } + + tableau_piles.push_back(tableau_pile); + } + + // Assign hole card + assert(doc.HasMember("hole card")); + const Value& json_h_card = doc["hole card"]; + assert(json_h_card.IsString()); + hole.place(json_h_card.GetString()); + */ return gs; } -- cgit v1.2.1 From b8f4cae869eace06df73ac7c1c390cc42cfe499b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 26 Dec 2017 17:29:24 +0000 Subject: Add basic tableau deal parsing --- src/main/input-output/deal_parser.cpp | 63 +++++++++-------------------------- 1 file changed, 16 insertions(+), 47 deletions(-) (limited to 'src/main/input-output/deal_parser.cpp') diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index 76da77b..3ad973f 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -2,70 +2,39 @@ // Created by thecharlesblake on 10/21/17. // -#include -#include -#include -#include - -#include -#include -#include - #include "deal_parser.h" +#include "../game/game_state.h" +#include "../util/util.h" using namespace std; using namespace rapidjson; -deal_parser::deal_parser(const sol_rules &r) : rules(r) {} - -game_state deal_parser::parse(const string& filename) const { - const Document doc = read_file(filename); +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; - game_state gs(rules); - /* +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); + } +} + +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()); - int max_rank = 1; for (auto& json_tab : json_tab_piles.GetArray()) { - pile tableau_pile(true, rules.build_ord, pol::ANY_SUIT, false); + pile tableau_pile(true, gs.rules.build_ord, pol::ANY_SUIT, false); for (auto& json_card : json_tab.GetArray()) { - card c(json_card.GetString()); - if (c.get_rank() > max_rank) max_rank = c.get_rank(); - tableau_pile.place(c); + tableau_pile.place(card(json_card.GetString())); } - tableau_piles.push_back(tableau_pile); - } - - // Assign hole card - assert(doc.HasMember("hole card")); - const Value& json_h_card = doc["hole card"]; - assert(json_h_card.IsString()); - hole.place(json_h_card.GetString()); - */ - return gs; -} - -Document deal_parser::read_file(const string& filename) { - // Reads the file into a string - std::ifstream ifstr(filename); - std::stringstream buf; - buf << ifstr.rdbuf(); - - if (ifstr.fail()) { - throw runtime_error("could not read file " + filename); - } else { - Document d; + gs.tableau_piles.push_back(tableau_pile); - d.Parse(buf.str().c_str()); - if (d.HasParseError()) { - throw runtime_error(filename + " not valid json"); - } else { - return d; + if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) { + util::json_parse_err("Incorrect number of tableau piles"); } } } -- cgit v1.2.1 From f8931f348ef456d9fac78b5cadad280f81dfcfc6 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 27 Dec 2017 00:04:32 +0000 Subject: First sp test working --- src/main/input-output/deal_parser.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'src/main/input-output/deal_parser.cpp') diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index 3ad973f..fecc7d9 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -24,17 +24,16 @@ 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()); - for (auto& json_tab : json_tab_piles.GetArray()) { - pile tableau_pile(true, gs.rules.build_ord, pol::ANY_SUIT, false); - - for (auto& json_card : json_tab.GetArray()) { - tableau_pile.place(card(json_card.GetString())); - } + if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) { + util::json_parse_err("Incorrect number of tableau piles"); + } - gs.tableau_piles.push_back(tableau_pile); + 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) { - if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) { - util::json_parse_err("Incorrect number of tableau piles"); + for (auto& json_card : p.first->GetArray()) { + p.second->place(card(json_card.GetString())); } } } -- cgit v1.2.1 From 3759513307f324761b9bf6f4ef20ebeb9c3da98a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 27 Dec 2017 12:04:33 +0000 Subject: All basic bh tests complete --- src/main/input-output/deal_parser.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/main/input-output/deal_parser.cpp') diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index fecc7d9..bf4ebaa 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -17,6 +17,11 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { if (gs.rules.tableau_pile_count > 0) { parse_tableau_piles(gs, doc); } + + // Construct hole card + if (gs.rules.hole) { + parse_hole(gs, doc); + } } void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { @@ -37,3 +42,11 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& } } } + +void deal_parser::parse_hole(game_state &gs, const rapidjson::Document& doc) { + assert(doc.HasMember("hole")); + const Value& json_hole = doc["hole"]; + assert(json_hole.IsString()); + + gs.hole.place(card(json_hole.GetString())); +} -- cgit v1.2.1 From 588a36c63991d24566d4af6f1b9722186bdcc286 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 27 Dec 2017 23:55:34 +0000 Subject: Added fc tests - currently intract. --- src/main/input-output/deal_parser.cpp | 40 ++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'src/main/input-output/deal_parser.cpp') diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index bf4ebaa..2ff5a02 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -22,6 +22,11 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { 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) { @@ -38,15 +43,40 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& ++p.first, ++p.second) { for (auto& json_card : p.first->GetArray()) { + assert(json_card.IsString()); p.second->place(card(json_card.GetString())); } } } -void deal_parser::parse_hole(game_state &gs, const rapidjson::Document& doc) { - assert(doc.HasMember("hole")); - const Value& json_hole = doc["hole"]; - assert(json_hole.IsString()); +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.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()); - gs.hole.place(card(json_hole.GetString())); + const auto json_cell_arr = json_cells.GetArray(); + + if (json_cell_arr.Empty()) return; + else if (gs.rules.cells != json_cell_arr.Size()) { + util::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()); + p.second->place(json_card->GetString()); + } + } } -- cgit v1.2.1