diff options
| author | Charlie Blake | 2017-10-28 13:28:49 +0100 |
|---|---|---|
| committer | GitHub | 2017-10-28 13:28:49 +0100 |
| commit | 7b05ae6f24856a207f9d9ab3218f1b5e75343d74 (patch) | |
| tree | 4c7b3d779f29f5cef8a5eb1803441f1aa20ac0bd /src/main/main.cpp | |
| parent | Merge pull request #13 from thecharlesblake/docs (diff) | |
| parent | Add RapidJSON to travis (diff) | |
| download | metasol-7b05ae6f24856a207f9d9ab3218f1b5e75343d74.tar.gz metasol-7b05ae6f24856a207f9d9ab3218f1b5e75343d74.zip | |
Merge pull request #14 from thecharlesblake/simple-black-hole
Simple black hole
Diffstat (limited to 'src/main/main.cpp')
| -rw-r--r-- | src/main/main.cpp | 140 |
1 files changed, 37 insertions, 103 deletions
diff --git a/src/main/main.cpp b/src/main/main.cpp index 1b75252..cc1df9f 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,121 +1,55 @@ -#include <string> #include <iostream> -#include <vector> -#include <map> -#include <algorithm> -#include <random> -#include "Card.h" -#include <boost/log/trivial.hpp> -#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 +#include <boost/program_options.hpp> +#include <rapidjson/document.h> -using namespace std; - -bool search(vector<vector<Card*>* >, Card* hole); - -int main() -{ - const int topRank = 4; +#include "command_line_helper.h" +#include "card.h" +#include "deal_parser.h" +#include "game_state.h" +#include "solver.h" - vector<vector<Card*>* > tableaux; +using namespace rapidjson; - vector<Card*> 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); - } - } +using namespace std; - auto rng = std::default_random_engine(2); // 3 has solution - std::shuffle(std::begin(shuffleCards), std::end(shuffleCards), rng); +namespace po = boost::program_options; - for (int i = 0; i < topRank * 2; i++) { - vector<Card*>* tab = new vector<Card*>(); - 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); +int main(int argc, const char* argv[]) { + // Parses the command-line options + command_line_helper clh; + if (!clh.parse(argc, argv)) { + return 1; } - Card c(2, string("H")); - LOG_DEBUG(c); - - cout << (search(tableaux, new Card(1, "S")) ? "Solution Found" : "No Solution"); - - return 0; -} - -bool search(vector<vector<Card*>* > 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 + int seed = clh.get_random_deal(); + if (seed >= 0) { + game_state gs(seed, clh.get_solitaire_type()); + solver sol(gs); -#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 << "Attempting to solve with seed: " << seed << "...\n"; + sol.run(); + cout << sol; } - 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; - } + for (auto input_json : clh.get_input_files()) { + try { + // Attempts to read the user's json into a document + Document doc; + deal_parser::parse(doc, input_json); - Card* topCard = tableau->back(); - if (topCard->rank == hole->rank + 1 || topCard->rank == hole->rank - 1) { - tableau->pop_back(); + // Creates a game state object from the json, plus a solver + game_state gs(doc, clh.get_solitaire_type()); + solver sol(gs); - bool success = search(tableaux, topCard); + cout << "Attempting to solve " << input_json << "...\n"; + sol.run(); + cout << sol; - if (success) { - return true; - } else { - tableau->push_back(topCard); - } + } catch (runtime_error& e) { + cerr << "Error: " << e.what() << "\n"; } } - return allTableauxEmpty; -}
\ No newline at end of file + return 0; +} |
