My first solo S02 Terminal Hacker attempt results

This was a challenging task, particularly for a relative new comer to programming. I’ve gone through a few courses on VB.NET and Python, so was able to bring back some of the concepts from those classes, which is why I chose to use an array. However, I’m sure this code has some clean up to be done. I’m looking forward to continuing the lecture to see how Ben solved the problem. I think it will take some time of working through coding challenges to start thinking of the best path to take. I did find myself dreaming about solution options, which for me is a good thing, and tells me that my brain is challenged by the problems and wants to solve them.

Here is my code (before watching Ben’s solution). :slight_smile: I’m pleased it didn’t throw any errors. It didn’t account for actions after the user wins, but I think the purpose was to successfully present password attempts and respond at a match.


public class Hacker : MonoBehaviour {

    //game state
    int level;
    string lvlGreeting;
    string lvl1Encrypt;
    string lvl1Answer;
    string lvl2Encrypt;
    string lvl2Answer;

    enum Screen { MainMenu, Password, Win };
    Screen currentScreen;

    string[,] passwordArraylvl1 = new string[,] { { "arlybri", "library" }, { "enifmist", "feminist" }, { "agenv", "vegan" }, {"adornedot", "deodorant" } };
    string[,] passwordArraylvl2 = new string[,] { { "aimilncr", "criminal" }, { "odtun", "donut" }, { "ferritingnp", "fingerprint" }, { "chadsnuff", "handcuffs" } };
    int randomNum;

    //Executes once at beginning
    void Start()
    {
        ShowMainMenu();
    }

    void ShowMainMenu()
    {
        Terminal.ClearScreen();
        level = 0;
        currentScreen = Screen.MainMenu;
        Terminal.WriteLine("Welcome " + Environment.UserName + "!");
        Terminal.WriteLine("What would you like to hack into?");
        Terminal.WriteLine("Press 1 for the local library");
        Terminal.WriteLine("Press 2 for the police department");
        Terminal.WriteLine("Type 'Menu' to return to the main menu");
    }

    //this should only decide how to handle user input, not actually do it
    void OnUserInput(string input)
    {
        if (input == "menu")
        {
            ShowMainMenu();
        }
        else if (currentScreen == Screen.MainMenu)
        {
            Terminal.ClearScreen();
            RunMainMenu(input);
        }
        else if (currentScreen == Screen.Password)
        {
            if (level == 1)
            {
                Trylvl1(input);
            }
            else if (level == 2)
            {
                Trylvl2(input);
            }
        }
 
    }

    void RunMainMenu(string input)
    {
        if (input == "1")
        {
            level = 1;
            lvlGreeting = ". You have selected to hack the library. Foolish mortal...";
            StartGame();
        }
        else if (input == "2")
        {
            level = 2;
            lvlGreeting = ". You have selected to hack the PD. Limited resistance expected.";
            StartGame();
        }
        else
        {
            Terminal.WriteLine("Please make a valid selection");
        }
    }

    void StartGame()
    {
        currentScreen = Screen.Password;
        Terminal.WriteLine("You have chosen level " + level + lvlGreeting);
        if (level == 1)
        {
            randomNum = UnityEngine.Random.Range(0, 4);
            lvl1Encrypt = passwordArraylvl1[randomNum, 0];
            lvl1Answer = passwordArraylvl1[randomNum, 1];
            Terminal.WriteLine("Encrypted password = " + lvl1Encrypt);
            Terminal.WriteLine("Hack the password.");

        }
        else if (level == 2)
        {
            randomNum = UnityEngine.Random.Range(0, 4);
            lvl2Encrypt = passwordArraylvl2[randomNum, 0];
            lvl2Answer = passwordArraylvl2[randomNum, 1];
            Terminal.WriteLine("Encrypted password = " + lvl2Encrypt);
            Terminal.WriteLine("Hack the password.");
        }
      
    }
    void Trylvl1(string input)
    {
        if (input == lvl1Answer)
        {
            currentScreen = Screen.Win;
            Terminal.WriteLine("You Win!!");
        }
        else
        {
            Terminal.WriteLine("Wrong! The sushi is spoiled! Try AGAIN!");
        }
    }

    void Trylvl2(string input)
    {
        if (input == lvl2Answer)
        {
            currentScreen = Screen.Win;
            Terminal.WriteLine("You Win!!");
        }
        else
        {
            Terminal.WriteLine("Wrong! The sushi is spoiled! Try AGAIN!");
        }
    }
}

Privacy & Terms