[Help] The computer repeats the guessed number

So after telling higher or lower, the new guesses seem to not change. Is it because the program isn’t guessing anymore, or is it because the guesses are the same? i am not sure.
i tried to avoid repeating the guesses by doing the following to have it guess lower:
03 PM

And the following to have it guess higher:
25 PM

But it still runs into the same problem. i would like to understand the reason behind it more than i actually solving it…

Thank you

Hello @uhadi,

Could you post your full code please, via copy and paste ideally too, rather than by screen shot, I will take a look for you :slight_smile:


See also;

Hello Rob,
Thank you for offering to help :slight_smile:
Below is the full code, i hope it’s helpful.
Thank you in advance:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class NumberWizard : MonoBehaviour {

	int max2;
	int min;
	public int guess2;
	public int MaxGuessesAllowed = 20;

	public Text text;

	//List <int> GuessedNumbers = new List <int> ();

	public void GuessHigher () 
	{
		min = guess2;
		NextGuess2 ();
	}

	public void GuessLower ()
	{
		max2 = guess2;
		NextGuess1 ();
	}

	// Use this for initialization
	void Start ()	{
		StartGame ();					
		}

	void StartGame ()
	{
		max2 = 1000000;
		min = 1;
		guess2 = (max2 + min)/2;
		NextGuess0 ();

	}

	void NextGuess0 ()
	{
	guess2 = Random.Range (min,max2 +1);
	text.text = guess2.ToString();

	}

		void NextGuess1 ()
	{
		guess2 = + Random.Range (min, guess2);
		text.text = guess2.ToString ();

		
	}

	void NextGuess2 ()
	{
		guess2 = Random.Range (guess2, max2 + 1);
		text.text = guess2.ToString (); 
		
	}


	public void TheGussedNumber ()
	{
		text.text = guess2.ToString (); 
	}

} 

Oh and sorry if the code seems a bit messy, i have been testing my own solutions, so the code might look a little strange.

Hello @uhadi,

You’re more than welcome, so let us see if we can establish what is happening.

I am a little confused with the number of methods you have at the moment, is there a reason you have the NextGuess0(), NextGuess1() and NextGuess2() methods? From memory, in the course there was just one method named NextGuess(). If you are trying to do something outside the scope of the course itself, that’s great, but if you explain what that may help me to understand the background of the issue a little better.

Anyway, I will outline below the execution steps and the value of each variable as we go.


  • Step 1

    script is initialised

    max2 = 0
    min = 0
    guess2 = 0
    MaxGuessesAllowed = 20
    
  • Step 2

    Start() is called

    StartGame() is called

  • Step 3

    max2 = 1,000,000
    min = 1
    guess2 = (1,000,000 + 1) / 2, thus, guess2 = 500,000 (the 0.5 is lost as guess2 is an _int_)
    

    NextGuess0() is called

  • Step 4

    guess2 = random number between 1 (min) and 500,000+1 (max2 +1), we will assume guess2 = 372,501

    Text output is updated (assumes you have dragged the UI Text game object from the Hierarchy into the exposed Text field of this script)

  • Step 5

    Nothing else in this script executes unless there is interaction from the player

    I assume that you have wired up the OnClick() events of the buttons for Guess Higher and Guess Lower but I am unable to determine to what from the information provided.


Based on the above you should have a random value between 1 and 500,001 displayed in your UI Text game object within the scene.

Please let me know how you have wired up your buttons to the script, e.g. which methods are you calling from which buttons. A screenshot which shows the Inspector and the OnClick() event details for each button would be beneficial.

Hello again,
Thank you for the quick reply.
As for the methods bit, i wanted to have a separate scene for when the computer guesses the number on the first try (the chances of that happening are less than 1 in a 1000000, but still), so i made NextGuess0 as the first step to link up to an alternate win scene. i do not use it right now, so sorry for adding it in the code.
NextGuess1 & NextGuess2 were a step towards insuring that the previous chosen number would not be chosen again. That was again my tinkering in hopes of fixing the primary issue.

The game works perfectly as in the lecture. But for some reason the chosen number after a couple of times does not change. And that is the issue i wish to resolve, or maybe just understand, since this is a learning experience.

Below are the screenshots of the different buttons and the method wired to each:

Higher Number:

Lower Number:

Please let me know if you need any more details. And thank you so much for taking the time to review my attempt.
All the best

[Edit: i wanted to clarify: the method NextGuess0 is actually used in the code as is, but i implemented it in the first place to have it link to another scene. Again, sorry for the messy code structure. i will be sure to clean up the code if i have any future questions]

Hi,

Thanks for the above and the explanation…

Regarding the “computer guesses the number on the first try” - I’m not sure you really needed a different method for that? The only person who really knows the real number is the player, you could argue that, given enough attempts, once the computer narrows the possibilities down to only one remaining number then the computer must have guessed correctly, that would be well passed the first go.

An easier approach would be to perhaps just keep track of the number of attempts the computer has made. So at the beginning, it guesses a number, attempts += 1 (attempts = 1 now) - and then have a check… if attempts = 1 - load the “wow, as number wizards go I am awesome” scene :slight_smile:

If the value of attempts is greater than 1, just carry on as normal, no need to load a special scene.

Ok, so let’s carry on with working through it.


  • Step 6

    we’ll assume the player clicks higher

    GuessHigher() is called

    min = 372,501
    

    NextGuess2() is called

  • Step 7

    guess2 = random number between 372,501 (guess2) and 1,000,000+1 (max2+1), we will assume guess2 = 475,123
    

    Text output is updated (475,123)

  • Step 8

    we’ll assume the player clicks lower

    GuessLower() is called

    max2 = 475,123 (guess2)
    
    `NextGuess1()` is called
    
    
  • Step 9

    "guess2 = + Random.Range (min, guess2);"
    

    That + character doesn’t look right there - what was it you were hoping this line would do?


To this point, I’ve not seen anything which would suggest the output is not going to get updated.

If you are happy to share the project it may just be easier for me to take a quick look at it, fully, rather than trying to step through like this. The forum will allow you to upload a file up to 10mb, so if your project is smaller than that you could zip it up and share it here. Alternatively, if you have Google Drive or DropBox those could be used too, just paste the link into your reply. :slight_smile:

hello,
Yes that plus sign had no business being there, i must have missed it. Thank you.

i have uploaded the game to gamebucket.io, but i get an error whenever i press play once the game start. It is a long error massage, so i took 2 screenshots to show all the massage:

The game runs just fine on Unity. SO the problem might be with the build… i am not sure though.

Again, happy to take a look if you can share the project files.

Privacy & Terms