I'm currently creating a Guess Game via a Udemy course based on C# using Unity Engine. What I'm trying to do is outside the scope of the course.
I got to the point you see below where when I press the "up arrow" or "down arrow" a random number is generated between two numbers: (avg, max) and (avg, min).
As it stand right now the numbers generated are always higher or lower than the 'avg' based on the key that's pressed, but the each number generated after that won't necessarily be higher or lower than the previously generated number.
For example. I could choose lower than 393 (the 'avg') and get 242. But if I press lower again it could be 345. While it's lower than the 'avg' I want to have each randomly generated number be lower than the last randomly generated number (or higher depending on the key press).
I thought maybe assigning a variable to the function was the right thing to do. I have an unused integer 'c' that I was thinking of using.
Not sure how to make this happen....
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NumberWizard : MonoBehaviour
{
int max = 734;
int min = 52;
int avg = 393;
int a;
int b;
int c;
// Use this for initialization
void Start()
{
StartGame();
}
void StartGame()
{
max = 734;
min = 52;
avg = 393;
Debug.Log("Welcome to Number Wizard you filthy son of a bitch.");
Debug.Log("Pick a goddamn number.");
Debug.Log("The highest number is: " + max);
Debug.Log("The lowest number is: " + min);
Debug.Log("Tell me if your number is higher or lower than " + avg);
Debug.Log("Push Up = Higher, Push Down = Lower, Push Enter = Correct!");
max = max + 1;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log("You chose higher. What is your guess? ");
NextGuess();
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
Debug.Log("You chose lower. What is your guess?");
NextGuess2();
}
else if (Input.GetKeyDown(KeyCode.Return))
{
Debug.Log("You are correct.");
StartGame();
}
else if (Input.GetKeyDown(KeyCode.KeypadEnter))
{
Debug.Log("You are correct.");
StartGame();
}
else if (Input.anyKeyDown)
{
Debug.Log("Input invalid. Try again.");
}
}
void NextGuess()
{
a = Random.Range(avg, max);
Debug.Log(a);
}
void NextGuess2()
{
b = Random.Range(avg, min);
Debug.Log(b);
}
}
Aucun commentaire:
Enregistrer un commentaire