From 3eff79ed14f1fda1f148a9a4c5c3a1ae4edbee37 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 26 Oct 2017 14:51:13 +0100 Subject: BH Solver with path outputted, based on user input --- src/main/solver.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/main/solver.cpp (limited to 'src/main/solver.cpp') diff --git a/src/main/solver.cpp b/src/main/solver.cpp new file mode 100644 index 0000000..7e7155a --- /dev/null +++ b/src/main/solver.cpp @@ -0,0 +1,68 @@ +// +// Created by thecharlesblake on 10/24/17. +// + +#include +#include +#include + +#include "solver.h" +#include "log_helper.h" +#include "game_state.h" +#include +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 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 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 -- cgit v1.2.1 From a841befd0ecdc0b469d6bc46b209f259358778ec Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 27 Oct 2017 23:25:50 +0100 Subject: Add random game generation to bh --- src/main/solver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/solver.cpp') diff --git a/src/main/solver.cpp b/src/main/solver.cpp index 7e7155a..396a9dc 100644 --- a/src/main/solver.cpp +++ b/src/main/solver.cpp @@ -29,7 +29,7 @@ bool solver::run() { node current = frontier.back(); frontier.pop_back(); - LOG_DEBUG << current.state; + LOG_DEBUG(current.state); // Create new nodes for each of the children vector new_children = current.state.get_next_legal_states(); -- cgit v1.2.1