I wanted to make this absolutely bugfree, so I came up with the following addition to make it work.
Instead of in the NextGuess()
method, add an if-statement in the OnPressHigher()
and OnPressLower()
methods:
public void OnPressHigher()
{
if (max > guess)
{
min = guess + 1;
NextGuess();
}
else if (guess == 1000){Debug.Log("The maximum you can choose is " + max);}
else{Debug.Log("You said it would be lower than " + (max + 1) + ". You're lying!");}
}
public void OnPressLower()
{
if (min < guess)
{
max = guess - 1;
NextGuess();
}
else if (guess == 1){Debug.Log("The minimum you can choose is " + min);}
else{Debug.Log("You said it would be higher than " + (min - 1) + ". You're lying!");}
}
Note that the else if
and else
statements are just for debugging, to see if the part of the code is being executed when expected. Possibly if you know what you’re doing, you could attach a pop-up message to show the player why his input is wrong.
In short, this is what the code does:
OnPressHigher()
checks if the max
is still higher than the guess
. If so, it can execute the code. If the max
isn’t higher than the current guess
, that means the player shouldn’t be able to say his number is higher than the guess
.
Same with OnPressLower()
but looks at the min
instead of the max
: if the min
is lower than the current guess
, execute the code. If the min
is not lower than the current guess
, the player should not be able to say that his number is lower than the current guess
.
I hope this helps anyone who is also looking for an airtight solution. (If it’s not airtight, please let me know, I might just have missed something.
Extra note: I hard coded in the max and min for the debug message of reaching the max and min, you could choose to set these to variables, but they will have to be seperate from the current max and min variables. Create a initialMax and initialMin in the Class scope, set them to max and min in the StartGame() method and voila, you should be good to go.