Took me a while to sort out what was going on and the logic. Basically I used enum screen states to sort what OnUserInput() should handle. I ended up using pen and paper (my preferred method) to sort out what each function should only handle. My first attempt had stuff happening all over the place. Took a while to fix the mess.
After watching the rest of the lesson, I would have also used the variable password as a game state. This would have simplified my code.
My code is included below in case someone may find it helpful.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hacker: MonoBehaviour
{
// Game state
enum Screen { MainMenu, WaitingForPassword, Win }
Screen CurrentScreen;
string agentName;
int level = 0;
void Start()
{
MainMenuHacker(agentName);
}
void MainMenuHacker(string agentName)
{
CurrentScreen = Screen.MainMenu;
Terminal.ClearScreen();
Terminal.WriteLine("Agent " + agentName );
Terminal.WriteLine("");
Terminal.WriteLine("You have been given 2 targets");
Terminal.WriteLine("");
Terminal.WriteLine("1 Twitter");
Terminal.WriteLine("2 Google");
Terminal.WriteLine("");
Terminal.WriteLine("Make your SELECTION and initiate");
Terminal.WriteLine("your attack");
Terminal.WriteLine("");
}
void RunMainMenu(string input)
{
if (input == "1")
{ MainMenuHacker(agentName);
level = 1;
StartGame();
CurrentScreen = Screen.WaitingForPassword;
}
else if (input == "2")
{ MainMenuHacker(agentName);
level = 2;
StartGame();
CurrentScreen = Screen.WaitingForPassword;
}
else if (input == "007")
{
agentName = "Bond";
MainMenuHacker(agentName);
}
else
{
MainMenuHacker(agentName);
Terminal.WriteLine("Please make a valid choice");
level = 0;
}
}
void OnUserInput(string input)
{
if ((input == "menu") | (input == "Menu"))
{
CurrentScreen = Screen.MainMenu;
MainMenuHacker(agentName);
}
else if (CurrentScreen == Screen.MainMenu)
{
RunMainMenu(input);
}
else if (CurrentScreen == Screen.WaitingForPassword)
{
AskPassWord(input);
}
}
void StartGame()
{
CurrentScreen = Screen.MainMenu;
if (level == 1)
{
Terminal.ClearScreen();
Terminal.WriteLine("You chose option " + level + ": Twitter");
Terminal.WriteLine("");
Terminal.WriteLine("Please enter the password");
CurrentScreen = Screen.WaitingForPassword;
}
if (level == 2)
{
Terminal.ClearScreen();
Terminal.WriteLine("You chose option " + level + ": Google");
Terminal.WriteLine("");
Terminal.WriteLine("Please enter the password");
CurrentScreen = Screen.WaitingForPassword;
}
}
void AskPassWord(string input)
{
string passwordEasy = "dog";
string passwordHard = "monkey";
if (level == 1)
{
if (passwordEasy == input)
{
Terminal.WriteLine("Password is a match");
CurrentScreen = Screen.Win;
}
else if (passwordEasy != input)
{
Terminal.WriteLine("Incorrect, try again");
}
}
else if (level == 2)
{
if (passwordHard == input)
{
Terminal.WriteLine("Password is a match");
CurrentScreen = Screen.Win;
}
else if (passwordHard != input)
{
Terminal.WriteLine("Incorrect, try again");
}
}
}
}