So I decided to up a pressure a bit by limiting the input attempts. My code works fine but I’d like to prevent further input after the game is over. Any ideas on how I would go about this please?
using UnityEngine;
public class Hacker : MonoBehaviour
{
// Game Configuration Data
const string menuHint = "Type menu at any time to return.";
string[] level1Passwords = { "beer", "wine", "chips", "pint" };
string[] level2Passwords = { "hospital", "bovine", "feline", "animals" };
string[] level3Passwords = { "extreme", "hardcore", "government", "investigate" };
// Game State
int level;
int lives;
string password;
enum Screen { MainMenu, Password, Win, Lose };
Screen currentScreen;
// Use this for initialization
void Start ()
{
ShowMainMenu ();
}
void ShowMainMenu ()
{
currentScreen = Screen.MainMenu;
lives = 3;
Terminal.ClearScreen();
Terminal.WriteLine("What system would you like to hack?");
Terminal.WriteLine("");
Terminal.WriteLine("1. The pub");
Terminal.WriteLine("2. The vet clinic");
Terminal.WriteLine("3. The FBI Mainframe");
Terminal.WriteLine("");
Terminal.WriteLine("Select a number: ");
}
void OnUserInput(string input)
{
if (input == "menu")
{
ShowMainMenu();
}
else if (currentScreen == Screen.MainMenu)
{
RunMainMenu(input);
}
else if (currentScreen == Screen.Password)
{
RunPassword(input);
}
else if (currentScreen == Screen.Lose)
{
RunLose();
}
else if (currentScreen == Screen.Win)
{
RunWin();
}
}
void RunMainMenu(string input)
{
bool isValidLevelNumber = (input == "1" || input == "2" || input == "3");
if (isValidLevelNumber)
{
level = int.Parse(input);
AskForPassword();
}
else
{
Terminal.WriteLine("Invalid Input!");
Terminal.WriteLine(menuHint);
}
}
void RunPassword(string input)
{
if (input == password)
{
RunWin();
}
else
{
lives = lives - 1;
if (lives == 0)
{
RunLose();
}
else
{
AskForPassword();
}
}
}
void AskForPassword()
{
currentScreen = Screen.Password;
Terminal.ClearScreen();
Terminal.WriteLine("You have " + lives + " tries remaining!!");
SetRandomPassword();
Terminal.WriteLine("Enter your password, hint: " + password.Anagram());
Terminal.WriteLine(menuHint);
}
void SetRandomPassword()
{
switch (level)
{
case 1:
password = level1Passwords[Random.Range(0, level1Passwords.Length)];
break;
case 2:
password = level2Passwords[Random.Range(0, level2Passwords.Length)];
break;
case 3:
password = level3Passwords[Random.Range(0, level3Passwords.Length)];
break;
default:
Debug.LogError("Invalid level number");
break;
}
}
void RunLose()
{
currentScreen = Screen.Lose;
Terminal.ClearScreen();
Terminal.WriteLine(@"
__ __
/ _ _ _ _ / ) _ _ ||
(__)(///)(- (__/ \/(-/ ..
");
}
void RunWin()
{
currentScreen = Screen.Win;
Terminal.ClearScreen();
ShowLevelReward();
Terminal.WriteLine(menuHint);
}
void ShowLevelReward()
{
switch (level)
{
case 1:
Terminal.WriteLine("Cheers!");
Terminal.WriteLine(@"
.~~~~.
i====i_
|cccc|_)
|cccc|
`-==-'
");
break;
case 2:
Terminal.WriteLine("Meow!");
Terminal.WriteLine(@"
/\_/\
( o.o )
> ^ <
");
break;
case 3:
Terminal.WriteLine("Welcome to the FB Aight!");
Terminal.WriteLine(@"
__ _ _
/ _| | (_)
| |_| |__ _
| _| '_ \| |
| | | |_) | |
|_| |_.__/|_|
");
break;
default:
Debug.LogError("Invalid level reward");
break;
}
}
}