In reading the initial thread, there are many of us that realized the issue if players lie and continue to press higher. I am sharing my solution to that as it makes a couple of subtle changes to the suggested code. Two changes.
-
Subtracting 1 from max during the OnPressLower() method and then adding 1 back in the NextGuess() method seemed redundant. Because the Max end of the Random is exclusive, I don’t think you have to do anything to ensure that the max is NOT guessed a second time. I removed those and it seems to be functioning correctly.
-
I added an if statement that only increments the min in the event that min < max. This ensures that you cant have minimum that is greater than the max which creates the ability to go above the max number.
Note: I named my variables minNumber and maxNumber
Any thoughts?
// Method to Process User Pressing Higher Button
public void OnPresssHigher()
{
minNumber = guess;
if (minNumber < maxNumber) minNumber = minNumber + 1;
NextGuess();
}
// Method to Process User Pressing Lower Button
public void OnPressLower()
{
maxNumber = guess;
NextGuess();
}
// user this method to update the gues
void NextGuess()
{
guess = Random.Range(minNumber, maxNumber);
guessText.text = guess.ToString();
}