#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");
}

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");

	printf( "The word is : ");
	// show empty word: _____ 
	for( int i=0; i < strlen(theAnswer); i++ ) {
		printf("_");
	}
	printf("\n");

	// get a letter from the user
	char c;
	do {
		c = getchar();
	}while( ! isalpha(c) );


	// check if the letter is alphabet

	// check if the letter is in the answer word
	if( strchr( theAnswer, c ) ) 
		printf("You got a letter right!\n");
	else
		printf("Sorry, you got it wrong\n");



	//draw_hangman(1);
	//draw_hangman(2);
	//draw_hangman(3);
	//draw_hangman(4);
	//draw_hangman(5);
	//draw_hangman(6);


	system("pause");

	return 0;
}
