/*C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
*/

#define MAX_WRONG_CNT 6

void draw_hangman( int n );
char getAlphaCapital( void );

int main( void )
{
    
    //****************************************************//
    // Static Word List //
    // ***************************************************//

/*C
    char *wordList[] = { "apple", "pear", "peach", "banana" };
    int wordCount = sizeof(wordList) / sizeof(wordList[0]);
*/
    
    //****************************************************//
    // Dynamic Word List //
    // ***************************************************//
    
/*C
    // get word count
    
    int wordCount;
    printf("Please enter the word count: ");
    scanf("%d", &wordCount);
    
    // make memory for word list
    // 		how to make an array of type X with size N:
    // 			X *p = malloc( N * sizeof(X) )
    char ** wordList = (char **) malloc(wordCount * sizeof(char*));
    
    
    // get words from the user
    for( int i=0; i < wordCount; i++) {
        
        printf("Please enter %d th word: ", i+1);
        
        // allocate a 20-byte memory block for each word
        wordList[i] = (char *)malloc(20);
        
        scanf("%s", wordList[i]);
        
    }
*/
    
    //**************************************//
    // Display Word List //
    // *************************************//
    
/*C
    for(int i=0; i<wordCount; i++) {
        printf("The word is %s\n", wordList[i]);
    }
*/
    
    //**************************************//
    // Pick a word at random //
    // *************************************//
    
    /*
    // get current time
    time_t t;
    time(&t);
    
    // set random seed
    srand( t );
    
    char *theAnswer = wordList[ rand() % wordCount ];
    printf( "DEBUG: The chosen word is %s\n", theAnswer );
    */
    
    
    //**************************************//
    // Initialize //
    //*************************************//

/*C
	printf( "#### Welcome to Hangman game, made by Minho Shin ####\n");
 
	draw_hangman(0);
 
	// initialization
	int wrongCount = 0;
	char used[100] = { 0 };
	char guess[100] = { 0 };
	char c; // user input
 
	// initialize guess
	memset( guess, '_', strlen(theAnswer) );

*/
    
    //**************************************//
    // Game Loop //
    //*************************************//

/*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 //
    //*************************************//

/*C
    if( wrongCount == MAX_WRONG_CNT )
        printf("Sorry, you lost. The answerr is %s.", theAnswer);
    else
        printf("Congratulations, you won the game!!");
*/
    
    system("read");
    
    return 0;
}

void draw_hangman( int n )
{
    printf("     __   \n");
    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 getAlphaCapital( void )
{
    char c;
    fseek(stdin,0,SEEK_END);
    
    do {
        printf("Please enter a letter: ");
        c = getchar();
    }while( ! isalpha(c) );
    
    //return toupper(c);
    return c;
}

