Files and Arrays Project I Build a Guessing Game Program 1

Files and Arrays

Project I – Build a Guessing Game Program

1. Write a program to implement the Number Guessing Game. The main method should loop as long as the player wants to play the game again.

2. Write a function called PlayGame(). In this function write the code to play the game. In the game the computer chooses a random number between 1 and 100, and the player tries to guess the number in as few attempts as possible. Each time the player enters a guess, the computer tells the player whether the guess is too high, too low, or right. Once the player guesses the number, that game is over. Keep track of how many guesses it takes the player to get the number correct.

3. The main function should use an array to keep track of how many attempts it takes the player to guess the random number each time they play the game. When the player decides to exit the game, print out the results of each game and the average number of guesses it took this player.

4. As the game ends, write the average number of guesses it took this player to guess the random numbers to a file. The next time they play, read in the avg from the last time they played, and if the current game avg is better than the one in the file print “Congratulations you have improved your score since the last time you played”. If not print “Your score was better last time you played than today”.

Write a program to implement the number Guessing game. The main method should loop as long as the player wants to play the game again. Write a function called Play Game In this function write the code to play the game. In the game the

Solution

Here is the code for you:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
int PlayGame(int num)
{
int guess, count = 0;
while(true)
{
cout<<\"Guess the number between 1 and 100: \";
cin>>guess;
count++;
if(guess == num)
{
cout<<\"Yay. You guessed it right. And you took \"<<count<<\" attempts to guess it right.\"<<endl;
return count;
}
else if(guess < num)
cout<<\"Your guess is less than the actual number.\"<<endl;
else
cout<<\"Your guess is greater than the actual number.\"<<endl;   
}
//return count;
}
int main()
{
srand(time(NULL));
int randNumber;
int guesses[20];
int count = 0;
char again;
double previousGuesses;
while(true)
{
randNumber = rand()%100+1;   //Generates a random number between 1 and 100.
guesses[count] = PlayGame(randNumber);
count++;
cout<<\"Do you want to play it again? (Y/N)\";
cin>>again;
if(again != \'y\' && again != \'Y\')
break;
}
double average = 0;
cout<<\"The guesses you took to play each game is: \";
for(int i = 0; i < count; i++)
{
cout<<guesses[i]<<\" \";
average += guesses[i];
}
average /= count;
cout<<endl<<\"The average guesses per game is: \"<<average<<endl;
ifstream fin;
ofstream fout;
fin.open(\"NumberGuessResults.txt\");
fout.open(\"NumberGuessResults.txt\");
if(fin.eof())
{
fin.close();
fout<<average;
}
else
{
fin>>previousGuesses;
if(average > previousGuesses)
{
cout<<\"Congratulations you have improved your score since the last time you played.\"<<endl;
fout<<average;
fout.close();
}
else
cout<<\"Your score was better last time you played than today.\"<<endl;
}
  
}

Files and Arrays Project I – Build a Guessing Game Program 1. Write a program to implement the Number Guessing Game. The main method should loop as long as the
Files and Arrays Project I – Build a Guessing Game Program 1. Write a program to implement the Number Guessing Game. The main method should loop as long as the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site