The game is to play hangman, if guessed right or wrong it will ask the user if they want to play another game (w/ a different word).
My code repeats the game with the user enters anything (even q) but doesnt quit when 'q' is entered. I've been trying to do a while(q != 0) at the beginning but I keep doing it wrong and I don't know how to get the next word in the file to be used in the next game.
Please help me figure out what I'm doing wrong, this is my code:
#include <stdio.h>
#include <string.h>
/* contant declarations */
#define MAX 6
/* function prototypes */
void clrscrn( void );
void draw_hangman( int incorrect_tries );
void DisplayAstrisks(char word[], char visible_word[]);
void Instructions();
int QuitorNot(char quit);
void DidyouWin(char visible_word[], char word[], char incorrect_letters[], char guess);
//void Compare(char visible_word[MAX], char incorrect_letters[MAX]);
//int FindLtr(char word[], char guess, char visible_word[], int repeat_flag, int correct_guesses, int correct_flag, char incorrect_letters[], int tries);
int main()
{
/* variable declarations */
char len = 0; /* length of word char array */
int count = 0, /* for word char array */
tries = 0, /* total tries user has used */
//num_vis_chars = 0, /* # of visible characters */
correct_guesses = 0, /* # of correct guesses */
correct_flag = 0, /* was guess correct? */
repeat_flag = 0; /* was guess a repeat? */
FILE* infile = NULL;
char guess;
int quit=0;
/* array declarations */
char word[255] = " ";
char incorrect_letters[255] = " ";
char visible_word[len]; /* displays correct guesses */
/* getting word from file */
infile = fopen("words.txt", "r");
fscanf(infile, " %s", word);
len = strlen( word );
DisplayAstrisks(word, visible_word);
Instructions();
draw_hangman( tries );
while( tries < MAX )
{
if(quit == 0)
{
printf( "WORD: %s\n", visible_word );
printf( "Incorrect Guesses: %s\n", incorrect_letters );
printf( "\nGuess a letter: " );
scanf( " %c", &guess );
//Compare(visible_word, incorrect_letters);
/*match guess against previous guesses*/
for( count = 0; count < len; count++ )
if( guess == visible_word[count] || guess == incorrect_letters[count] )
{
repeat_flag = 1;
correct_flag = 1;
break;
}
if( repeat_flag == 0 )
//DidyouWin(visible_word, word, incorrect_letters, guess);
/* check for matches in string*/
for( count = 0; count < len; count++ )
{
if( guess == word[count] )
{
visible_word[count] = guess;
correct_guesses++;
if( correct_guesses == len )
{
printf( " --------------YOU WIN!--------------" );
printf( "\n ANSWER: %s\n", visible_word );
QuitorNot(0);
}
correct_flag = 1;
}
}
if( correct_flag == 0 )
{
incorrect_letters[tries] = guess;
tries++;
}
repeat_flag = 0;
correct_flag = 0;
/* reset flags */
clrscrn();
draw_hangman( tries );
}
printf( " --------------YOU LOSE!--------------\n" );
printf( " THE ANSWER: %s\n", word );
QuitorNot(1);
}
}
/**************************************************************/
/* clrscrn() clears the screen */
void clrscrn( void )
{
int x = 0;
for( x = 0; x < 3; x++ )
printf( "\n" );
}
/**************************************************************/
/* draw_hangman() displays the hangman */
void draw_hangman( int incorrect_tries )
{
/*
___
| &
| O
| /|\
| / \
|
---
*/
switch( incorrect_tries )
{
default:
{
puts( "\n ___" );
puts( " | &" );
puts( " |" );
puts( " |" );
puts( " |" );
puts( " |" );
puts( "---" );
break;
}
case 1:
{
puts( "\n ___" );
puts( " | &" );
puts( " | O" );
puts( " |" );
puts( " |" );
puts( " |" );
puts( "---" );
break;
}
case 2:
{
puts( "\n ___" );
puts( " | &" );
puts( " | O" );
puts( " | /" );
puts( " |" );
puts( " |" );
puts( "---" );
break;
}
case 3:
{
puts( "\n ___" );
puts( " | &" );
puts( " | O" );
puts( " | /|" );
puts( " |" );
puts( " |" );
puts( "---" );
break;
}
case 4:
{
puts( "\n ___" );
puts( " | &" );
puts( " | O" );
puts( " | /|\\" );
puts( " |" );
puts( " |" );
puts( "---" );
break;
}
case 5:
{
puts( "\n ___" );
puts( " | &" );
puts( " | O" );
puts( " | /|\\" );
puts( " | /" );
puts( " |" );
puts( "---" );
break;
}
case 6:
{
puts( "\n ___" );
puts( " | &" );
puts( " | O" );
puts( " | /|\\" );
puts( " | / \\" );
puts( " |" );
puts( "---" );
break;
}
}
}
/**************************************************************/
void DisplayAstrisks(char word[], char visible_word[])
{
int count=0;
char len=strlen(word);
/* initialize visble_word */
for( count = 0; count < len; count++ )
visible_word[count] = '*';
visible_word[len] = '\0';
}
/**************************************************************/
void Instructions()
{
printf("--------------------WELCOME TO--------------------\n");
printf("H H GGGGGG \n");
printf("H H AA N N G MM MM AA N N \n");
printf("HHHHH A A NN N G GGG M M M M A A NN N \n");
printf("H H AAAAAA N N N G G M M M AAAAAA N N N \n");
printf("H H A A N NN GGGGGG M M A A N NN \n\n\n");
printf(" -The computer will pick a secret word.\n");
printf(" -The game is to guess this word with \n");
printf(" guessing one letter at a time.\n");
printf(" -Guess wrong six times and game over.\n\n\n");
printf(" --------------START--------------\n\n\n");
return;
}
/**************************************************************/
int FindLtr(char word[], char guess, char visible_word[], int repeat_flag, int correct_guesses, int correct_flag, char incorrect_letters[], int tries)
{
int count;
char len = strlen(visible_word);
if(repeat_flag == 0 )
/* check for matches in string */
for( count = 0; count < len; count++ )
{
if( guess == word[count] )
{
visible_word[count] = guess;
correct_guesses++;
if( correct_guesses == len )
{
puts( "\n\n --------------YOU WIN!--------------" );
printf( "WORD: %s\n", visible_word );
return 1;
//exit( 0 );
}
correct_flag = 1;
}
}
if( correct_flag == 0 )
{
incorrect_letters[tries] = guess;
tries++;
return -1;
}
return 0;
}
int QuitorNot(char quit)
{
printf(" Try again?\n Enter Q to QUIT, anything else to play\n");
scanf(" %c", &quit);
while((quit == 'q') || (quit == 'Q'))
{
return 1;
}
if((quit != 'q') && (quit != 'Q'))
{
return 0;
}
return 0;
}
void DidyouWin(char visible_word[], char word[], char incorrect_letters[], char guess)
{
int repeat_flag = 0;
int correct_flag = 0;
int count;
int correct_guesses = 0;
int tries = 0;
char len = strlen(visible_word);
if( repeat_flag == 0 )
for(count = 0; count < len; count++)
{
if( guess == word[count] )
{
visible_word[count] = guess;
correct_guesses++;
if( correct_guesses == len )
{
printf( " --------------YOU WIN!--------------" );
printf( "\n ANSWER: %s\n", visible_word );
QuitorNot(0);
}
correct_flag = 1;
}
}
if( correct_flag == 0 )
{
incorrect_letters[tries] = guess;
tries++;
}
repeat_flag = 0;
correct_flag = 0;
}