From 215496d51c6663dd12c4ffb51521f00a8bf9fcbe Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 16:07:13 +0100 Subject: Add build order to game rules class --- src/main/solver.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/solver.cpp') diff --git a/src/main/solver.cpp b/src/main/solver.cpp index 396a9dc..ac8ffb8 100644 --- a/src/main/solver.cpp +++ b/src/main/solver.cpp @@ -12,8 +12,8 @@ #include using namespace std; -solver::solver(const game_state gs) : root(gs), solution(gs), - solution_found(false) {} +solver::solver(const game_state gs, const sol_rules& sr) + : root(gs), rules(sr), solution(gs), solution_found(false) {} solver::node::node(const game_state gs) : state(gs) {} -- cgit v1.2.1 From c3e6f892599f145c14317e5712263b3e5562b785 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 22:21:03 +0100 Subject: Add generic solving and pile class --- src/main/solver.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/main/solver.cpp') diff --git a/src/main/solver.cpp b/src/main/solver.cpp index ac8ffb8..c706a01 100644 --- a/src/main/solver.cpp +++ b/src/main/solver.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "solver.h" #include "log_helper.h" @@ -34,9 +35,17 @@ bool solver::run() { // 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++) { + game_state new_state = *it; + + // If we have seen the state before, ignore it (loop detection) + if (find(begin(current.history), end(current.history), new_state) + != end(current.history)) { + continue; + } + node n = current; n.history.push_back(n.state); - n.state = *it; + n.state = new_state; frontier.push_back(n); } -- cgit v1.2.1 From 578f9c9f92d05b5907b91f07a4c732839d4dfca5 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 6 Nov 2017 14:14:38 +0000 Subject: Add counting of states searched --- src/main/solver.cpp | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'src/main/solver.cpp') diff --git a/src/main/solver.cpp b/src/main/solver.cpp index c706a01..1db92f9 100644 --- a/src/main/solver.cpp +++ b/src/main/solver.cpp @@ -14,7 +14,8 @@ using namespace std; solver::solver(const game_state gs, const sol_rules& sr) - : root(gs), rules(sr), solution(gs), solution_found(false) {} + : root(gs), rules(sr), solution(gs), solution_found(false), + states_searched(0) {} solver::node::node(const game_state gs) : state(gs) {} @@ -34,44 +35,48 @@ bool solver::run() { // 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++) { - game_state new_state = *it; + states_searched++; + + // NOTE: get_next_legal_states() makes the is_solved() function work + if (current.state.is_solved()) { + solution = current; + solution_found = true; + break; + } - // If we have seen the state before, ignore it (loop detection) - if (find(begin(current.history), end(current.history), new_state) + 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 = new_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"; + stream << sol.solution.state << "\nSolved!\n"; } else if (sol.been_run) { - stream << "No Possible Solution\n"; + 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 -- cgit v1.2.1