/*---------------------------------------------------------------------------*\ * http//www.incubus.c.uk * hangman.c \*---------------------------------------------------------------------------*/ #include #include #include #include #include /*-- DOS Header --*/ #define MAXLEN 20 #define MAX 10 #define YES 1 #define NO 0 #define STARS "********************" int main (void) { char word [MAXLEN + 1], solution [MAXLEN + 1], ch, *w_ptr, *s_ptr; int num_guess = 0, len = 0, found = 0; clrscr(); printf("\n**** Welcome to @incubus Hangman ****"); do { num_guess = 0; printf("\nEnter the word for the player to guess > "); fgets(word, MAXLEN, stdin); clrscr(); /*-- Clear screen so word is hidden --*/ for( w_ptr = word ; *w_ptr ; w_ptr++) *w_ptr = toupper(*w_ptr); len = strlen(word); strncpy(solution, STARS, len); solution[len] = '\0'; while( num_guess < MAX ) { printf("\n\nWord so far is %s, Guesses so far %d" ,solution ,num_guess++); printf("\nEnter a letter > "); ch = toupper(getche()); s_ptr = solution; w_ptr = word; found = NO; while ( *w_ptr ) { if ( ch == *w_ptr ) { *s_ptr = *w_ptr; found = YES; } w_ptr++; s_ptr++; } if ( strcmp(solution , word) == 0 ) break; if ( found ) printf("\nThe letter \"%c\" was in the answer",ch); } if ( num_guess < MAX ) printf("\nYou got it! The word was %s.\nIt took you %d tries \n",word , num_guess); else printf("\nYou had %d tries, the word was %s\n", MAX ,word); printf("\nPlay again [Y/N] - "); }while( (toupper(getche())) == 'Y'); printf("\nGoodbye\n"); return EXIT_SUCCESS; }