-
Hangman Game Troubles
So I have designed this hangman game and completed the program. The program correctly prompts the user if they want to play and correctly displays a hidden word, but after that it does nothing more than keep asking the user for their guess. Any help would be greatly appreciated thank you!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_SIZE 30
#define MAXWORDS 1000
#define MAX_ATTEMPTS 5
void initialize_words(char *word, int word_length);
int load_game(char *game_word, char puzzle[][MAX_SIZE]);
void print_game_word(char guessed_word[], int word_length);
char get_user_guess();
int perform_guess(char game_word[], char *guessed_word, int word_length, char guess);
int game_solved(char game_word[], char guessed_word[]);
void read_words(char all_words[][MAX_SIZE], FILE *fin, int n);
int main(void)
{
char puzzle[MAXWORDS][MAX_SIZE];
char filename[MAX_SIZE];
char game_word[MAX_SIZE];
FILE *fin;
int word_length;
char guessed_word[MAX_SIZE];
char user_choice[6];
int chosen_letters[26];
char guess;
int correct_guess;
int i;
int num_words;
int user_attempts = 0;
srand(time(0));
// Ask the user for the input file.
printf("What file stores the puzzle words?\n");
scanf("%s", filename);
fin = fopen(filename, "r");
printf("\n");
// Read in all of the words into the array.
fscanf(fin, "%d", &num_words);
read_words(puzzle, fin, num_words);
fclose(fin);
do
{
printf("Would you like to play hangman (yes/no)?\n"); //Asks the user if they would like to play a game of
scanf("%s", user_choice); //hangman
if (!(strcmp(user_choice, "no") == 0 || strcmp(user_choice, "n") == 0 )) //If the user decides to play, their puzzle is loaded
{ //and printed. The user is than asked for their guess.
printf("Here is your puzzle:\n");
word_length = load_game(game_word, puzzle);
initialize_words(guessed_word, MAX_SIZE);
print_game_word(guessed_word, word_length);
printf("You currently have %d incorrect guesses\n", user_attempts); //Informs the user how man incorrect guesses they
} //have
else
{
printf("Thanks for playing!\n"); //Exits the loop if user does not want to play
system("PAUSE");
return 0;
}
for (i = 0; i < 26; i++) //For loop initializing letters that the user has chosen to zero before each new
{ //game is played
chosen_letters[i] = 0;
}
while (user_attempts != MAX_ATTEMPTS)
{
guess = get_user_guess();
correct_guess = perform_guess(game_word, guessed_word, guess, word_length);
if (guess == 1)
{
printf("Congratulations, you guessed a letter in the puzzle!\n");
}
else if (guess == 0)
{
printf("Sorry, that letter is NOT in the puzzle.\n");
user_attempts++;
}
else if (chosen_letters[i] > 2)
{
printf("Sorry, you guessed that letter already. Now it counts as a miss.\n");
user_attempts++;
chosen_letters[(int)(guess - 'A')]++;
}
else if (user_attempts == MAX_ATTEMPTS)
{
printf("Sorry, you have made 5 incorrect guesses, you lose.\n The correct word was %s.\n", game_word);
}
else if (game_solved(game_word, guessed_word))
{
printf("Congratulation! You got the correct word, %s.\n", game_word);
break;
}
}
} while (strcmp(user_choice, "no") != 0);
system("PAUSE");
return 0;
}
//Initializes all words to zero, indicating no letter has been guessed
void initialize_words(char *word, int word_length)
{
int i;
for (i = 0; i < word_length; i++)
{
word[i] = 0;
}
}
//Loads a random word in text file to be used for playing the game
int load_game(char *game_word, char puzzle[][MAX_SIZE])
{
int num_words;
int underscore_length = 0;
int puzzlenum;
puzzlenum = rand()%num_words; //Picks a random word in the file
strcpy(game_word, puzzle[puzzlenum]); //Copies randomly chosen word in file to "game_word"
underscore_length = strlen(game_word);
return underscore_length; //Returns length of "game_word"
}
//Prints out the word which was chosen from the file
void print_game_word(char guessed_word[], int word_length)
{
int i;
for (i = 0; i < word_length; i++)
{
if (guessed_word[i]==0)
{
printf("_ ");
}
else
{
printf("%c ", guessed_word[i]);
}
}
printf("\n");
}
//Prompts menu for user playing the game to guess letter, converts the guess to a Capital letter as specified,
//and returns the user's guess
char get_user_guess()
{
char guess;
printf("Please enter your guess\n");
scanf("%c", &guess);
guess = toupper(guess);
return(guess);
}
//Performs the user's guess. If the the guess is a letter in the word, the value is swapped with any "_" letters
int perform_guess(char game_word[], char *guessed_word, int word_length, char guess)
{
int i;
int guessed_right = 0;
int chosen_letters[26];
for (i = 0; i < word_length; i++)
{
if (guessed_word[i] != 0)
{
if (game_word[i] == guess)
{
guessed_word[i] = guess;
chosen_letters[guess - 'A']++;
return 1;
}
}
}
}
//Checks to see if word user has guessed is equal to underscore_word
int game_solved(char game_word[], char guessed_word[])
{
if (strcmp(guessed_word, game_word) == 0)
{
return 1;
}
else
{
return 0;
}
}
// Pre-conditions: allWords must be of size n, at least, fin must
// point to a file with n words, with only uppercase letters
// ready to read from the first word.
// Post-conditions: The n words stored in the file pointed to by fin will
// we stored in the array allWords.
void read_words(char all_words[][MAX_SIZE], FILE *fin, int n)
{
int i;
for (i=0; i<n; i++)
{
fscanf(fin, "%s", all_words[i]);
}
}
-
Presumably you want to compare correct_guess with 1 and 0, rather than the character variable guess.
-
I tried that and still nothing changed
-
You do need a return 0 at the bottom of perform_guess.
-
I appreciate everyone's help. If I add a return 0 at the bottom of perform_guess, the program tells me that every letter I type in is incorrect.
-
Be careful about your reading in -- I would bet that half your guesses are \n characters rather than letters. Otherwise, if perform_guess is always returning 0, then that probably means your word is not in uppercase as expected.
-
tabstop, I am not quite sure I follow what you're saying. So are you saying that that my toupper function is incorrect? Thanks.
-
I doubt it's your toupper function -- you didn't write it, it's a system function. What I mean is that I expect a game to go like this:
Guess 1: T
Guess 2: enter-key
Guess 3: R
Guess 4: enter-key
Guess 5: S
Guess 6: enter-key
and so on.
-
Oh, so you are saying that the program will read an enter-key as an actual guess, correct? Which is what you meant by \n characters. How could I go about solving such a problem?
-
Right after every line which includes scanf(), add another line that will pull off the remaining /n, from the keyboard buffer.
Do you have a function in C that will get a char from the input stream?