My try

Here is my try.
I started quite messy, than had an individual function for each level, before finding a more global solution.

public class Hacker : MonoBehaviour
{
    string level; // game state
    string Password;
    enum Screen {MainMenu, Password, Win};
    Screen currentScreen;

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

    void ShowMainMenu(string greeting)
    {
        currentScreen = Screen.MainMenu;
        Terminal.ClearScreen();
        Terminal.WriteLine(greeting);
        Terminal.WriteLine("Do you like to play a game with me?");
        Terminal.WriteLine("It is a word puzzle that will teach you how to hack into passwords.");
        Terminal.WriteLine("Are you interested? Then ...");
        Terminal.WriteLine("");
        Terminal.WriteLine("Press 1 for an easy game");
        Terminal.WriteLine("Press 2 for a medium game");
        Terminal.WriteLine("Press 3 for a difficult game");
        Terminal.WriteLine("");
        Terminal.WriteLine("Enter your selection in the next line.");
    }

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

    void RunMainMenu(string input)
    {
        if (input == "1")
        {
            level = "easy";
            RunGame();
        }
        else if (input == "2")
        {
            level = "medium";
            RunGame();
        }
        else if (input == "3")
        {
            level = "difficult";
            RunGame();
        }
        else
        {
            Terminal.WriteLine("Sorry. I don't know what to do. Try again.");
        };
    }

    void RunGame()
    {
        currentScreen = Screen.Password;
        Terminal.WriteLine("Please enter your guess for the password:");
        if (level == "easy")
        {
            Password = "school";
        }
        else if (level == "medium")
        {
            Password = "America";
        }
        else if (level == "difficult")
        {
            Password = "unfortunately";
        }

    }

    void RunResult(string input)
    {
        currentScreen = Screen.Win;
        if (input == Password)
        {
            Terminal.WriteLine("Super. This is exactly right.");
            Terminal.WriteLine("Do you want another game? Yes/No");
        }
        else
        {
            Terminal.WriteLine("Sorry, this isn't correct. Please try again.");
            RunGame();
        };    
    }
    void RunAgain(string input)
    {
        if (input == "Yes")
        {
            ShowMainMenu("");
        }
        else if (input == "No")
        {
            Terminal.ClearScreen();
            Terminal.WriteLine("You can stop the Terminal now.");
        }
        else
        {
            ShowMainMenu("Sorry, I don't understand. Therefore I start the menu again.");
        };
    }
}

Great solution!

I just want to give a little feedback: Try making the names of your methods as explicit as possible.

Currently you have two methods with really confusing names such as RunMainMenu and RunAgain, you already have a ShowMainMenu which is basically the same as RunMainMenu in terms of name but in reality in RunMainMenu you are not running the main menu, you are setting the state of your game, so a better name would be SetGameState or SetPasswordDifficulty.

Your RunAgain method has the same problem, I don’t know what you are running again.

Try to name your methods as you would write a childrens’ book, make it as clear as possible.

Maybe this is advanced stuff, but I think is something all coders should practice right from the start so it becomes second nature asap.

thanks for the feedback.

I changed them now to
RunWhatLevel
RunNextGame

1 Like

Privacy & Terms