Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your task is to implement one of the very popular games in history -- number gue

ID: 3562609 • Letter: Y

Question

Your task is to implement one of the very popular games in history -- number guessing game!

Source code for this game is widely available. Here is the implementation you should use:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int num; // holding a random number generated by computer
    int guess; // holds the guess of the user
    bool isGuessed; // true if user guessed the number correctly

    srand(time(0)); // initialize randomness with time
    num = rand() % 100; // ensure the number is 0 to 100

    isGuessed = false;

    cout << "I have a number between 0 and 100. Try to guess what it is." << endl << endl;

    while (!isGuessed)
  {
        cout << "So, what is your guess? ";
        cin >> guess;

        if (guess == num)
        {
           cout << "Phew... You got it!!" << endl << endl;
           isGuessed = true;
        }
        else if (guess < num)
        {
           cout << "Your guess is too low, go higher!" << endl << endl;
        }
        else if (guess > num)
        {
           cout << "Your guess is too high, go lower!" << endl << endl;
        }
    }

    system("PAUSE");
    return 0;
}


Step #1:
First, copy the above code into visual studio, compile, run and enjoy playing the game for couple of minutes :-)

Step #2:
Modify the program so that in the end (with "You got it!!" message), it would also tell the user in how many tries he/she found the right number. Hint: you need a variable to hold the number of tries, and this variable should be incremented by one at each try. And in the end, you simply output that variable.

Step #3:
Modify this program so that user gets at most 6 tries. If not guessed in 6 tries, program should indicate that user has lost the game and exits -- you probably want to use "break" statement to exist out of the loop.

Step #4:
Currently program only tells the user if the guess is high or low. Modify this program so that it would give an indication of how high or how low the guess is.

If the guess is more than 50 above or below the number, output
"Way too high" or "Way too low".
If less than 20,
"High, but getting closer" or "Low, but getting closer".
If less than 10,
"almost there but still high", "almost there but still low".

Notice that for between 20 and 50, we simply provide the default message. you can play around with these messages, this is provided as an example.

My suggestion on this step is to create a new variable that would keep the difference between the guessed number and the target number. Let's call it diff. So declare diff at the begining of your program and then set its value after getting the guess from user.

At this point you need to change the if statements so that:

if diff == 0, then user found the number.
else if diff >= 50, then user is way too high
else if diff >= 20, the user is high
.
.
else if diff <= -50, then user is way too low
else if diff <= -20, then user is low
.
.

I think you got the idea :-))

Step #5:
Modify the program so that it does not exit after each game, but instead asks the user if he/she wants to play again. This is very much like the assignment we had earlier for the loan calculator.

Step #6:
Modify the program so that it would keep a high score. Highest score is the least amount of tries to find the target number.

One way to handle this would be to declare a variable that would hold the high score. Let's call this variable highscore.

After each successful guessing of the target number, we should check number of tries to the high score, if lower, new high score is set to the number of tries in that game.

Step #7:
Modify the program so that the high score is saved to a file, otherwise each time we run the program high score resets.

Explanation / Answer

Storing score in highscore.txt

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <string>

using namespace std;

int main(){
   int highscore;
   int scorestored;
   int diff; //difference between random number and your guess.
   int tries = 0;
   int num; // holding a random number generated by computer
   int guess; // holds the guess of the user
   bool isGuessed; // true if user guessed the number correctly
   srand(time(0)); // initialize randomness with time
   num = rand() % 100; // ensure the number is 0 to 100

   isGuessed = false;


   cout << "I have a number between 0 and 100. Try to guess what it is." << endl << endl;

   while (!isGuessed)
   {
       char answer;
       cout << "So, what is your guess? ";
       cin >> guess;
       diff = num - guess;
       highscore = tries;
       tries = tries++;
       if (diff == 0){
           cout << "You got it!! in tries " << tries << endl;
           isGuessed = true;
       }
       else if (diff >=50){
           cout << "Your guess is very low , go higher!" << endl << endl;
       }
       else if (diff >= 20){
           cout << "Your guess is low, go higher!" << endl << endl;
       }
       else if (diff <=-50){
           cout << "Your guess is way to high , go lower !" << endl << endl;
       }
       else if (diff <= -20){
           cout << "Your guess is high , go lower!" << endl << endl;
       }
       else if (diff >= -20 && diff <= 50){
           cout << "Your close!" << endl << endl;
       }
       if ( tries > 5 ){
           cout << "They highscore is" << highscore - tries << endl;
           cout<<"Game over - You failed to guess the number... Play again?[y/n]";
           cin >> answer;
           if (answer!='n'){
               isGuessed=false;
               //break;
           }
           else{
               break;
           }


       }

   }
   string line;
   ifstream myfile ("highscore.txt");
   if (myfile.is_open())
   {
       while ( getline (myfile,line) )
       {
           scorestored = atoi(line)
       }
       myfile.close();
   }

   if(scorestored < highscore){
       ofstream myfile ("highscore.txt");
       if (myfile.is_open())
       {
           myfile << highscore;
           myfile.close();
       }
   }
   return 0;
}