aboutsummaryrefslogtreecommitdiff
path: root/src/main/solver.cpp
blob: 7e7155a2bf70679e5ecccde8c5a234d4b39cd50c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// Created by thecharlesblake on 10/24/17.
//

#include <string>
#include <ostream>
#include <iostream>

#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) : 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<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();
        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;
}