I am trying to do the extra credit and for the last part of the project and am having trouble getting my code to work. Whenever I run my code the first part runs fine, but it skips the part where it is asking for the user to confirm the maximum and will immediately start guessing the number instead. I am guessing that this has something to do with the Update() function being called every frame, but I tried modifying that code as well (using lowercase update(); and then calling it in the function to get it to re-loop if it does not guess the answer). Unfortunately, it did not work either. I’ve been working on this for a coupole of hours now, and I can’t seem to figure out how to get this to work. Would anyone be able to help me out?
Side Note: I have also had a probem recently with Monodevelop not showing anything in the console (no errors, no info, nothing). I made sure that the button prompts were pressed in, but I can’t get the game to even show up on the console anymore! Doe’s anyone know how to fix this?
After struggling for hours, I actually ended up solving the extra credit problem altogether. I will post my problem and solution below in case anyone else is having trouble solving the extra credit as well.
Problem: I was attempting to have the user select a maximum number, but whenever I ran the code, the program would not ask the member to choose a maximum range and would instead immediately start guessing the number.
Solution: I noticed that in my code above, the update log only contained code for guessing the player’s number instead of also having code to have the player set their maximum range. In order to make this happen, I had to introduce a new concept (bool) to allow for the program to first have the player confirm the maximum range, and only after completing that criteria, then the program would be allowed to guess the number.
My completed code is below if anyone has any feedback for me. I am brand new to C# and have very little JavaScript programming under my belt so any feedback would be appreciated. Thanks guys!
Completed Code:
using UnityEngine;
using System.Collections;
public class NumberWizards : MonoBehaviour {
// Use this for initialization
int max;
int min;
int guess;
bool hasPlayerSelectedRange = false;
void Start () {
startGame();
}
void startGame () {
max = 1000;
min = 1;
guess = Random.Range(min, max);
hasPlayerSelectedRange = false;
print ("========================");
print ("Welcome to Number Wizard");
print ("What would you like the maximum number that you can guess up to be?");
nextMaxGuess();
}
void playerChoseMax() {
guess = Random.Range (min,max);
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");
max = max +1;
}
void nextMaxGuess () {
print ("Do you want it to be " + max + "?");
print ("Up = higher, down = lower, return = equal");
}
// Update is called once per frame
void Update () {
if (hasPlayerSelectedRange == true){
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();
}
}
if (hasPlayerSelectedRange == false) {
if (Input.GetKeyDown(KeyCode.UpArrow)) {
max = max *10 ;
nextMaxGuess();
} else if (Input.GetKeyDown(KeyCode.DownArrow)) {
max = max / 2;
nextMaxGuess();
} else if (Input.GetKeyDown(KeyCode.Return)) {
print("Great! Let's Continue!");
hasPlayerSelectedRange = true;
playerChoseMax ();
}
}
}
void NextGuess () {
guess = Random.Range (min,max);
//guess = (max + min) / 2;
print ("Higher or lower than " + guess);
print ("Up = higher, down = lower, return = equal");
}
}