aboutsummaryrefslogtreecommitdiff
path: root/src/main/solver.cpp
diff options
context:
space:
mode:
authorCharlie Blake2017-11-13 09:52:23 +0000
committerGitHub2017-11-13 09:52:23 +0000
commit96f9d446750e07c039ce5d482c92c464593c5bc9 (patch)
tree5731e7eee2c041166479585daab804bc6d8e445c /src/main/solver.cpp
parentMerge pull request #19 from thecharlesblake/canfield (diff)
parentRefactor solver (diff)
downloadmetasol-96f9d446750e07c039ce5d482c92c464593c5bc9.tar.gz
metasol-96f9d446750e07c039ce5d482c92c464593c5bc9.zip

Merge pull request #20 from thecharlesblake/initial-code-cleanup

Initial code cleanup

Diffstat (limited to 'src/main/solver.cpp')
-rw-r--r--src/main/solver.cpp82
1 files changed, 0 insertions, 82 deletions
diff --git a/src/main/solver.cpp b/src/main/solver.cpp
deleted file mode 100644
index 1db92f9..0000000
--- a/src/main/solver.cpp
+++ /dev/null
@@ -1,82 +0,0 @@
-//
-// Created by thecharlesblake on 10/24/17.
-//
-
-#include <string>
-#include <ostream>
-#include <iostream>
-#include <algorithm>
-
-#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, 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