[Soved] How do I stop the player from cheating?

There’s an exploit where the player can win every time.

Observed behavior: Player can continue to click the higher/lower button, even if the number isn’t changing anymore.

Desired behavior: Player automatically gets sent to the lose screen if they click higher/lower after the number won’t change anymore.

Steps to reproduce: Start by clicking the higher number button and keep clicking it after the number is at it’s max (1000). Eventually the computer’s guesses will run out and you will win, even though the computer can’t go higher. Same works for when the lower number button is clicked and the current guess is at 1.

Things I’ve tried: I tried setting the scene to the lose screen when the max equals the min. This was the only thing I could think to try.

Is there a way to go to the lose screen if the number doesn’t change? The computer should never pick the same number twice, right? Is there a way to prevent this? When max guess is at 10000, it guesses 9997 or 9998 twice.

You could always store the last guess in its own variable, then perform a check to see if the current guess is the same as the last guess, and send the player to the lose screen if that’s the case. Something like:

void NextGuess()
{
    currentGuess = Random.Range(min, max+1);
    ShowGuess();
    maxGuesses--;
    if (maxGuesses <= 0)
    {
        // Go to win screen 
    }
    if (lastGuess == currentGuess)
    {
        // Go to lose screen
    }
    lastGuess = currentGuess;
}

There is a problem with this though, if you use a randomly guessing algorithm there’s always a chance that it will guess the same number twice, and then declare itself the winner.

Is this just a known issue with this game?

I had to change a couple of things to stop the duplicate guesses.

For Guess Higher, set min to guess + 1. For Guess Lower, set max to guess - 1. This limits Random.Range(min, max+1) to only numbers not guessed.

That opened up a couple of other issues. Here’s my code snippets for GuessHigher and Guess Lower:

	public void GuessHigher()
	{
		if (guess < max) 
		{
			min = guess + 1;
			NextGuess ();
		}
		else
		{
                        if (min == max)
			    SceneManager.LoadScene ("LoseCheat");
		}
	}

    	public void GuessLower()
    	{
    		if (min < guess) 
    		{
    			max = guess - 1;
    			NextGuess ();
    		}
    		else
    		{
                        if (min == max)
    			    SceneManager.LoadScene ("LoseCheat");
    		}
    	}

If min == max, then the user manipulated the guesses to the point there are no available numbers. Clearly, that’s cheating or the user forgot the pick.

If you have any other question, ask away. Also, I posted a link to my version a day or so ago. You can see the code in action there.

Thanks,
PaulD

This lets the number go up to 1001. It also doesn’t work if you get to 1000 and then try guessing lower a bunch.

Could you explain or show how you managed to get that behavior? I used Debug.Log to display min/max/guess each round and never experienced that behavior. The key to guessing is compressing the available range. Once the min is say, at 500, you should never be able to tell it to go lower than that. Same with max.

Thanks,
PaulD

I guess the 2nd number in Random.Range is included and not explicit in Unity 2017 or Visual Basic.

Privacy & Terms