Hi all, just finished section 2: Code included below, moved things around a bit, guided by the idea that the start and update functions should be close together. Also made the brackets more symmetrical for easier read. Still don’t understand how moving the max = max + 1 bellow the print function would change anything, 1001 error is the sort of bug that would bug me a lot. Will take a look at it another time.
I’ve also created a flow graph of the the script to make it easier for me to uderstand the separate components. I’m using https://drakon-editor.com to make the graphs.
…
using UnityEngine;
using System.Collections;
public class NumberWizards : MonoBehaviour {
int max;
int min;
int guess;
// Use this for initialization
void Start ()
{
StartGame();
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
min = guess;
NextGuess();
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
max = guess;
NextGuess();
}
else if (Input.GetKeyDown(KeyCode.Return))
{
print("I won!");
StartGame();
}
}
//functions
void StartGame ()
{
max = 1000;
max = max + 1;
min = 1;
guess = 500;
print ("//////////////////////////////////");
print ("Welcome to Number Wizard");
print ("Pick a number in your head, but don't tell me!");
print ("The highest number you can pick is " + max);
print ("The lowest number you can pick it " + min);
print ("Is the number higher or lower than? " + guess);
print ("Up = higher, down = lower, return = equal");
}
void NextGuess ()
{
guess = (min + max)/2;
print ("Higher or Lower than " + guess + "?");
print ("Up = higher, down = lower, return = equal");
}
}