From db208b38d78571d68a5f92b7221175d2e67d09ed Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 16 Oct 2017 12:53:14 +0100 Subject: Move setup classes to legacy --- src/main/main.cpp | 121 ------------------------------------------------------ 1 file changed, 121 deletions(-) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index 1b75252..e69de29 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,121 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "Card.h" -#include - -#if defined NDEBUG - #define LOG_DEBUG(msg) -#else - #define LOG_DEBUG(msg) BOOST_LOG_TRIVIAL(debug) << msg -#endif -#define LOG_INFO(msg) BOOST_LOG_TRIVIAL(info) << msg -#define LOG_WARNING(msg) BOOST_LOG_TRIVIAL(warning) << msg -#define LOG_ERROR(msg) BOOST_LOG_ERROR(error) << msg - -using namespace std; - -bool search(vector* >, Card* hole); - -int main() -{ - const int topRank = 4; - - vector* > tableaux; - - vector shuffleCards; - for (int r = 1; r <= topRank; r++) { - for (int s = 0; s < 4; s++) { - string suit; - if (s == 0) { - suit = "D"; - } else if (s == 1) { - suit = "S"; - } else if (s == 2) { - suit = "H"; - } else { - suit = "C"; - } - - Card* c = new Card(r, suit); - shuffleCards.push_back(c); - } - } - - auto rng = std::default_random_engine(2); // 3 has solution - std::shuffle(std::begin(shuffleCards), std::end(shuffleCards), rng); - - for (int i = 0; i < topRank * 2; i++) { - vector* tab = new vector(); - for (int j = 0; j < 2; j++) { - int idx = i + (topRank * 2 * j); - if (shuffleCards[idx]->suit == "S" && shuffleCards[idx]->rank == 1) { - continue; - } else { - tab->push_back(shuffleCards[idx]); - } - } - tableaux.push_back(tab); - } - - Card c(2, string("H")); - LOG_DEBUG(c); - - cout << (search(tableaux, new Card(1, "S")) ? "Solution Found" : "No Solution"); - - return 0; -} - -bool search(vector* > tableaux, Card* hole) { - // If the tableaux are all empty, return true - - // Seaches through each tableau. If the top card is one away from - // the hole card, puts it in the hole and recurses. - // If that fails then put the card back and repeat for the next foundation - // If it succeeds, return true - - // If we get to the end of the function, return false - -#ifndef NDEBUG - string stateRep; - for (unsigned int i = 0; i < 2; i++) { - for (auto tableau : tableaux) { - if (tableau->size() > i) { - cout << *(*tableau)[i]; - } - cout << "\t"; - } - cout << "\n"; - } - cout << "Hole: " << *hole << "\n"; -#endif - - bool allTableauxEmpty = true; - for (unsigned int i = 0; i < tableaux.size(); i++) { - auto tableau = tableaux[i]; - - if (tableau->empty()) { - continue; - } else { - allTableauxEmpty = false; - } - - Card* topCard = tableau->back(); - if (topCard->rank == hole->rank + 1 || topCard->rank == hole->rank - 1) { - tableau->pop_back(); - - bool success = search(tableaux, topCard); - - if (success) { - return true; - } else { - tableau->push_back(topCard); - } - } - } - - return allTableauxEmpty; -} \ No newline at end of file -- cgit v1.2.1 From 9101c98f951e7b104e6969fd5984379ca04745ea Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 16 Oct 2017 14:15:25 +0100 Subject: Add demo command line parsing --- src/main/main.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index e69de29..8c07d1f 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -0,0 +1,29 @@ +#include +#include +#include "command_line_helper.h" + +using namespace std; + +namespace po = boost::program_options; + +int main(int argc, const char* argv[]) { + // Creates the command line options + po::options_description desc = get_options_desc(); + + po::variables_map vm; + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + + if (vm.count("help")) { + cout << desc << "\n"; + return 1; + } + + if (vm.count("compression")) { + cout << "Compression level was set to " + << vm["compression"].as() << ".\n"; + } else { + cout << "Compression level was not set.\n"; + } +} + -- cgit v1.2.1 From dc01ab729c749fbfd2421943198a038d126aa66c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 16 Oct 2017 22:56:16 +0100 Subject: Add simple black hole cmdline parse & err handling --- src/main/main.cpp | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index 8c07d1f..642b27c 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,29 +1,27 @@ #include -#include #include "command_line_helper.h" +#include using namespace std; namespace po = boost::program_options; int main(int argc, const char* argv[]) { - // Creates the command line options - po::options_description desc = get_options_desc(); - - po::variables_map vm; - po::store(po::parse_command_line(argc, argv, desc), vm); - po::notify(vm); - - if (vm.count("help")) { - cout << desc << "\n"; + // Parses the command-line options + command_line_helper clh; + bool success = clh.parse(argc, argv); + if (!success) { return 1; } - if (vm.count("compression")) { - cout << "Compression level was set to " - << vm["compression"].as() << ".\n"; - } else { - cout << "Compression level was not set.\n"; + cout << "Random deal: " << clh.get_random_deal() << "\n"; + cout << "Solitaire type: " << clh.get_solitaire_type() << "\n"; + cout << "Filenames: "; + for (auto filename : clh.get_input_files()) { + cout << filename << ", "; } + cout << "\n"; + + return 0; } -- cgit v1.2.1 From e9693380d952552319b2cdd3f183e2e676676f95 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 23 Oct 2017 22:01:18 +0100 Subject: Add deal file parsing and verifying --- src/main/main.cpp | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 165 insertions(+), 8 deletions(-) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index 642b27c..4d1c60f 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,27 +1,184 @@ +#include +#include + #include +#include + #include "command_line_helper.h" -#include +#include "card.h" +#include "deal_parser.h" + +using namespace rapidjson; using namespace std; namespace po = boost::program_options; +bool search(vector* >, card* hole); + int main(int argc, const char* argv[]) { // Parses the command-line options command_line_helper clh; - bool success = clh.parse(argc, argv); - if (!success) { + if (!clh.parse(argc, argv)) { return 1; } - cout << "Random deal: " << clh.get_random_deal() << "\n"; - cout << "Solitaire type: " << clh.get_solitaire_type() << "\n"; - cout << "Filenames: "; for (auto filename : clh.get_input_files()) { - cout << filename << ", "; + try { + Document d; + deal_parser::parse(d, filename); + + vector* > tableaux; + const Value& json_tabx = d["tableau piles"]; + assert(json_tabx.IsArray()); + + for (auto& json_tab : json_tabx.GetArray()) { + vector* tab = new vector(); + + for (auto& json_card : json_tab.GetArray()) { + card* c = new card(json_card.GetString()); + tab->push_back(c); + } + + tableaux.push_back(tab); + } + + bool sol = search(tableaux, new card("AS")); + + cout << (sol ? "Solution Found" : "No Solution"); + + for (vector* tab : tableaux) { + for (card* c : *tab) { + delete c; + } + delete tab; + } + } catch (runtime_error& e) { + cerr << "Error: " << e.what() << "\n"; + } + + + /* + // Reads the file into a string + std::ifstream ifstr(filename); + std::stringstream buf; + buf << ifstr.rdbuf(); + string in_json = buf.str(); + + if (ifstr.fail()) { + cerr << "Error: could not read file '" << filename << "'\n"; + //return 1; + } + + Document sd; + 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 cards\":{\"type\":\"array\",\"items\":{" + "\"$ref\":\"#/definitions/card\"}}},\"additionalProperties\":fa" + "lse,\"anyOf\":[{\"required\":[\"tableau piles\"]}]}"; + if (sd.Parse(schema_json).HasParseError()) { + // the schema is not a valid JSON. + assert(false); + } + SchemaDocument schema(sd); + + Document d; + if (d.Parse(in_json.c_str()).HasParseError()) { + // the input is not a valid JSON. + cerr << "in not valid json\n"; + } + SchemaValidator validator(schema); + if (d.Accept(validator)) { + vector* > tableaux; + const Value& json_tabx = d["tableau piles"]; + assert(json_tabx.IsArray()); + + for (auto& json_tab : json_tabx.GetArray()) { + vector* tab = new vector(); + + for (auto& json_card : json_tab.GetArray()) { + card* c = new card(json_card.GetString()); + tab->push_back(c); + } + + tableaux.push_back(tab); + } + + cout << (search(tableaux, new card("AS")) ? "Solution Found" : "No Solution"); + + for (vector* tab : tableaux) { + for (card* c : *tab) { + delete c; + } + delete tab; + } + + } else { + // Input JSON is invalid according to the schema + // Output diagnostic information + StringBuffer sb; + validator.GetInvalidSchemaPointer().StringifyUriFragment(sb); + printf("Invalid schema: %s\n", sb.GetString()); + printf("Invalid keyword: %s\n", validator.GetInvalidSchemaKeyword()); + sb.Clear(); + validator.GetInvalidDocumentPointer().StringifyUriFragment(sb); + printf("Invalid document: %s\n", sb.GetString()); + }*/ } - cout << "\n"; return 0; } +bool search(vector* > tableaux, card* hole) { + // If the tableaux are all empty, return true + + // Seaches through each tableau. If the top card is one away from + // the hole card, puts it in the hole and recurses. + // If that fails then put the card back and repeat for the next foundation + // If it succeeds, return true + + // If we get to the end of the function, return false + +#ifndef NDEBUG + string stateRep; + for (unsigned int i = 0; i < 2; i++) { + for (vector* tableau : tableaux) { + if (tableau->size() > i) { + cout << *((*tableau)[i]); + } + cout << "\t"; + } + cout << "\n"; + } + cout << "Hole: " << *hole << "\n"; +#endif + + bool allTableauxEmpty = true; + for (unsigned int i = 0; i < tableaux.size(); i++) { + auto tableau = tableaux[i]; + + if (tableau->empty()) { + continue; + } else { + allTableauxEmpty = false; + } + + card* topCard = tableau->back(); + if (topCard->get_rank() == hole->get_rank() + 1 || topCard->get_rank() == hole->get_rank() - 1) { + tableau->pop_back(); + + bool success = search(tableaux, topCard); + + if (success) { + return true; + } else { + tableau->push_back(topCard); + } + } + } + + return allTableauxEmpty; +} -- cgit v1.2.1 From c5e5d9d6bbd7d7d2a6c6463505e7b7031e846ebf Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 24 Oct 2017 01:23:00 +0100 Subject: Draft of OO game logic --- src/main/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index 4d1c60f..7e9d132 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -6,7 +6,7 @@ #include "command_line_helper.h" #include "card.h" -#include "deal_parser.h" +#include "io_utils.h" using namespace rapidjson; @@ -26,7 +26,7 @@ int main(int argc, const char* argv[]) { for (auto filename : clh.get_input_files()) { try { Document d; - deal_parser::parse(d, filename); + io_utils::parse(d, filename); vector* > tableaux; const Value& json_tabx = d["tableau piles"]; -- cgit v1.2.1 From 3eff79ed14f1fda1f148a9a4c5c3a1ae4edbee37 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 26 Oct 2017 14:51:13 +0100 Subject: BH Solver with path outputted, based on user input --- src/main/main.cpp | 165 +++++------------------------------------------------- 1 file changed, 13 insertions(+), 152 deletions(-) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index 7e9d132..a7e5871 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,12 +1,13 @@ #include -#include #include #include #include "command_line_helper.h" #include "card.h" -#include "io_utils.h" +#include "deal_parser.h" +#include "game_state.h" +#include "solver.h" using namespace rapidjson; @@ -14,8 +15,6 @@ using namespace std; namespace po = boost::program_options; -bool search(vector* >, card* hole); - int main(int argc, const char* argv[]) { // Parses the command-line options command_line_helper clh; @@ -23,162 +22,24 @@ int main(int argc, const char* argv[]) { return 1; } - for (auto filename : clh.get_input_files()) { + for (auto input_json : clh.get_input_files()) { try { - Document d; - io_utils::parse(d, filename); - - vector* > tableaux; - const Value& json_tabx = d["tableau piles"]; - assert(json_tabx.IsArray()); - - for (auto& json_tab : json_tabx.GetArray()) { - vector* tab = new vector(); - - for (auto& json_card : json_tab.GetArray()) { - card* c = new card(json_card.GetString()); - tab->push_back(c); - } - - tableaux.push_back(tab); - } + // Attempts to read the user's json into a document + Document doc; + deal_parser::parse(doc, input_json); - bool sol = search(tableaux, new card("AS")); + // Creates a game state object from the json, plus a solver + game_state gs(doc); + solver sol(gs); - cout << (sol ? "Solution Found" : "No Solution"); + cout << "Attempting to solve " << input_json << "...\n"; + sol.run(); + cout << sol; - for (vector* tab : tableaux) { - for (card* c : *tab) { - delete c; - } - delete tab; - } } catch (runtime_error& e) { cerr << "Error: " << e.what() << "\n"; } - - - /* - // Reads the file into a string - std::ifstream ifstr(filename); - std::stringstream buf; - buf << ifstr.rdbuf(); - string in_json = buf.str(); - - if (ifstr.fail()) { - cerr << "Error: could not read file '" << filename << "'\n"; - //return 1; - } - - Document sd; - 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 cards\":{\"type\":\"array\",\"items\":{" - "\"$ref\":\"#/definitions/card\"}}},\"additionalProperties\":fa" - "lse,\"anyOf\":[{\"required\":[\"tableau piles\"]}]}"; - if (sd.Parse(schema_json).HasParseError()) { - // the schema is not a valid JSON. - assert(false); - } - SchemaDocument schema(sd); - - Document d; - if (d.Parse(in_json.c_str()).HasParseError()) { - // the input is not a valid JSON. - cerr << "in not valid json\n"; - } - SchemaValidator validator(schema); - if (d.Accept(validator)) { - vector* > tableaux; - const Value& json_tabx = d["tableau piles"]; - assert(json_tabx.IsArray()); - - for (auto& json_tab : json_tabx.GetArray()) { - vector* tab = new vector(); - - for (auto& json_card : json_tab.GetArray()) { - card* c = new card(json_card.GetString()); - tab->push_back(c); - } - - tableaux.push_back(tab); - } - - cout << (search(tableaux, new card("AS")) ? "Solution Found" : "No Solution"); - - for (vector* tab : tableaux) { - for (card* c : *tab) { - delete c; - } - delete tab; - } - - } else { - // Input JSON is invalid according to the schema - // Output diagnostic information - StringBuffer sb; - validator.GetInvalidSchemaPointer().StringifyUriFragment(sb); - printf("Invalid schema: %s\n", sb.GetString()); - printf("Invalid keyword: %s\n", validator.GetInvalidSchemaKeyword()); - sb.Clear(); - validator.GetInvalidDocumentPointer().StringifyUriFragment(sb); - printf("Invalid document: %s\n", sb.GetString()); - }*/ } return 0; } - -bool search(vector* > tableaux, card* hole) { - // If the tableaux are all empty, return true - - // Seaches through each tableau. If the top card is one away from - // the hole card, puts it in the hole and recurses. - // If that fails then put the card back and repeat for the next foundation - // If it succeeds, return true - - // If we get to the end of the function, return false - -#ifndef NDEBUG - string stateRep; - for (unsigned int i = 0; i < 2; i++) { - for (vector* tableau : tableaux) { - if (tableau->size() > i) { - cout << *((*tableau)[i]); - } - cout << "\t"; - } - cout << "\n"; - } - cout << "Hole: " << *hole << "\n"; -#endif - - bool allTableauxEmpty = true; - for (unsigned int i = 0; i < tableaux.size(); i++) { - auto tableau = tableaux[i]; - - if (tableau->empty()) { - continue; - } else { - allTableauxEmpty = false; - } - - card* topCard = tableau->back(); - if (topCard->get_rank() == hole->get_rank() + 1 || topCard->get_rank() == hole->get_rank() - 1) { - tableau->pop_back(); - - bool success = search(tableaux, topCard); - - if (success) { - return true; - } else { - tableau->push_back(topCard); - } - } - } - - return allTableauxEmpty; -} -- cgit v1.2.1 From a841befd0ecdc0b469d6bc46b209f259358778ec Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 27 Oct 2017 23:25:50 +0100 Subject: Add random game generation to bh --- src/main/main.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index a7e5871..ae5e00b 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -22,6 +22,16 @@ int main(int argc, const char* argv[]) { return 1; } + int seed = clh.get_random_deal(); + if (seed >= 0) { + game_state gs(seed); + solver sol(gs); + + cout << "Attempting to solve with seed: " << seed << "...\n"; + sol.run(); + cout << sol; + } + for (auto input_json : clh.get_input_files()) { try { // Attempts to read the user's json into a document -- cgit v1.2.1 From 70522b28586ba117ff9e1e27c8f4b183f8414fd1 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 10:13:13 +0100 Subject: Add simple and regular bh types --- src/main/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index ae5e00b..cc1df9f 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -24,7 +24,7 @@ int main(int argc, const char* argv[]) { int seed = clh.get_random_deal(); if (seed >= 0) { - game_state gs(seed); + game_state gs(seed, clh.get_solitaire_type()); solver sol(gs); cout << "Attempting to solve with seed: " << seed << "...\n"; @@ -39,7 +39,7 @@ int main(int argc, const char* argv[]) { deal_parser::parse(doc, input_json); // Creates a game state object from the json, plus a solver - game_state gs(doc); + game_state gs(doc, clh.get_solitaire_type()); solver sol(gs); cout << "Attempting to solve " << input_json << "...\n"; -- cgit v1.2.1