Practicing robust coding practices

This challenge was a little intimidating at first, but also the most exciting so far. This course is my first venture into coding outside of some basic HTML I did almost 20 years ago. My solution required me to talk with a more experienced friend first to really make sure I grasped the concept of using arguments when a method/function is called. i.e.


(Hid his name for privacy reasons. The marked out names are identical."

Once he helped me realize HOW and WHY this works I was able to jump into action. Here is what I put together in the end:

public class Hacker : MonoBehaviour
{
    // Game state
    int level;
    enum Screen {MainMenu, PassEntry, Victory}
    Screen currentScreen;
    string actualPass = null;
    string location = null;

    // Start is called before the first frame update
    void Start()
    {
        ShowMainMenu("Hello, User.");
    }

    void ShowMainMenu(string greeting)
    {
        currentScreen = Screen.MainMenu;
        Terminal.WriteLine(greeting);
        Terminal.WriteLine("What would you like to hack into today?");
        Terminal.WriteLine("Press 1 for the restaurant. (Easy)");
        Terminal.WriteLine("Press 2 for the school. (Medium)");
        Terminal.WriteLine("Press 3 for the bank. (Difficult)");
        Terminal.WriteLine(" ");
        Terminal.WriteLine("Please enter your selection:");
    }

    void OnUserInput(string input)
    {
        if (input == "menu")
        {
            Terminal.ClearScreen();
            ShowMainMenu("Hello again, User.");
        }
        else if (currentScreen == Screen.MainMenu)
        {
            RunMainMenu(input);
        }
        // sendoff for password verification
        else if (currentScreen == Screen.PassEntry)
        {
            VerifyPass(input);
        }
        // intended to always return to main menu from victory screen regardless of input
        else if (currentScreen == Screen.Victory)
        {
                ShowMainMenu("Welcome back.");
        }
    }

    void RunMainMenu(string input)
    {

        if (input == "1")
        {
            level = 1;
            StartGame();
        }
        else if (input == "2")
        {
            level = 2;
            StartGame();
        }
        else if (input == "3")
        {
            level = 3;
            StartGame();
        }
        // Easter egg
        else if (input == "joan")
        {
            Terminal.WriteLine("Hi Mom!");
        }
        else
        {
            Terminal.WriteLine("Please enter a valid selection.");
        }
    }

    // actual password is defined here depending on level
    void StartGame()
    {
        currentScreen = Screen.PassEntry;
        if (level == 1)
        {
            actualPass = "diner";
            location = "restaurant";
        }
        else if (level == 2)
        {
            actualPass = "locker";
            location = "school";
        }
        else if (level == 3)
        {
            actualPass = "withdrawal";
            location = "bank";
        }
        else
        {
            Debug.Log("CRITICAL ERROR. I really don't know how this happened.");
            Debug.Log("Level not set to 1, 2, or 3.");
        }
        Terminal.ClearScreen();
        Terminal.WriteLine("You have selected the " + location + ".");
        Terminal.WriteLine("To return to the previous screen, type 'menu.'");
        Terminal.WriteLine("Please enter password: ");
        // This output for testing and verification purposes only
        Debug.Log("Current ActualPass is " + actualPass);
    }

    //guess is verified against actual password
    private void VerifyPass(string input)
    {
        if (input == actualPass)
        {
            Victory(location);
        }
        else
        {
            Terminal.WriteLine("Incorrect. Please enter the correct password: ");
        }
    }

    void Victory(string location)
    {
        currentScreen = Screen.Victory;
        Terminal.ClearScreen();
        Terminal.WriteLine("Congratulations! You have successfully hacked into the " + location + ".");
        Terminal.WriteLine(""); 
        Terminal.WriteLine("Press Enter key to continue...");
    }
}

Problems with my solution:
Originally the lines within the VerifyPass method were nested within the OnUserInput method in the Screen. PassEntry section. I decided to extract these lines to their own function for the purposes of making more robust and readable code for future editing. I did not think to do this until I saw Ben’s example in the video.

Merits with my solution:
I implemented a Victory screen way ahead of schedule. I assumed this was part of the challenge and it was actually the first thing I wrote. I made the destination, and then built the road leading to it.

My VerifyPass method is extremely simple, as I am using a newly-implemented member variable called ‘actualPass’ which is defined after the player selects a level.

For practice in troubleshooting my own code later, I implemented debug messages which I was able to use to verify that the password was being correctly set. I hope that these will continue to be useful after randomized answers are implemented in a later module.

Thanks for reading. Hopefully this helps someone out there, or at least demonstrates a different approach to the same problem. Please share any feedback you have about how my code could be further improved!

Cheers

Privacy & Terms