Terminal Hacker using Lists

Hi There,

Here is my way of writing the terminal hacker to this point.
-I have 1 method to handle the input for all levels, rather than one per level.

  • I dynamically set the random password with a method that just takes the level number you want.
  • Easy to scale this version for many more levels.
public class Hacker : MonoBehaviour {

    //Game config data

    // Game State
    public System.Random rnd = new System.Random();
    string systemPassword;
    int lvl;

    enum Screen
    {
        MainMenu,
        Level1,
        Level2,
        Level3,
        Win
    };

    Screen currentScreen = Screen.MainMenu;

    // Use this for initialization
    void Start () {
        ShowMainMenu();
    }

    void ShowMainMenu()
    {
        currentScreen = Screen.MainMenu;
        Terminal.WriteLine("What would you like to hack into?");
        Terminal.WriteLine("Press 1 for the local library");
        Terminal.WriteLine("Press 2 for the police station");
        Terminal.WriteLine("Enter your selection");
        Terminal.WriteLine(" ");
    }

    

    void OnUserInput(string input)
    {
        if (input == "menu")
        {
            ShowMainMenu();
        }
        else if (currentScreen == Screen.MainMenu)
        {
            RunMainMenu(input);
        }
        else if (currentScreen == Screen.Level1 || currentScreen == Screen.Level2)
        {
            HandleLevel(input, lvl);
        }
    }


    private string SetSecretPassword(int lvl)
    {
        List<string> passwords = new List<string>();

        switch(lvl)
        {
            case 1:
                passwords = new List<string>() { "books", "aisle", "self", "donkey", "font", "borrow" };
                break;
            case 2:
                passwords = new List<string>() { "cops", "robber", "crankoldpeople", "zeegermans" };
                break;
            default:
                break;

        }

        int rndChoice = rnd.Next(1, passwords.Count);
        string pw = passwords[rndChoice];
        return pw;       

    }

    private void HandleLevel(string input, int lvl)
    {

        if (input == systemPassword)
        {
            Terminal.WriteLine("You Got it! Hurray!, You've beaten level " + lvl + "!!!");
        }
        else if (input == "q")
        {
            ShowMainMenu();
        }
        else
        {
            Terminal.WriteLine("Sorry, try again, or press Q to go to the main menu");
        }
    }


    private void RunMainMenu(string input)
    {
        switch (input)
        {
            case "1":
                currentScreen = Screen.Level1;
                systemPassword = SetSecretPassword(1);
                lvl = 1;
                Terminal.WriteLine(currentScreen.ToString());
                Terminal.WriteLine("you chose level 1, press 'Q' to go back");
                break;
            case "2":
                systemPassword = SetSecretPassword(2);
                lvl = 2;
                currentScreen = Screen.Level2;
                Terminal.WriteLine("you chose level 2, press 'Q' to go back");
                break;
            case "3":
                currentScreen = Screen.Level3;
                Terminal.WriteLine("you chose level " + input);
                break;
            case "menu":
                Terminal.ClearScreen();
                currentScreen = Screen.MainMenu;
                Terminal.WriteLine("Accessing Main Menu...");
                ShowMainMenu();
                break;
            case "dir":
                Terminal.WriteLine("your choices are: 1,2, or 3");
                break;
            case "clr":
                Terminal.ClearScreen();
                break;
            default:
                Terminal.WriteLine(input + " is an invalid choice");
                break;
        }
            
    }
}

Privacy & Terms