aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/Card.cpp25
-rw-r--r--src/main/Card.h19
-rw-r--r--src/main/card.cpp84
-rw-r--r--src/main/card.h26
-rw-r--r--src/main/command_line_helper.cpp143
-rw-r--r--src/main/command_line_helper.h36
-rw-r--r--src/main/deal_parser.cpp96
-rw-r--r--src/main/deal_parser.h27
-rw-r--r--src/main/game_state.cpp161
-rw-r--r--src/main/game_state.h38
-rw-r--r--src/main/log_helper.h19
-rw-r--r--src/main/main.cpp140
-rw-r--r--src/main/solver.cpp68
-rw-r--r--src/main/solver.h36
14 files changed, 771 insertions, 147 deletions
diff --git a/src/main/Card.cpp b/src/main/Card.cpp
deleted file mode 100644
index ead2fd5..0000000
--- a/src/main/Card.cpp
+++ /dev/null
@@ -1,25 +0,0 @@
-//
-// Created by thecharlesblake on 10/11/17.
-//
-
-#include "Card.h"
-#include <string>
-
-Card::Card(const int r, const std::string s): rank(r), suit(s) {}
-
-std::ostream & operator<<(std::ostream & Str, Card const & c) {
- std::string r;
- if (c.rank == 1) {
- r = "A";
- } else if (c.rank <= 10) {
- r = std::to_string(c.rank);
- } else if (c.rank == 11) {
- r = "J";
- } else if (c.rank == 12) {
- r = "Q";
- } else if (c.rank == 13) {
- r = "K";
- }
-
- return Str << r << c.suit;
-} \ No newline at end of file
diff --git a/src/main/Card.h b/src/main/Card.h
deleted file mode 100644
index 651df71..0000000
--- a/src/main/Card.h
+++ /dev/null
@@ -1,19 +0,0 @@
-//
-// Created by thecharlesblake on 10/11/17.
-//
-
-#ifndef SOLITAIRE_PIP_CARD_H
-#define SOLITAIRE_PIP_CARD_H
-
-#include <string>
-
-struct Card {
- Card(const int, const std::string);
- const int rank;
- const std::string suit;
-};
-
-std::ostream & operator<<(std::ostream &, Card const &);
-
-
-#endif //SOLITAIRE_PIP_CARD_H
diff --git a/src/main/card.cpp b/src/main/card.cpp
new file mode 100644
index 0000000..0b7c0b8
--- /dev/null
+++ b/src/main/card.cpp
@@ -0,0 +1,84 @@
+//
+// Created by thecharlesblake on 10/11/17.
+//
+
+#include <sstream>
+#include <ctype.h>
+
+#include "card.h"
+
+using namespace std;
+
+card::card() : rank(-1), suit(-1) {}
+
+card::card(int r, int s) : rank(r), suit(s) {}
+
+card::card(const char* c) : rank(-1), suit(-1) {
+ bool letter = false;
+ switch(tolower(c[0])) {
+ case 'a': rank = 1;
+ letter = true;
+ break;
+ case 'j': rank = 11;
+ letter = true;
+ break;
+ case 'q': rank = 12;
+ letter = true;
+ break;
+ case 'k': rank = 13;
+ letter = true;
+ break;
+ default: rank = atoi(c);
+ }
+
+ char s;
+ if (rank >= 10 && !letter) {
+ s = c[2];
+ } else {
+ s = c[1];
+ }
+
+ s = tolower(s);
+
+ if (s == 's') {
+ suit = 0;
+ } else if (s == 'h') {
+ suit = 1;
+ } else if (s == 'd') {
+ suit = 2;
+ } else if (s == 'c') {
+ suit = 3;
+ }
+}
+
+int card::get_rank() const {
+ return rank;
+}
+
+char card::get_suit() const {
+ switch (suit) {
+ case 0: return 'S';
+ case 1: return 'H';
+ case 2: return 'D';
+ case 3: return 'C';
+ default: return 'X';
+ }
+}
+
+std::ostream & operator<<(std::ostream & s, card const & c) {
+ std::string r;
+ if (c.get_rank() == 1) {
+ s << "A";
+ } else if (c.get_rank() <= 10) {
+ s << c.get_rank();
+ } else if (c.get_rank() == 11) {
+ s << "J";
+ } else if (c.get_rank() == 12) {
+ s << "Q";
+ } else if (c.get_rank() == 13) {
+ s << "K";
+ }
+
+ s << c.get_suit();
+ return s;
+} \ No newline at end of file
diff --git a/src/main/card.h b/src/main/card.h
new file mode 100644
index 0000000..13507f0
--- /dev/null
+++ b/src/main/card.h
@@ -0,0 +1,26 @@
+//
+// Created by thecharlesblake on 10/11/17.
+//
+
+#ifndef SOLITAIRE_PIP_CARD_H
+#define SOLITAIRE_PIP_CARD_H
+
+#include <string>
+
+class card {
+public:
+ card();
+ card(int, int);
+ card(const char*);
+ int get_rank() const;
+ char get_suit() const;
+
+private:
+ int rank;
+ int suit;
+};
+
+std::ostream & operator<<(std::ostream &, card const &);
+
+
+#endif //SOLITAIRE_PIP_CARD_H
diff --git a/src/main/command_line_helper.cpp b/src/main/command_line_helper.cpp
new file mode 100644
index 0000000..fdf6c9c
--- /dev/null
+++ b/src/main/command_line_helper.cpp
@@ -0,0 +1,143 @@
+//
+// Created by thecharlesblake on 10/16/17.
+//
+
+#include <tuple>
+#include <vector>
+#include <iostream>
+
+#include <boost/program_options.hpp>
+
+#include "command_line_helper.h"
+
+using namespace std;
+
+namespace po = boost::program_options;
+
+command_line_helper::command_line_helper()
+ : main_options("options"), random_deal(-1) {
+
+ main_options.add_options()
+ ("help", "produce help message")
+ ("type", po::value<string>(),
+ "specify the type of the solitaire game to be solved (REQUIRED)")
+ ("random", po::value<int>(), "create and solve a random solitaire "
+ "deal based on a seed");
+
+ po::options_description hidden_options("Hidden options");
+ hidden_options.add_options()
+ ("input-files", po::value<vector<string>>(),
+ "the file containing the deal to be solved");
+
+ cmdline_options.add(main_options).add(hidden_options);
+
+ p.add("input-files", -1);
+}
+
+bool command_line_helper::parse(int argc, const char* argv[]) {
+ po::variables_map vm;
+ try {
+ store(po::command_line_parser(argc, argv).options(cmdline_options)
+ .positional(p).run(), vm);
+ } catch(po::error& e) {
+ cerr << "Error: " << e.what() << "\n";
+ return false;
+ }
+
+ notify(vm);
+
+ help = vm.count("help");
+
+ if (vm.count("input-files")) {
+ input_files = vm["input-files"].as<vector<string>>();
+ }
+
+ if (vm.count("type")) {
+ solitaire_type = vm["type"].as<string>();
+ }
+
+ if (vm.count("random")) {
+ random_deal = vm["random"].as<int>();
+ }
+
+ // Handle logic error scenarios
+ return assess_errors();
+}
+
+bool command_line_helper::assess_errors() {
+ if (help) {
+ print_help();
+ return false;
+ }
+
+ if (solitaire_type.empty()) {
+ print_no_sol_type_error();
+ return false;
+ }
+
+ bool valid_sol_type = assess_sol_type();
+ if (!valid_sol_type) return false;
+
+ if (random_deal >= 0 && !input_files.empty()) {
+ print_rand_plus_input_err();
+ return false;
+ }
+
+ if (input_files.empty() && random_deal >= 0) {
+ print_no_input_error();
+ return false;
+ }
+ return true;
+}
+
+bool command_line_helper::assess_sol_type() {
+ if (solitaire_type == "black-hole") {
+ return true;
+ } else if (solitaire_type == "simple-black-hole") {
+ return true;
+ } else {
+ cerr << "Error: Solitaire type is not valid: " << solitaire_type
+ << "\nValid solitaire types are: 'black-hole' and "
+ "'simple-black-hole'";
+ print_help();
+ return false;
+ }
+}
+
+void command_line_helper::print_help() {
+ cout << "Usage: solvitaire [options] input-file1 input-file2 ...\n"
+ << main_options << "\n";
+}
+
+void command_line_helper::print_no_input_error() {
+ cerr << "Error: User must supply input file(s) or the '--random' "
+ "option\n";
+ print_help();
+}
+
+void command_line_helper::print_no_sol_type_error() {
+ cerr << "Error: User must supply a solitaire type\n";
+ print_help();
+}
+
+void command_line_helper::print_rand_plus_input_err() {
+ cerr << "Error: User must supply input file(s) or the '--random' option, "
+ "not both\n";
+ print_help();
+}
+
+const vector<string> command_line_helper::get_input_files() {
+ return input_files;
+}
+
+const string command_line_helper::get_solitaire_type() {
+ return solitaire_type;
+}
+
+int command_line_helper::get_random_deal() {
+ return random_deal;
+}
+
+bool command_line_helper::get_help() {
+ return help;
+}
diff --git a/src/main/command_line_helper.h b/src/main/command_line_helper.h
new file mode 100644
index 0000000..5694830
--- /dev/null
+++ b/src/main/command_line_helper.h
@@ -0,0 +1,36 @@
+//
+// Created by thecharlesblake on 10/16/17.
+//
+
+#ifndef SOLVITAIRE_COMMAND_LINE_HELPER_H
+#define SOLVITAIRE_COMMAND_LINE_HELPER_H
+
+#include <boost/program_options.hpp>
+
+class command_line_helper {
+public:
+ command_line_helper();
+ bool parse(int argc, const char* argv[]);
+ const std::vector<std::string> get_input_files();
+ const std::string get_solitaire_type();
+ int get_random_deal();
+ bool get_help();
+private:
+ bool assess_errors();
+ bool assess_sol_type();
+ void print_help();
+ void print_no_input_error();
+ void print_no_sol_type_error();
+ void print_rand_plus_input_err();
+
+ boost::program_options::options_description cmdline_options;
+ boost::program_options::options_description main_options;
+ boost::program_options::positional_options_description p;
+
+ std::vector<std::string> input_files;
+ std::string solitaire_type;
+ int random_deal;
+ bool help;
+};
+
+#endif //SOLVITAIRE_COMMAND_LINE_HELPER_H
diff --git a/src/main/deal_parser.cpp b/src/main/deal_parser.cpp
new file mode 100644
index 0000000..8a9cef1
--- /dev/null
+++ b/src/main/deal_parser.cpp
@@ -0,0 +1,96 @@
+//
+// Created by thecharlesblake on 10/21/17.
+//
+
+#include <vector>
+#include <fstream>
+#include <iostream>
+#include <sstream>
+
+#include <boost/optional.hpp>
+#include <rapidjson/document.h>
+#include <rapidjson/schema.h>
+#include <rapidjson/stringbuffer.h>
+
+#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<string> in_json = read_file(filename);
+
+ 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");
+ }
+
+ if (!doc.Accept(validator)) {
+ throw runtime_error(schema_err_str(validator));
+ }
+}
+
+const optional<string> 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;
+ }
+
+ 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
diff --git a/src/main/deal_parser.h b/src/main/deal_parser.h
new file mode 100644
index 0000000..99523f6
--- /dev/null
+++ b/src/main/deal_parser.h
@@ -0,0 +1,27 @@
+//
+// Created by thecharlesblake on 10/21/17.
+//
+
+#ifndef SOLVITAIRE_DEAL_PARSER_H
+#define SOLVITAIRE_DEAL_PARSER_H
+
+#include <vector>
+
+#include <boost/optional.hpp>
+#include <rapidjson/document.h>
+#include <rapidjson/schema.h>
+
+#include "card.h"
+
+class deal_parser {
+public:
+ static void parse(rapidjson::Document&, const std::string);
+private:
+ static const boost::optional<std::string> read_file(const std::string);
+ static void gen_schema_doc(rapidjson::Document&);
+ static bool to_json(rapidjson::Document&, const char*);
+ static bool to_json(rapidjson::Document&, const std::string);
+ static const std::string schema_err_str(const rapidjson::SchemaValidator&);
+};
+
+#endif //SOLVITAIRE_DEAL_PARSER_H
diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp
new file mode 100644
index 0000000..ef9db97
--- /dev/null
+++ b/src/main/game_state.cpp
@@ -0,0 +1,161 @@
+//
+// Created by thecharlesblake on 10/24/17.
+//
+
+#include <vector>
+#include <ostream>
+#include <numeric>
+#include <random>
+#include <algorithm>
+
+#include <rapidjson/document.h>
+
+#include "game_state.h"
+
+using namespace rapidjson;
+using namespace std;
+
+// Construct an initial game state from a JSON doc
+game_state::game_state(const Document& doc, string sol_type)
+ : max_rank(1) {
+ simple = sol_type == "simple-black-hole";
+
+ // Construct tableau piles
+ assert(doc.HasMember("tableau piles"));
+ const Value& json_tab_piles = doc["tableau piles"];
+ assert(json_tab_piles.IsArray());
+
+ for (auto& json_tab : json_tab_piles.GetArray()) {
+ vector<card> tableau_pile;
+
+ 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.push_back(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_card = json_h_card.GetString();
+}
+
+// Construct an initial game state from a seed
+game_state::game_state(int seed, string sol_type) {
+ simple = sol_type == "simple-black-hole";
+ max_rank = simple ? 7 : 13;
+
+ int start = 1; // Ignore the first (hole) card
+ int end = max_rank * 4;
+
+ // Create a vector of pointers to ints from [start, end)
+ vector<int*> v;
+ for (int i = start; i < end; i++) {
+ v.push_back(new int(i));
+ }
+
+ // Randomly shuffle the pointers
+ auto rng = std::default_random_engine(seed);
+ std::shuffle(std::begin(v), std::end(v), rng);
+
+ for (vector<int*>::size_type i = 0; i < v.size(); i++) {
+ int r = ((*v[i]) % max_rank);
+ int s = (*v[i]) / max_rank;
+
+ // Add the randomly generated card to the tableau piles
+ if (i % 3 == 0) tableau_piles.push_back(vector<card>());
+ tableau_piles[i / 3].push_back(card(r + 1, s));
+ }
+
+ hole_card = "AS";
+
+ // release memory
+ for (int i = start; i < max_rank; i++) {
+ delete v[i];
+ }
+}
+
+vector<game_state> game_state::get_next_legal_states() const {
+ vector<game_state> next;
+
+ // Searches through each column for a top card that can be moved to the hole
+ bool all_tableaux_empty = true;
+ for (vector<vector<card>>::size_type i = 0; i < tableau_piles.size(); i++) {
+ vector<card> tableau_pile = tableau_piles[i];
+
+ // Ignore empty piles
+ if (tableau_pile.empty()) {
+ continue;
+ } else {
+ all_tableaux_empty = false;
+ }
+ card top_card = tableau_pile.back();
+
+ // If a card at the top of a tableau pile is adjacent to the hole card,
+ // move it to the hole and add that state
+ if (adjacent(top_card, hole_card)) {
+ game_state s = *this;
+ s.move_to_hole(i);
+ next.push_back(s);
+ }
+ }
+
+ // If there are no cards left in play, deal solved
+ solved = all_tableaux_empty;
+
+ return next;
+}
+
+bool game_state::adjacent(const card& a, const card& b) const {
+ int x = a.get_rank();
+ int y = b.get_rank();
+
+ return x == y + 1
+ || x + 1 == y
+ || (x == 1 && y == max_rank)
+ || (x == max_rank && y == 1);
+}
+
+void game_state::move_to_hole(int tab_idx) {
+ hole_card = tableau_piles[tab_idx].back();
+ tableau_piles[tab_idx].pop_back();
+}
+
+bool game_state::is_solved() const {
+ return solved;
+}
+
+ostream& game_state::print(ostream& stream) const {
+ bool empty_row = false;
+ vector<card>::size_type row_idx = 0;
+
+ while (!empty_row) {
+ empty_row = true;
+
+ for (vector<card> tableau_pile : tableau_piles) {
+ if (tableau_pile.size() > row_idx) {
+ empty_row = false;
+ stream << tableau_pile[row_idx];
+ }
+ stream << "\t";
+ }
+
+ if (!empty_row) stream << "|";
+ if (row_idx == 0) {
+ stream << " Hole card: " << hole_card;
+ }
+
+ stream << "\n";
+ row_idx++;
+ }
+
+ return stream;
+}
+
+ostream& operator <<(ostream& stream, const game_state& gs) {
+ return gs.print(stream);
+} \ No newline at end of file
diff --git a/src/main/game_state.h b/src/main/game_state.h
new file mode 100644
index 0000000..ff92247
--- /dev/null
+++ b/src/main/game_state.h
@@ -0,0 +1,38 @@
+//
+// Created by thecharlesblake on 10/24/17.
+//
+
+#ifndef SOLVITAIRE_GAME_STATE_H
+#define SOLVITAIRE_GAME_STATE_H
+
+#include <vector>
+#include <ostream>
+#include <string>
+
+#include <rapidjson/document.h>
+
+#include "card.h"
+
+class game_state {
+public:
+ game_state(const rapidjson::Document&, std::string);
+ game_state(int seed, std::string);
+ std::vector<game_state> get_next_legal_states() const;
+ bool is_solved() const;
+ std::ostream& print(std::ostream&) const;
+
+ friend std::ostream& operator<< (std::ostream&, const game_state&);
+
+private:
+ void move_to_hole(int);
+ bool adjacent(const card& a, const card& b) const;
+ bool simple;
+
+ std::vector<std::vector<card> > tableau_piles;
+ int max_rank;
+ card hole_card;
+ mutable bool solved;
+};
+
+
+#endif //SOLVITAIRE_GAME_STATE_H
diff --git a/src/main/log_helper.h b/src/main/log_helper.h
new file mode 100644
index 0000000..889a5ae
--- /dev/null
+++ b/src/main/log_helper.h
@@ -0,0 +1,19 @@
+//
+// Created by thecharlesblake on 10/16/17.
+//
+
+#ifndef SOLVITAIRE_LOG_HELPER_H
+#define SOLVITAIRE_LOG_HELPER_H
+
+#include <iostream>
+
+#if defined NDEBUG
+ #define LOG_DEBUG
+#else
+ #define LOG_DEBUG(msg) clog << "[debug]\n" << msg
+#endif
+#define LOG_INFO(msg) clog << "[info]\n" << msg
+#define LOG_WARNING(msg) clog << "[warning]\n" << msg
+#define LOG_ERROR(msg) cerr << "[error]\n" << msg
+
+#endif //SOLVITAIRE_LOG_HELPER_H
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;
+}
diff --git a/src/main/solver.cpp b/src/main/solver.cpp
new file mode 100644
index 0000000..396a9dc
--- /dev/null
+++ b/src/main/solver.cpp
@@ -0,0 +1,68 @@
+//
+// Created by thecharlesblake on 10/24/17.
+//
+
+#include <string>
+#include <ostream>
+#include <iostream>
+
+#include "solver.h"
+#include "log_helper.h"
+#include "game_state.h"
+#include <boost/log/trivial.hpp>
+using namespace std;
+
+solver::solver(const game_state gs) : root(gs), solution(gs),
+ solution_found(false) {}
+
+solver::node::node(const game_state gs) : state(gs) {}
+
+bool solver::run() {
+ been_run = true;
+
+ // Iteratively run a depth-first search. Nodes in the frontier are yet to
+ // be explored
+ vector<node> frontier;
+ frontier.push_back(root);
+ while (!frontier.empty()) {
+ // Pop the element at the top of the stack
+ node current = frontier.back();
+ frontier.pop_back();
+
+ LOG_DEBUG(current.state);
+
+ // Create new nodes for each of the children
+ vector<game_state> new_children = current.state.get_next_legal_states();
+ for (auto it = new_children.rbegin(); it != new_children.rend(); it++) {
+ node n = current;
+ n.history.push_back(n.state);
+ n.state = *it;
+
+ frontier.push_back(n);
+ }
+
+ if (current.state.is_solved()) {
+ solution = current;
+ solution_found = true;
+ break;
+ }
+ }
+
+ return solution_found;
+}
+
+std::ostream& operator<< (std::ostream& stream, const solver& sol) {
+ if (sol.solution_found) {
+ stream << "Solution:\n";
+ for (auto state : sol.solution.history) {
+ stream << state << "\n";
+ }
+ stream << sol.solution.state << "\n";
+
+ } else if (sol.been_run) {
+ stream << "No Possible Solution\n";
+ } else {
+ stream << "Solver has not been run yet\n";
+ }
+ return stream;
+} \ No newline at end of file
diff --git a/src/main/solver.h b/src/main/solver.h
new file mode 100644
index 0000000..0520cc7
--- /dev/null
+++ b/src/main/solver.h
@@ -0,0 +1,36 @@
+//
+// Created by thecharlesblake on 10/24/17.
+//
+
+#ifndef SOLVITAIRE_SOLVER_H
+#define SOLVITAIRE_SOLVER_H
+
+#include <string>
+#include <vector>
+
+#include <boost/optional.hpp>
+
+#include "game_state.h"
+
+class solver {
+public:
+ solver(const game_state);
+ bool run();
+
+ friend std::ostream& operator<< (std::ostream&, const solver&);
+
+private:
+ struct node {
+ node(const game_state);
+ std::vector<game_state> history;
+ game_state state;
+ };
+
+ const node root;
+ bool been_run;
+ node solution;
+ bool solution_found;
+};
+
+
+#endif //SOLVITAIRE_SOLVER_H