diff options
| author | Charlie Blake | 2017-11-13 09:52:23 +0000 |
|---|---|---|
| committer | GitHub | 2017-11-13 09:52:23 +0000 |
| commit | 96f9d446750e07c039ce5d482c92c464593c5bc9 (patch) | |
| tree | 5731e7eee2c041166479585daab804bc6d8e445c /src/main/solver | |
| parent | Merge pull request #19 from thecharlesblake/canfield (diff) | |
| parent | Refactor solver (diff) | |
| download | metasol-96f9d446750e07c039ce5d482c92c464593c5bc9.tar.gz metasol-96f9d446750e07c039ce5d482c92c464593c5bc9.zip | |
Merge pull request #20 from thecharlesblake/initial-code-cleanup
Initial code cleanup
Diffstat (limited to 'src/main/solver')
| -rw-r--r-- | src/main/solver/solver.cpp | 85 | ||||
| -rw-r--r-- | src/main/solver/solver.h | 43 |
2 files changed, 128 insertions, 0 deletions
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp new file mode 100644 index 0000000..f412d03 --- /dev/null +++ b/src/main/solver/solver.cpp @@ -0,0 +1,85 @@ +// +// 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; +using namespace boost; + +solver::solver(const game_state gs, const sol_rules& sr) + : root(NULL, gs), rules(sr), solution(root), states_searched(0) {} + +solver::node::node(const node* parent, const game_state& gs) : + history(solver::node::gen_history(parent)), state(gs) {} + +const vector<game_state> solver::node::gen_history(const node* parent) { + vector<game_state> hist; + if (parent != NULL) { + hist = std::vector<game_state>(parent->history); + hist.push_back(parent->state); + } else { + hist = vector<game_state>(); + } + return hist; +} + +const optional<solver::node> solver::run() { + // 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 + const 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++; + + 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; + } + + const node n(¤t, *it); + if (n.state.is_solved()) { + return n; + } else { + frontier.push_back(n); + } + } + } + + return none; +} + +const solver::node solver::get_root() const { + return root; +} + +int solver::get_states_searched() const { + return states_searched; +} + +std::ostream& operator<< (std::ostream& stream, const solver::node& solution) { + for (auto state : solution.history) { + stream << state << "\n"; + } + stream << solution.state << "\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..91c93d6 --- /dev/null +++ b/src/main/solver/solver.h @@ -0,0 +1,43 @@ +// +// 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: + struct node { + node(const node*, const game_state&); + std::vector<game_state> history; + game_state state; + + static const std::vector<game_state> gen_history(const node* parent); + }; + + solver(const game_state, const sol_rules&); + + const node get_root() const; + int get_states_searched() const; + + const boost::optional<node> run(); + + friend std::ostream& operator<< (std::ostream&, const solver::node&); + +private: + const node root; + const sol_rules rules; + node solution; + int states_searched; +}; + + +#endif //SOLVITAIRE_SOLVER_H |
