So, let’s run through what’s happening then…
We will say that I choose 925 as my number, Start()
is called.
Start()
- min = 1
- max = 1000
-
NextGuess()
is called
NextGuess()
- min(1) doesn’t equal max(1000)
- guess = random number between 1 (inclusive) and 1000 (exclusive)
Notes on NextGuess() at this point;
- 1000 cannot be returned as a possibility because max is exclusive
- validation of guesses taken could be improved
for the sake of argument we will assume guess now equals 356
-
guesstext.text
is updated
I indicate that my number is higher and click the relevant button, GuessHigher()
is called;
GuessHigher()
- min = 356+1, thus min = 357
- max remains unchanged, thus max = 1000
-
NextGuess()
is called
NextGuess()
- min (357) doesn’t equal max (1000)
- guess = random number between 357 (inclusive) and 1000 (exclusive)
Notes on NextGuess() at this point;
- 1000 cannot be returned as a possibility because max is exclusive
for the sake of argument we will assume guess now equals 926
-
guesstext.text
is updated
I indicate that my number is lower and click the relevant button, GuessLower()
is called;
GuessLower()
- min remains unchanged, thus min = 357
- max = 926 - 1, thus max = 925
-
NextGuess()
is called
NextGuess()
- min (357) doesn’t equal max (925)
- guess = random number between 357 (inclusive) and 925 (exclusive)
Notes on NextGuess() at this point;
- 926 could not have been returned as a possibility because max is exclusive
- 925 can now no longer either be returned for the same reason
- the game can no longer guess my number
for the sake of argument we will assume guess now equals 622
-
guesstext.text
is updated
I indicate that my number is higher and click the relevant button, GuessHigher()
is called;
GuessHigher()
- min = 622+1, thus min = 623
- max remains unchanged, thus max = 925
-
NextGuess()
is called
NextGuess()
- min (623) doesn’t equal max (925)
- guess = random number between 623 (inclusive) and 925 (exclusive)
Notes on NextGuess() at this point;
- 925 cannot be returned as a possibility because max is exclusive
- the game cannot win and will eventually run out of guesses
for the sake of argument we will assume guess now equals 778
-
guesstext.text
is updated
This is just one example of a run through the game using the logic from your code, as you can see, it unfortunately breaks.
Because max will always been exclusive you would probably be better of adding 1 to it at the beginning, e.g. max = 1001
, this would give you an actual initial range between 1 and 1000.
Your +1 on the min appears to work and prevents the same number coming up again.
Your -1 on the max within GuessLower()
is a problem and also unnecessary, as that number cannot be returned again as max is exclusive.
It would be worth spending some time going through something similar to the above yourself for a variety of combination, for example, higher, higher, higher, higher, higher, then lower, lower, lower, lower, lower - then a mixture. Test to see that you can actually return a number in the correct range.
The cheater test, can this be achieved? Perhaps with more allowed guesses?
The test for whether the number of allowed guesses have been taken could be improved. For example;
- the variable name maxGuessesAllowed suggests it is a constant, e.g. a number that won’t change but is then decreased through the game play, it may read better if it were called guessesRemaining, it’s a minor detail but something that may help you going forward.
- within
NextGuess()
you currently create a new guess, reduce the number of guesses, check if they have run out, if so load another scene - if that was the case then you just create the guess for nothing. Tweaking your if
statement logic there may help with this. Equally, you could consider calling a method to test how many guesses are remaining from the GuessHigher()
and GuessLower()
methods, before running the existing code, that would then map it to the user input more directly and that method would be the one responsible for loading the “Win” scene.
Hope the above helps, sorry its a bit of a textual mess, layout features are limited on the forum but I’ve tried to be fairly consistent and space things out a bit.