aboutsummaryrefslogtreecommitdiff
path: root/src/main/solver
diff options
context:
space:
mode:
authorthecharlesblake2017-11-11 11:36:27 +0000
committerthecharlesblake2017-11-11 11:37:10 +0000
commitbba8775ce7c63783303240470d4a4b8404c9498b (patch)
treea3f6e5dc405ecb4637eca6c54e1261b5630a058c /src/main/solver
parentMerge pull request #19 from thecharlesblake/canfield (diff)
downloadmetasol-bba8775ce7c63783303240470d4a4b8404c9498b.tar.gz
metasol-bba8775ce7c63783303240470d4a4b8404c9498b.zip

Add src file directory structure

Diffstat (limited to 'src/main/solver')
-rw-r--r--src/main/solver/solver.cpp82
-rw-r--r--src/main/solver/solver.h39
2 files changed, 121 insertions, 0 deletions
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
new file mode 100644
index 0000000..1c03d02
--- /dev/null
+++ b/src/main/solver/solver.cpp
@@ -0,0 +1,82 @@
+//
+// Created by thecharlesblake on 10/24/17.
+//
+
+#include <string>
+#include <ostream>
+#include <iostream>
+#include <algorithm>
+
+#include "solver.h"
+#include "input-output/log_helper.h"
+#include "game/game_state.h"
+#include <boost/log/trivial.hpp>
+using namespace std;
+
+solver::solver(const game_state gs, const sol_rules& sr)
+ : root(gs), rules(sr), solution(gs), solution_found(false),
+ states_searched(0) {}
+
+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();
+ states_searched++;
+
+ // NOTE: get_next_legal_states() makes the is_solved() function work
+ if (current.state.is_solved()) {
+ solution = current;
+ solution_found = true;
+ break;
+ }
+
+ for (auto it = new_children.rbegin(); it != new_children.rend(); it++) {
+ // If we have seen the state before, ignore it (loop detection))
+ if (find(begin(current.history), end(current.history), *it)
+ != end(current.history)) {
+ continue;
+ }
+
+ node n = current;
+ n.history.push_back(n.state);
+ n.state = *it;
+
+ frontier.push_back(n);
+ }
+ }
+
+ 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 << "\nSolved!\n";
+
+ } else if (sol.been_run) {
+ stream << "Deal:\n" << sol.root.state << "\n"
+ << "No Possible Solution\n";
+ } else {
+ stream << "Solver has not been run yet\n";
+ }
+ stream << "States Searched: " << sol.states_searched << "\n";
+ return stream;
+} \ No newline at end of file
diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h
new file mode 100644
index 0000000..c1a72d3
--- /dev/null
+++ b/src/main/solver/solver.h
@@ -0,0 +1,39 @@
+//
+// 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/game_state.h"
+#include "game/sol_rules.h"
+
+class solver {
+public:
+ solver(const game_state, const sol_rules&);
+ 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;
+ const sol_rules rules;
+ bool been_run;
+ node solution;
+ bool solution_found;
+ int states_searched;
+};
+
+
+#endif //SOLVITAIRE_SOLVER_H