#include "EightPuzzle.h"

using namespace std;

EightPuzzle::EightPuzzle(int root [9], int goal [9]) {
	this->root = new Node(root);
	this->goal = new Node(goal);
};

bool EightPuzzle::solve(string search_type) {
	totalVisited = 0;
	vector<string> discovered;
	vector<Node*> toVisit;
	toVisit.push_back(root);

	while (toVisit.size() != 0) {
		Node* n;
		if (search_type == "dfs") {
			n = toVisit.back();
			toVisit.pop_back();
		}
		else if ("bfs") {
			n = toVisit.at(0);
			toVisit.erase(toVisit.begin());
		}

		totalVisited += 1;
		totalDiscovered = totalVisited + toVisit.size();


		if (n->getValueString().compare(goal->getValueString()) != 0) {
			cout << n->getLevel() << " " << totalVisited << endl;
			vector<Node*> children = n->getChildren();
			for (unsigned int i=0; i<children.size(); i++) {
				Node* child = children.at(i);
				if (!contains(discovered, child->getValueString())) {
					toVisit.push_back(child);
					discovered.push_back(child->getValueString());
				}
			}
		}
		else {
			cout << n->getValueString() << endl << goal->getValueString();
			path.push_back(n);
			while (n->getParent() != NULL) {
				n = n->getParent();
				path.push_back(n);
			}
			return true; // Solution Found!
		}
	}
	return false; // No Solution Exists.
};

bool EightPuzzle::contains(vector<string> container, string state) {
	for (unsigned int i=0; i<container.size(); i++) {
		if (container.at(i).compare(state) == 0) {
			return true;
			break;
		}
	}
	return false;
}

