#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 )
{
    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;
}

int main( void )
{
    
#if 1
    
    //****************************************************//
    // Example for Dynamic Memory Allocation of Word List //
    // ***************************************************//
    
    
    // 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 stored words
    for(int i=0; i<wordCount; i++) {
        printf("The word is %s\n", wordList[i]);
    }
    
    
#endif
    
    //**************************************//
    // Example for Random Number Generation //
    // *************************************//
    
    // display maximum value of rand()
    //printf("RAND_MAX: %d\n", RAND_MAX );
    
    // get current time
    time_t t;
    time(&t);
    //printf("Current Unix Time/Epoch Time: %d\n", t);
    
    // set random seed
    srand( t );
    
    // print three random numbers
    //printf("a randome number: %d\n", rand() );
    //printf("a randome number: %d\n", rand() );
    //printf("a randome number: %d\n", rand() );
    
    //printf("A randomly chosen word is %s\n", wordList[ rand() % wordCount ] );
    
    char *theAnswer = wordList[ rand() % wordCount ];
    printf( "DEBUG: The chosen word is %s\n", theAnswer );
    
    draw_hangman(0);
    
    printf( "#### Welcome to Hangman game, made by Minho Shin ####\n");
    
    int wrongCount = 0;
    char used[100] = {'\0'}; // initialize all 0
    
    char guess[100] =  { 0 };
    memset( guess, '_', strlen(theAnswer) );
    
    do {
        printf("\n\nYou have %d incorrect guesses left\n", MAX_WRONG_CNT - wrongCount);
        printf("You have used the following letters so far: %s\n", used);
        printf("Current guess: %s\n", guess);
        char c;
        while ( strchr(used, c = getAlphaCapital()) ) {
            printf("You already guessed this letter before\n");
        }

        used[ strlen(used) ] = c;
        
        if( strchr( theAnswer, c ) ) {
            printf("You correctly guessed a letter\n");
            for( int i=0; i < strlen(theAnswer); i++ ) {
                if( theAnswer[i] == c )
                    guess[i] = c;
            }
        } else {
            printf("Your guess is not in the word\n");
            wrongCount++;
            draw_hangman(wrongCount);
        }
    } while( wrongCount < MAX_WRONG_CNT && strcmp( theAnswer, guess) );
    
    if( wrongCount == MAX_WRONG_CNT ) {
        printf("\nYOU LOST: the answer is %s\n", theAnswer);
    }
    else
        printf("\nYOU WIN THE GAME!\n");
    
    system("pause");
    
    return 0;
}