My Solo Challenge , before video review!

Hi wanted to share the code that I came up to make the Solo Challenge requisite work.

public class Hacker : MonoBehaviour
{
    // Game State
    int level;
    enum Screen { MainMenu, Password, Win };
    Screen currentScreen;
    string password;

    // Start is called before the first frame update
    void Start()
    {
        ShowMainMenu();
    }

    void ShowMainMenu() {
        currentScreen = Screen.MainMenu;
        Terminal.ClearScreen();
        var greeting = "Hello Leandro";
        Terminal.WriteLine(greeting);
        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:");
    }

    void OnUserInput(string input)
    {
        if (input == "menu")
        {
            ShowMainMenu();
        }
        else if (currentScreen == Screen.MainMenu)
        {
            RunMainMenu(input);
        }
        else if (currentScreen == Screen.Password)
        {
            checkPassword(input);
        }

    }

    void RunMainMenu(string input)
    {
        if (input == "1")
        {
            level = 1;
            password = "book";
            StartGame();
        }
        else if (input == "2")
        {
            level = 2;
            password = "jail";
            StartGame();
        }
        else if (input == "hola")
        {
            Terminal.WriteLine("Hola, gusto en conocerte!");
        }
        else if (input == "chau")
        {
            Terminal.WriteLine("No te vayas!");
        }
        else
        {
            Terminal.WriteLine("No entiendo");
        }
    }


    void StartGame()
    {
        Terminal.WriteLine("Write the Password:");
        currentScreen = Screen.Password;
    }

    void checkPassword(string input)
    {

        if (level == 1)
        {
            if (password == input)
            {
                WinGame();
            }
            else
            {
                Terminal.WriteLine("Please try again!");
            }
        }
        else if (level == 2)
        {
            if (password == input)
            {
                WinGame();
            }
            else
            {
                Terminal.WriteLine("Please try again!");
            }
        }
    }

    void WinGame()
    {
        Terminal.ClearScreen();
        Terminal.WriteLine("You just nailed it");
        Terminal.WriteLine("Well done! type menu to restart!");
        currentScreen = Screen.Win;

    }
}

I think it works as expected but not sure if the video suggested some other way to make it work. Probably refactoring some things would make it easier, not sure.

1 Like

Anytime you do the solo challenge and you get something that works than I give you all the people! That’s how you get to where you want to go in life.

Privacy & Terms