diff options
| author | thecharlesblake | 2018-01-01 17:56:54 +0000 |
|---|---|---|
| committer | thecharlesblake | 2018-01-01 17:56:54 +0000 |
| commit | 74d43eaf200d42a3060de0f7b458902d52274225 (patch) | |
| tree | 029c1e95287838fc82b2e1464c89bf582db94fde /src/main/solver | |
| parent | Remove Canfield from canonical solitaires (diff) | |
| download | metasol-74d43eaf200d42a3060de0f7b458902d52274225.tar.gz metasol-74d43eaf200d42a3060de0f7b458902d52274225.zip | |
Fix bug in solver - improper ref to vector element
Diffstat (limited to 'src/main/solver')
| -rw-r--r-- | src/main/solver/solver.cpp | 19 | ||||
| -rw-r--r-- | src/main/solver/solver.h | 2 |
2 files changed, 11 insertions, 10 deletions
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 23ed693..694ad4e 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -18,7 +18,7 @@ solver::solver(const game_state& gs, const sol_rules& sr) : initial_state(gs), rules(sr), states_searched(0) {} solver::node::node(const game_state::move m, - vector<game_state::move> &&uc) + vector<game_state::move> uc) : move(m), unsearched_children(std::move(uc)) { } @@ -27,10 +27,10 @@ bool solver::run() { frontier.emplace_back(NULL_MOVE, state.get_legal_moves()); states_searched++; + LOG_DEBUG (state); while (!frontier.empty()) { node& current = frontier.back(); - LOG_DEBUG (state); // If we have a solution, returns true if (state.is_solved()) return true; @@ -45,23 +45,24 @@ bool solver::run() { // Returns to the previous state frontier.pop_back(); } else { + game_state::move next_move = current.unsearched_children[current.unsearched_children.size()-1]; + current.unsearched_children.pop_back(); + // Applies the first possible move in this state - state.make_move(current.unsearched_children.back()); + state.make_move(next_move); // Insert the state into the global cache bool is_new_state = global_cache.insert(state.get_data()).second; if (is_new_state) { // If the state is new, creates a new node in the frontier to // represent the move just made, and those that are now possible - frontier.emplace_back(current.unsearched_children.back(), - state.get_legal_moves()); + frontier.emplace_back(next_move, state.get_legal_moves()); states_searched++; + LOG_DEBUG (state); } else { - // If we've seen the state before, undo the move - state.undo_move(current.unsearched_children.back()); + // If we've seen the state before, undoes the move + state.undo_move(next_move); } - // Removes the move just made from the list of possible moves - current.unsearched_children.pop_back(); } } diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 1ef841d..563a11e 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -20,7 +20,7 @@ public: > global_cache; struct node { - explicit node(const game_state::move, std::vector<game_state::move>&&); + explicit node(game_state::move, std::vector<game_state::move>); const game_state::move move; std::vector<game_state::move> unsearched_children; }; |
