You are on page 1of 4

//Muryum Naim, 02/20/17, Letter Guessing Interactive Game

#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 LetterGuessGameRules(void);

//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 PlayOneRound function
void PlayOneRound(char);

//this function prompts the player to make a guess and returns that
guess
//this function is called from inside the PlayOneRound ( ) function
described above
char GetGuess(void);

//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 solution
int CompareCharacters(char, char);

//this function provides instructions to the user on how to play the


game
void LetterGuessGameRules(void);

//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 PlayOneRound function
int main()
{
//declare additional variables
//declare FILE pointer
FILE *inPtr;
int numGames, i = 0;

char letter;
//letter from file

//display game rules


LetterGuessGameRules();
//Ask and get number of games to play
printf("How many games would you like to play?\n");
scanf("%d", &numGames);

//connect to the file HINT: use fopen


inPtr = fopen("letters.txt", "r");

//this for loop will allow the player to play more than one
game
//without recompiling
for (i = 0; i < numGames; i++)
{
printf("Let's play game %d", i + 1);
//get a solution letter from file - use fscanf
fscanf(inPtr, " %c", &letter);
//change the solution to uppercase
letter = toupper(letter);
//call the PlayOneRound function and pass it the
solution
PlayOneRound(letter);

//close file pointer


return 0;
}

//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 PlayOneRound function
void PlayOneRound(char letter)
{
int win = 0;
int numGuess = 0;
char guess;
//declare additional variables
int correct = 0;
while (numGuess < MAXGUESSES && win == 0)
{
//get a guess from the user by calling the GetGuess
function
guess = GetGuess();
//change the guess to uppercase
guess = toupper(guess);
printf("Your guess was %c\n", guess);
//win = call the function to compare the guess with
the solution
correct = CompareCharacters(guess, letter);
if (correct == 1)
break;
numGuess++;//count the number of guesses so far
}
if(correct == 0)
printf("You have ran out of guesses\n");

}
//use conditions to let the user know if they won or lost the round of
the game

//with the comments


// this function provides instructions to the user on how to play the
game
void LetterGuessGameRules(void)
{
printf("Welcome to the Letter Guessing Game\n");
printf("To get started, enter the number of games you want to
play\n");
printf("You have 5 chances in each round to guess the letter
\n");
return;
}
//this function prompts the player to make a guess and returns that
guess
//this function is called from inside the PlayOneRound() function
char GetGuess()
{
char guess;
printf("\nEnter a guess");
scanf(" %c", &guess);
return guess;

//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 solution
int CompareCharacters(char guess, char letter)
{

if (guess == letter)
{
printf("Good Job!\n");
return 1;
}
else
if (guess < letter)
{
printf("The letter you are looking for comes
after %c\n", guess);
return 0;
}
else
if (guess > letter)
{
printf("The letter you are looking
for comes before %c\n", guess);
return 0;
}
}

You might also like