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/solver.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/solver.cpp')
| -rw-r--r-- | src/main/solver.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
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 |
