#include "Node.h"

Node::~Node() {
	// TODO Auto-generated destructor stub
}

Node::Node(int state [9], Node* parent, int level, string direction, int indexOfZero) {
	for (int i=0; i<9; i++) {
		value[i] = state[i];
	}
	this->parent = parent;
	this->level = level;
	this->direction = direction;
	this->indexOfZero = indexOfZero;
}

Node::Node(int state [9]) {
	for (int i=0; i<9; i++) {
		value[i] = state[i];
		if (value[i] == 0)
			indexOfZero = i;
	}
	this->parent = NULL;
	this->level = 0;
	this->direction = "";
}

vector<Node*> Node::getChildren() {

	int row = indexOfZero / 3;
	int col = indexOfZero % 3;

	vector<int> zeroIndices;
	vector<string> directions;

	if (row != 0) { // NORTH
		zeroIndices.push_back(3*(row - 1) + col);
		directions.push_back("N");
	}
	if (row != 2) { // SOUTH
		zeroIndices.push_back(3*(row + 1) + col);
		directions.push_back("S");
	}
	if (col != 0) { // WEST
		zeroIndices.push_back(3*row + (col - 1));
		directions.push_back("W");
	}
	if (col != 2) { // EAST
		zeroIndices.push_back(3*row + (col + 1));
		directions.push_back("E");
	}

	vector<Node*> children;
	for (unsigned int i=0; i<zeroIndices.size(); i++) {
		int newState [9];
		memcpy(newState, value, sizeof(value));
		int index = zeroIndices.at(i);
		newState[indexOfZero] = value[index];
		newState[index] = 0;

		Node *n = new Node(newState, this, this->level+1, directions.at(i), index);
		children.push_back(n);
	}
	return children;
}

Node* Node::getParent(){
	return parent;
}

int* Node::getValue() {
	return &value[0];
}

string Node::getValueString() {
	stringstream s;
	for (int i=0; i<9; i++) {
		s << value[i];
	}
	return s.str();
}

void Node::printValue() {
	for (int i=0; i<9; i++) {
		cout << value[i];
	}
	cout << endl;
}

int Node::getLevel() {
	return level;
}

string Node::getDirection() {
	return direction;
}

