My first solo

Let me know your comments - what are the improvements I could introduce in this code. This asks for a level and then a passwords and congratulates if they match.

public class Hacker : MonoBehaviour //Class
{
// Start is called before the first frame update
// Game state - use global variables to hold state of the game but do not abuse and overuse
int level;
enum Screen { MainMenu, Password, Win }; // all of these are the only possible values for enum Screen
Screen currentScreen; // defining enum screen with a value of ‘MainMenu’
string passwordOne = “pass1”;
string passwordTwo = “pass2”;

//Initialization
void Start() //Function/Method
{
    ShowMainMenu();
}

void ShowMainMenu()
{
    currentScreen = Screen.MainMenu;
    Terminal.ClearScreen();
    Terminal.WriteLine("");
    Terminal.WriteLine("The objective of this program is");
    Terminal.WriteLine("to hack into one of the following:");
    Terminal.WriteLine("");
    Terminal.WriteLine("Press 1 for the library");
    Terminal.WriteLine("Press 2 for the police station");
    Terminal.WriteLine("Press 3 for the NASA");
    Terminal.WriteLine("");
    Terminal.WriteLine("Enter your selection: ");
}
void OnUserInput(string input)
{
    if (input == "menu")
    {
        ShowMainMenu();
    }
    else if (currentScreen == Screen.MainMenu)
    {
        RunMainMenu(input);
    }
    else if (currentScreen == Screen.Password)
    {
        
        PasswordCheck(input);
    }
}

private void PasswordCheck(string input)
{
    
    if (level == 1)
    {
        if (input == passwordOne)
        {
            Terminal.WriteLine("Correct password");
            StartGame();
        }
        else if (input != passwordOne)
        {
            Terminal.WriteLine("Incorrect password, try again");
        }
    }
    else if (level == 2)
    {
        if (input == passwordTwo)
        {
            Terminal.WriteLine("Correct password");
            StartGame();
        }
        else if (input != passwordTwo)
        {
            Terminal.WriteLine("Incorrect password, try again");
        }
    }
}

void RunMainMenu(string input)
{
    if (input == "1")
    {
        level = 1;
        Terminal.WriteLine("You have chosen level " + level);
        currentScreen = Screen.Password;
        
    }
    else if (input == "2")
    {
        level = 2;
        Terminal.WriteLine("You have chosen level " + level);
        currentScreen = Screen.Password;
        
    }
    else
    {
        Terminal.WriteLine("ERROR - Please choose a valid level");
    }
    Terminal.WriteLine("Please provide password :");
}

void StartGame()
{
    Terminal.WriteLine("Congratulations! You have chosen your level and typed in correct password");
}

Disregard in code comments - some are outdated.

Hi there!
As for me first time i made separete function wich was assigning password by checking lvl inputed, but it was abit messy and big , so i decided to mannually assign password same as in video where we assigning LVL, made it much smaller and i got this :slight_smile:

void CheckPass(string input,string pass)
{

    if (input == pass)
    {
        Terminal.WriteLine("Congrats !Password accepted !");
        currentScreen = Screen.Win;
    }
    else
        Terminal.WriteLine("try again!");
}
1 Like

Privacy & Terms