Hi peoples. I would really appreciate some help on this as I’ve been banging my head against the wall. My powers of logic have a long way to go yet:
I’m creating a number guessing game. You simply think of a number in your head and the computer makes random guesses. You tell the computer using the buttons whether your number is Higher or Lower than its guess, then select You Guessed It when it gets the number right.
The game works well except for when the player clicks continuously between Higher and Lower. The player can do this for a few times before the logic finally kicks in and tells you you’ve lost. I cannot fathom out why things are happening this way, and any extra code I add seems to either not help or make things worse.
I would have thought that I need to add to the conditional checking in the GuessHigher() and GuessLower() methods, but I seem to be having a mental block on what to put in.
As a little bonus, if anyone can suggest ways of improving their logical thinking (other than by just practising coding), I’d be really grateful as its something I’m not very confident about. Or, perhaps, if this is simply a case of learning more debugging tricks which could lead me to a solution more easily, I’d be grateful for some pointers on that too.
Here’s a link to the current playable build on gamebucket.io: https://gamebucket.io/game/3e0d5e3f-efd9-4a5e-9b72-c85e1eb0fbc4
Here is the code behind the script:
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class NumberWizard : MonoBehaviour
{
int max;
int min;
public int guess;
public int maxGuessesAllowed = 10;
public Text text;
public Text guessesRemaining;
// Use this for initialization
void Start()
{
StartGame();
UpdateDisplay();
}
void StartGame()
{
max = 1000;
min = 1;
guess = GenerateNumber();
Debug.Log("StartGame() Min: " + min + " Max: " + max + " Guess: " + guess);
}
public void GuessHigher()
{
Debug.Log("=============\nPressed Higher");
if (max > guess)
{
Debug.Log("Pre-NextGuess() Min: " + min + " Max: " + max + " Guess: " + guess);
if (guess == min) min++;
else min = guess;
NextGuess();
Debug.Log("Post-NextGuess() Min: " + min + " Max: " + max + " Guess: " + guess);
}
else if (max <= guess) { SceneManager.LoadScene("Lose2"); }
}
public void GuessLower()
{
Debug.Log("=============\nPressed Lower");
if (min < guess)
{
Debug.Log("Pre-NextGuess() Min: " + min + " Max: " + max + " Guess: " + guess);
if (guess == max) max--;
else max = guess;
NextGuess();
Debug.Log("Post-NextGuess() Min: " + min + " Max: " + max + " Guess: " + guess);
}
}
public void GuessCorrect()
{
SceneManager.LoadScene("Lose");
StartGame();
}
void NextGuess()
{
if (maxGuessesAllowed <= 0) SceneManager.LoadScene("Win");
else
{
guess = GenerateNumber(guess);
maxGuessesAllowed -= 1;
UpdateDisplay();
}
}
void UpdateDisplay()
{
text.text = guess.ToString() + " ?";
guessesRemaining.text = maxGuessesAllowed.ToString();
}
int GenerateNumber(int lastGuess)
{
// Use recursion to ensure a different number is generated each time
int randNum = UnityEngine.Random.Range(min, max + 1);
//Debug.Log("randNum: " + randNum + " lastGuess: " + lastGuess);
if (randNum == lastGuess)
{
return GenerateNumber(randNum);
}
else
{
return randNum;
}
}
int GenerateNumber()
{
// Use recursion to ensure a different number is generated each time
int randNum = UnityEngine.Random.Range(min, max + 1);
//Debug.Log("Random Number: " + randNum);
return randNum;
}
}
Many thanks in advance.