Your task is to implement one of the very popular games in history -- number gue
ID: 3562608 • 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 #include #include 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
#include #include #include using namespace std; char chr; int main() { srand(time(NULL)); //the function that generates random numbers int number=rand()%100+1; //the range of the random numbers int guess; //The guess is stored here int tries=0; //The number of tries stored here char answer; //The answer to the question is stored here answer='y'; while(answer=='y'||answer=='Y') { while (triesRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.