I am writing a game where the player has to guess a random number between 1 - 100. It now tells if the inserted number is higher or lower than the correct random number, but I would like to add a new feature.
Would it be possible to somehow compare the players guesses to the correct answer and instead of saying "The number is lower than X" I would like it to say "The number is lower than X. The correct answer is between X and Y."
This means that I would need to compare the values added into the vector lastGuess to the variable correct and print the closest lower and higher value in place of X and Y.
I imagine that a problem would also be that there is no higher or lower value after the very first guess.
Could someone give me a hint here? If this is too advanced for a newbie like me please tell it also. I don't know where to start.
There is no any real reason for implementing this feature, I just want to learn. :)
int main()
{
srand(time(NULL));
int correct = (rand()%100)+1;
int guess;
int guesses = 0;
int maxGuesses = 8;
int i = 0;
size_t size = 8;
vector<int> lastGuess(size);
cout << "\nGuess the number." << endl;
cout << "It must be between 1 - 100. You have only 8 attempts." << endl << endl;
do
{
cout << "Insert your guess: ";
cin >> guess;
guesses++; //Total number of guesses
lastGuess[i++]=guess; //Guesses in a list
if(guess < correct)
{
cout << "The number is higher than " << guess << ". " << endl;
cout << "That was your " << guesses << ". guess. " << endl << endl;
}
elseif(guess > correct)
{
cout << "The number is lower than " << guess << ". " << endl;
cout << "That was your " << guesses << ". guess. " << endl << endl;
}
else
{
break; //After correct answer or max number of attempts passed the loop ends.
}
} while (guess != correct && guesses<maxGuesses);
if(guess==correct)
{
system("cls");
cout << "\nCorrect! The answer was " << correct << ". " << endl;
cout << "You needed to guess " << guesses << " times to get it right." << endl;
cout << "\nYour guesses were: ";
i=0;
for(guesses; guesses>0; guesses--)
{
cout << "[" << lastGuess[i++] << "] ";
}
cout << "\n";
cin.ignore();
}
else
{
system("cls");
cout << "You lost. You weren't able to get it right with " << maxGuesses << " attempts." << endl;
cout << "The correct answer would have been " << correct << "." << endl;
cin.ignore();
}
}
The way I would approach this is on the first guess I would state that the number is either higher or lower than the guess given. Then with the second guess, I would state it would be either be between or still higher/lower than the guess.
To make it know if something is either higher, lower, or in between the guesses, I would use subtraction with an int to determine how to output the hints. So, if the I guessed is lower than the number being guessed, I would do something like my_guess - correct_answer = -n.
the '-' in this case would state that the guess was lower so I would say that the right number is greater than the guess. Do the same with a higher number except the correct_number would be positive.
Now to determine when someone is getting closer, I would do this through the array of guesses the user has done and keep the numbers that were closest to 0 (since when the guess minus the answer would result in 0). For example, as I was to guess from a number that was originally lower than the answer and kept guessing lower, the negative number would get closer to 0. The same would happen if I was going from the highest number guessed and kept working my way down to the answer.
So overall, just keep track the lowest and highest numbers that are close to 0 based on a '-' sign or a '+' sign. That's how I personally would address this.