You will write a program that will play a letter guessing game. - Write a progra
ID: 3790106 • Letter: Y
Question
You will write a program that will play a letter guessing game.
- Write a program that reads letters from a file called“lettersin.txt”.
- Your program will ask the user to enter the number of games they wish to play (1 to 4)
- Your program will open the lettersin.txt file read in one character at a time and repeat this for the number of games the user wants to play.
- For this assignment the test file will contain letters, all lowercase
- When the number of games has been played, the program will end
- A preprocessor directive must be used to define the maximum number of guesses as 5
- If the player has used up all of their guesses, a message that the game is over should be displayed along with the letter they were trying to guess.
- You must have at least 4 user defined functions as follows:
//this function provides instructions to the user on how to play the game
void GameRules( );
//this function runs one entire game. It for checks either 5 incorrect guesses or a correct guess.
//It returns a 0 if the game is over and the player did not guess the letter, otherwise it returns 1.
int SingleGame(char file_letter);
//this function prompts the player to make a guess and returns that guess
//this function is called from inside the SingleGame( ) function described above
char RetrieveGuess( );
//this function takes two arguments, the guess from the player
//and the solution letter from the file.
//It lets the user know if the guess comes alphabetically before or after the answer
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
//this function is called from inside the OneGame( ) function described above
int GuessedIt(char answer, char input_letter);
Additional Requirements:
- Use function prototypes.
- Write comments for each function that will appear in the file before each prototype and again before each function definition.
- Be sure to comment your code adequately.
- Be sure to indent properly.
- Use meaningful variable names
- Please do not use cin or cout.
Explanation / Answer
// C code
#include <stdio.h>
#include <string.h>
#define MAXIMUMGUESS 5
//this function provides instructions to the user on how to play the game
void GameRules()
{
printf(" Guessing Game. Guess the letter within 5 guesses ");
}
//this function prompts the player to make a guess and returns that guess
//this function is called from inside the SingleGame( ) function described above
char RetrieveGuess( )
{
char guess;
printf(" Guess a letter: ");
scanf(" %c", &guess);
return guess;
}
//this function takes two arguments, the guess from the player
//and the solution letter from the file.
//It lets the user know if the guess comes alphabetically before or after the answer
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
//this function is called from inside the OneGame( ) function described above
int GuessedIt(char answer, char input_letter)
{
if (answer == input_letter)
{
return 1;
}
else if (answer > input_letter)
{
printf("The correct letter comes before %c ",input_letter);
return 0;
}
else
{
printf("The correct letter comes after %c ",input_letter);
return 0;
}
}
//this function runs one entire game. It for checks either 5 incorrect guesses or a correct guess.
//It returns a 0 if the game is over and the player did not guess the letter, otherwise it returns 1.
int SingleGame(char file_letter)
{
int guess = 0;
char userGuess;
while (guess < MAXIMUMGUESS)
{
userGuess=RetrieveGuess();
int result = GuessedIt(file_letter, userGuess);
if (result == 1)
{
return 1;
}
else
guess++;
}
if (guess == 5)
{
return 0;
}
}
int main()
{
int totalGames, i = 1;
char answer;
GameRules();
FILE *fp;
fp = fopen("lettersin.txt", "r");
printf("Enter the number of games you wish to play (1 to 4): ");
scanf("%d", &totalGames);
while(totalGames--)
{
printf(" Game number #%d",i);
fscanf(fp, " %c", &answer);
int result = SingleGame(answer);
if (result == 1)
{
printf("You Win! ");
}
else if (result == 0)
{
printf("You Lose! ");
}
i++;
}
fclose(fp);
return 0;
}
/*
lettersin.txt
a
b
c
d
e
f
output:
Guessing Game. Guess the letter within 5 guesses
Enter the number of games you wish to play (1 to 4): 3
Game number #1
Guess a letter: r
The correct letter comes after r
Guess a letter: e
The correct letter comes after e
Guess a letter: a
You Win!
Game number #2
Guess a letter: f
The correct letter comes after f
Guess a letter: r
The correct letter comes after r
Guess a letter: g
The correct letter comes after g
Guess a letter: v
The correct letter comes after v
Guess a letter: d
The correct letter comes after d
You Lose!
Game number #3
Guess a letter: d
The correct letter comes after d
Guess a letter: c
You Win!
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.