#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <ctime>

using namespace std;

void draw_hangman( int n );
char getAlpha( void );

int main( void )
{
	const int MAX_WRONG_CNT = 6;
    
    //****************************************************//
    // Static Word List //
    // ***************************************************//
	
    vector<string> wordList;
	wordList.push_back( "apple" );
	wordList.push_back( "pear" );
	wordList.push_back( "peach" );
	wordList.push_back( "banana" );
	

    //****************************************************//
    // Dynamic Word List //
    // ***************************************************//
    
	/*
	vector<string> wordList;
	// get user input for word count
	int wordCount;
	cout << "Please enter the number of words: ";
	cin >> wordCount;
	// get user input for each word
	string aWord;
	for( int i=0; i < wordCount; i++ )
	{
		cout << "Enter " << i+1 << " th word: ";
		cin >> aWord;
		wordList.push_back( aWord );
	}
	*/
   
    //**************************************//
    // Display Word List //
    // *************************************//
    
	cout << "There are " << wordList.size() << " words stored" << endl;
	for( int i=0; i < wordList.size(); i++ ) {
		cout << "The " << i+1 << " th word is " << /*wordList.at(i)*/ wordList[i] << endl;
	}
    
    //**************************************//
    // Pick a word at random //
    // *************************************//

	srand( (unsigned int) time(0) );
	string theAnswer = wordList[ rand() % wordList.size() ];
	cout << "The randomly chosen word is " << theAnswer << endl;
        
/*
	// Get user input exercise //
	char c;
	cout << "Please enter a letter: ";
	cin >> c;
	cout << "You entered the letter: " << c << endl;

	for( int i=0; i <=6; i++ ) {
		cout << "Wrong count = " << i << endl;
		draw_hangman(i);
	}
*/
    //**************************************//
    // Initialize //
    //*************************************//

	cout << "#### Welcome to Hangman game, made by Minho Shin ####" << endl;
	draw_hangman(0);

	// initialize
	int wrongCount = 0;
	string used = "";

	//string guess = new String( '_', theAnswer.length() );


	/*
	string guess = "";
	for( int i=0; i < theAnswer.length(); i++ )
		guess += "_";
	*/
	string guess( theAnswer.length(), '_' );

	char c;
    
    //**************************************//
    // Game Loop //
    //*************************************//

	while( wrongCount < MAX_WRONG_CNT && theAnswer != guess ) {
		cout << MAX_WRONG_CNT - wrongCount << " incorrect guesses left." << endl;
		cout << "So far, you used the following letters: " << used << endl;
		cout << "Current guess string is: " << guess << endl;

		cout << "Please enter a new letter: ";
		while( used.find( c=getAlpha() ) != string::npos ) {
			cout << "You have already used this letter. Type again: ";
		}

		used += c; // used = used + c

		// hit
		if( theAnswer.find( c ) != string::npos ) {
			cout << "You got it right! " << endl;
			// update guess string
            for( int i=0; i<theAnswer.length(); i++ ) {
                if( theAnswer[i] == c )
                    guess[i] = c;
            }

		} 
		// miss
		else {
			cout << "Sorry, no such letter in the word" << endl;
			wrongCount++;
		}

		draw_hangman(wrongCount);


	}

/*C
    while( wrongCount < MAX_WRONG_CNT && strcmp( theAnswer, guess) ) {
        
        //==========================
        // Print variables
        //==========================
 
        printf("%d incorrect guesses left.\n", MAX_WRONG_CNT - wrongCount );
        printf("So far, you used the following letters: %s\n", used );
        printf("Current guess string is: %s\n", guess );
        
        //==========================
        // Get a new alphabet letter
        //==========================
        printf("Please enter a letter:" );
 
        while(strchr( used, c = getAlpha() ))
            printf("You have already used this letter. Type again:");
        
        //==========================
        // Append the letter to used
        //==========================
 
        used[ strlen(used) ] = c;
        
        //==========================
        // Check hit/miss
        //==========================

        // if hit
        if( strchr( theAnswer, c ) ) {
 
            printf("You correctly guessed a letter\n");
 
            // update guess string
            for( int i=0; i<strlen(theAnswer); i++ ) {
                if( theAnswer[i] == c )
                    guess[i] = c;
            }
            
        // if miss
        } else {
            printf("You guess a wrong letter\n");
            wrongCount++;
        }
        
        draw_hangman(wrongCount);
        
    }
*/
    
    
    //**************************************//
    // Finalize //
    //*************************************//

    if( wrongCount == MAX_WRONG_CNT )
		cout << "Sorry, you lost. The answer was " << theAnswer << endl;
	else
		cout << "Congratulations, you are genius!! " << endl;

    system("pause");
    
    return 0;
}

void draw_hangman( int n )
{
	string top = "     __ \n    |  | \n";
	string pole = "   |   \n";
	string pole_head =  "    |  O \n";
	string pole_body =  "    |  @ \n";
	string pole_body_rarm = "    |  /@ \n";
	string pole_body_arms = "    |  /@) \n";
	string pole_rleg = "    |  /  \n";
	string pole_legs = "    |  / ) \n";
	string pole_bottom = "   __|__ \n";

    cout << "     __   " << endl;

    printf("    |  |  \n");
    if( n == 0 ) {
        printf("    |     \n");
        printf("    |     \n");
        printf("    |     \n");
    }
    else if( n == 1 ) {
        // Draw a hangman
        printf("    |  O  \n");
        printf("    |     \n");
        printf("    |     \n");
    }
    else if( n == 2 ) {
        // Draw a hangman
        printf("    |  O  \n");
        printf("    |  @  \n");
        printf("    |     \n");
    }
    else if( n == 3 ) {
        // Draw a hangman
        printf("    |  O  \n");
        printf("    | /@  \n");
        printf("    |     \n");
    }
    else if( n == 4 ) {
        // Draw a hangman
        printf("    |  O  \n");
        printf("    | /@) \n");
        printf("    |     \n");
    }
    else if( n == 5 ) {
        // Draw a hangman
        printf("    |  O  \n");
        printf("    | /@) \n");
        printf("    | /   \n");
    }
    else if( n == 6 ) {
        // Draw a hangman
        printf("    |  O  \n");
        printf("    | /@) \n");
        printf("    | / ) \n");
    }
    printf("    |     \n");
    printf("  __|__   \n");
}

char getAlpha( void )
{
    char c;
    
    do {
        //cout << "Please enter a letter: ";
        cin >> c;
    }while( ! isalpha(c) );
    
    return c;
}


