This is my own code rate it

Disclaimer:
I screen shot doesnt mean i steal content, just to show the issue i find.

this is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;


public class NumberWizard : MonoBehaviour
{
    [SerializeField] int max;
    [SerializeField] int min;
    [SerializeField] TextMeshProUGUI guessText;
    
    int guess;
    void Start()
    {
        StartGame();
    }

    void StartGame()
    {        
        guess = Random.Range(min, max);
        guessText.text = guess.ToString();               
    }

    public void OnPressHigher()
    {
        min = guess;        
        NextGuess();       
    }

    public void OnPressLower()
    {
        max = guess;
        NextGuess();        
    }
    // Use this for initialization
    void NextGuess()
    {
        guess = (max + min + 1) / 2 ;
        guessText.text = guess.ToString();
    }
    // Update is called once per frame
}

Compare with Rick

public class NumberWizard : MonoBehaviour
{
    [SerializeField] int max;
    [SerializeField] int min;
    [SerializeField] TextMeshProUGUI guessText;
    
    int guess;

    void Start()
    {
        StartGame();
    }

    void StartGame()
    {
        NextGuess();
        guessText.text = guess.ToString();
    }

    public void OnPressHigher()
    {
        min = guess + 1;
        NextGuess();
    }

    public void OnPressLower()
    {
        max = guess - 1;
        NextGuess();
    }

    void NextGuess()
    {
        guess = Random.Range(min,max+1);
        guessText.text = guess.ToString();
    }
}

The question is:
0. I misunderstanding about the updated same number.(ignore it if my code is not right.)

  1. Mine is stopped when I guess the right number.
  2. Rick’s code keeps updating when I push the higher.
    but clarify, am I doing the right code or I miss something?
  3. is this mean instead divide /2 the computer generate random number between max n min?

Hi,

Thank you for your questions.

  1. Rick’s code keeps updating when I push the higher.
    but clarify, am I doing the right code or I miss something?

We do not prevent the player from cheating. However, if your game stops and does not increase the “guess” value, that’s great. :slight_smile:

  1. is this mean instead divide /2 the computer generate random number between max n min?

In your code, only the first “guess” is a “random” value because StartGame gets called only once. In Rick’s code, the Random.Range method gets called in the NextGuess method. His idea was to make a “random guess” each time the player presses “Higher” or “Lower”.

However, that’s just Rick’s idea. If you are happy with your game, keep your code. :slight_smile:


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms