#include "nikolaus.h"

using namespace Backtrack;

Nikolaus::Nikolaus()
{
}

Nikolaus::NodeListT Nikolaus::transitions(char from) const
{
	static QMap <char, NodeListT > valid_transitions;
	if (valid_transitions.isEmpty())
	{
		valid_transitions[0].append('1');
		valid_transitions[0].append('2');
		valid_transitions[0].append('3');
		valid_transitions[0].append('4');
		valid_transitions[0].append('5');

		valid_transitions['1'].append('2');
		valid_transitions['1'].append('3');
		valid_transitions['1'].append('4');

		valid_transitions['2'].append('1');
		valid_transitions['2'].append('3');
		valid_transitions['2'].append('4');

		valid_transitions['3'].append('1');
		valid_transitions['3'].append('2');
		valid_transitions['3'].append('4');
		valid_transitions['3'].append('5');

		valid_transitions['4'].append('1');
		valid_transitions['4'].append('2');
		valid_transitions['4'].append('3');
		valid_transitions['4'].append('5');

		valid_transitions['5'].append('3');
		valid_transitions['5'].append('4');


	}

	if (!valid_transitions.contains(from))
		throw;

	return valid_transitions[from];
}

bool Nikolaus::solved(void) const
{
	return Path.count() >= 9;
}

Nikolaus::ListT Nikolaus::variants(void) const
{
	Nikolaus::ListT vars;

	char cur_node = 0;

	if (Path.count() > 0)
		cur_node = Path.at(Path.count()-1).toLatin1();

	//if (Path.count() >= 5)
		//return vars;

	const NodeListT& targets = transitions(cur_node);
	for (NodeListT::const_iterator it = targets.begin(); it != targets.end(); ++it)
	{
		QString forward = QString(cur_node)+QString(*it);
		QString backward = QString(*it)+QString(cur_node);

		if ( (Path.contains(forward)) || (Path.contains(backward)) )
			continue;

		Nikolaus variant(*this);
		variant.Path.append(*it);
		vars.append(variant);
	}

	return vars;
}

QString Nikolaus::toString(void) const
{
	return Path;
	/*
	QStringList nodes;
	for (NodeListT::const_iterator it = Path.begin(); it != Path.end(); ++it)
	{
		nodes.append(QString::number(*it));
	}
	return nodes.join(", ");
	*/
}
