Hello Couple of hours ago i saw a guy “Uhadi” here tried to make a code that responds only once. After i saw it his idea lead me to add starting and finishing inputs to my Number Wizard game. I made some researches and here is what ive accomplished . https://pastebin.com/KhDTzRjm It works pretty neat. But I was wondering if anyone pro coder here can tell me how could this be done in a more professional way? Is my code so trash by its logic? Thanks in advance
Hello @ehturan,
The code you have written is very clean and easy to follow, and is certainly not trash, well done.
Regarding whether it could be improved…
In your Update()
method you have three cases where the state of checkStart
and checkFinish
are all identical, and four cases where you are interested in checkFinish
being false. The following example consolidates that in a different way and also uses specific methods for changing the state and values of the variables.
Example;
using UnityEngine;
public class NumberWizards : MonoBehaviour
{
int max, min, guess;
bool checkStart;
bool checkFinish;
void Start()
{
Initial();
}
void Update()
{
// start the game
if(!checkFinish)
{
if(checkStart)
{
StartGame();
}
else
{
// during the game
if(Input.GetKeyDown(KeyCode.DownArrow))
{
GuessLower();
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
GuessHigher();
}
else if (Input.GetKeyDown(KeyCode.Return))
{
NumberWizardWins();
}
}
}
else
{
// after the game
if(Input.GetKeyDown("a")
{
Initial();
}
}
}
private void Initial()
{
max = 1000;
min = 1;
guess = 500;
checkStart = true;
checkFinish = false;
Debug.Log("<color=red>===================================================</color>");
print("Welcome to Number Wizard");
print("Pick a number in your head, but do not tell me xD");
print("It can be min. " + min + " and max. " + max);
print("Whenever you are ready press spacebar to continue");
max = max + 1;
}
private void StartGame()
{
checkStart = false;
print("GAME HAS STARTED! MAY CHANCE BE WITH YOU!");
print("Is the number higher or lower than " + guess + "?");
print("Up = higher, down = lower, enter = equal");
}
private void GuessLower()
{
max = guess;
NextGuess();
}
private void GuessHigher()
{
min = guess;
NextGuess();
}
private void NumberWizardWins()
{
print("I won xD");
print("Press A to restart");
print("<color=red>===================================================</color>");
checkFinish = true;
}
}
Minor notes on consistency…
-
In your original code you check for true or false in two different ways, e.g. “== true” and “!= false”.
“== true” and “!= false” are obviously the same thing.
I would suggest that being consistent in your approach would serve you well in the long term, regardless of which way you do it. In my example above I have used a shorter approach again.
-
In your
Initial()
method you initialise the majority of your variables, apart fromcheckStart
andcheckFinish
which are being initialised at the top of the class.I would perhaps declare them at the top, and then set them in your
Initial()
method.
Finally, and it really doesn’t make a lot of difference for this game, especially as you re-use some of this game later in the course anyway, but I would perhaps also consider separating the display from the business.
As an example, the Initial()
method is primarily responsible for initialising the variables for the game, but it also has a chunk of code in there to display a welcome message to the player. The code for the welcome message could be stripped out and placed into it’s own method, perhaps called DisplayWelcome()
for example.
By doing this you will increase the number of methods you have, but each will be responsible for very specific, distinct tasks. From an extensibility perspective, you could potentially move the display methods to their own class later, a class which is then purely responsible for handling the display based on the state of the game. This approach would enable you to update, easily, various pieces of functionality within the game.
I hope the above is of some use. I’ve typed it all up here and it is untested, so if you want to give it a try, please make sure you have a backup of your own code first. The above is just for consideration, by no means do you have to change anything for this fairly basic game.
Again, well done on extending the original game and introducing the concept of state within it. Your code was easy to read and follow which goes a long way when working with other developers
Aww I wasnt expecting this quick response. Thank you so much. Really appreciated and I will take into consideration your suggestions for my further coding. With this quick and fast response and quality of the videos, my thoughts has been consolidated in a very positive way
Cheers
You are more than welcome
I gave it a go and with 1-2 changes tadaa here is the latest tested working version. https://pastebin.com/Aacz3iTr Now i will go watch the course from where i left.
Nicely done, very clean, easy to read and follow