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.");
};
}
}