#include <iostream>              // for instance cout/cerr
#include <fstream>               // for class ifstream
#include <string>                // for class string
using namespace std;
#include "ladder.h"


const string DEFAULT_WORDS_DB = "knuth.dat";


bool IsValidInput (const string & from, const string & to)
{
    return (from.length() == Ladder::WORD_SIZE && 
	    to.length() == Ladder::WORD_SIZE);
}


int main (int argc, char * argv[])
{
    string wordsFile = DEFAULT_WORDS_DB;
    if (argc > 1)
    {
        wordsFile = argv[1];
    }

    ifstream input(wordsFile.c_str());
    if (input.fail())
    {
        cerr << "ERROR: could not find words file " << wordsFile << endl;
        return 1;
    }

    Ladder ladder(input);
    string from, to; 
    do
    {
        cout << "Please enter two 5-letter words: ";
        cin  >> from >> to;

        if (IsValidInput(from, to))
	{
	    if (ladder.Find(from, to))
	    {
	        ladder.Print(cout);
	    }
	    else
	    {
	        cout << "No path found from " << from << " to " << to << endl;
	    }
	}
    }
    while (IsValidInput(from, to));

    return EXIT_SUCCESS;
}
