You will code a C program that is a letter guessing game. The user can pick betw
ID: 3589115 • Letter: Y
Question
You will code a C program that is a letter guessing game. The user can pick between 1-8 games to play. The program will read the letter from an input file for each game. The user will try to guess the letter and has 1-8 tries to guess it correctly. After which, the game ends and the next one begins. Instructions: • Write a program that reads letters from a file called “letterList.txt”. • Your program will ask the user to enter the number of games they wish to play (1 to 8) • Your program will open the letterList.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 at least 8 letters, uppercase OR lowercase • In your program you will change each letter (solution and guess) to lowercase – o Use the function tolower in #include • When the number of games has been played, the program will end • A sample of an input file that you can use to test your program is included with the assignment. • 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: o No modifications may be made the to functions
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
#define MAXGUESSES 5
//this function provides instructions to the user on how to play the game
void GameRules();
//this function runs one game.
//input: character from the file, void return type
//all other functions to Play one round of a game
//are called from within the GuessTheLetter function void GuessTheLetter(char);
//this function prompts the player to make a guess and returns that guess
//this function is called from inside the GuessTheLetter( ) function described above char GetTheGuess();
//this function takes two arguments, the guess from the player //and the solution letter from the file.
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
//This function also lets the user know if the guess comes alphabetically before or after the answer int CompareLetters(char, char);
Explanation / Answer
//program
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
#define MAXGUESSES 8
//this function provides instructions to the user on how to play the game
void GameRules();
//this function runs one game.
//input: character from the file, void return type
void rungame(char );
//all other functions to Play one round of a game
//are called from within the GuessTheLetter function void GuessTheLetter(char);
//this function prompts the player to make a guess and returns that guess
char GetTheGuess();
//this function is called from inside the GuessTheLetter( ) function described above char GetTheGuess();
int GuessTheLetter(char);
//this function takes two arguments, the guess from the player //and the solution letter from the file.
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
//This function also lets the user know if the guess comes alphabetically before or after the answer int CompareLetters(char, char);
int CompareLetters(char guess, char charFromFile);
int main()
{
int i = 0, n, j, charCount;
//dclare file pointer fp
FILE *fp;
//char array to hold characters read from file
char str[100];
char ch;
//open input file to rad letters
fp = fopen("letterList.txt","r");
//check if file is open
if (!fp)
{
printf("Not able to open input file letterList.txt ");
return -1;
}
//read chars from file
while (fscanf(fp, "%c", &ch) != EOF)
{
str[i++] = ch;
}
str[i] = '';
charCount = i;
GameRules();
printf("Please pick number of games user wish to play between 1-%d: ", MAXGUESSES);
scanf("%d", &n);
//run the game till the number times user has specified
for (i = 0; i < n; i++)
{
//run gmae for each char read from file
rungame(tolower(str[i]));
}
printf("Game over,bye ");
}
void GameRules()
{
printf("####################Instruction for game#################################### ");
printf("The user can pick between 1-8 games to play. The program will read the letter from an input file for each game. The user will try to guess the letter and has 1-8 tries to guess it correctly. After which, the game ends and the next one begins ");
printf("####################End of instruction for game############################## ");
}
void rungame(char ch)
{
int i,tmp;
if (GuessTheLetter(ch))
{
printf("User has guessed letter correctly ");
}
else
{
printf("User has not guessed letter correctly ");
}
}
char GetTheGuess()
{
char ch;
fflush(stdin);
printf("Enter your letter guess: ");
scanf("%c", &ch);
ch = tolower(ch);
return ch;
}
int GuessTheLetter(char charFromFile)
{
//call the GetTheGuess function to get guess from user
char ch = GetTheGuess();
//check if guessed char and char read from file are same
if (CompareLetters(ch, charFromFile))
{
return 1;
}
else
return 0;
}
int CompareLetters(char guess, char charFromFile)
{
if (guess == charFromFile)
return 1;
else
return 0;
}
-------------------------------------------------------------------------------------
//output
####################Instruction for game####################################
The user can pick between 1-8 games to play.
The program will read the letter from an input file for each game.
The user will try to guess the letter and has 1-8 tries to guess it correctly.
After which, the game ends and the next one begins
####################End of instruction for game##############################
Please pick number of games user wish to play between 1-8: 5
Enter your letter guess: c
User has not guessed letter correctly
Enter your letter guess: b
User has guessed letter correctly
Enter your letter guess: f
User has not guessed letter correctly
Enter your letter guess: a
User has not guessed letter correctly
Enter your letter guess: e
User has guessed letter correctly
Game over,bye
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.