Hi everyone, first time posting here.
I finished the first part of the course the hacker game.
I mainly focused on coding, so the words etc aren’t that difficult.
You can only access the third level if you’ve passed both the first and second.
Here is the code and please tell me what I can improve, leave out or what might be a better way of implementing it:
using UnityEngine;
public class Hacker : MonoBehaviour
{
// Game configuration data
const string menuHint = "You may type menu at any time.";
string[] level1Passwords = { "books", "shelf", "cover", "paper" };
string[] level2Passwords = { "siren", "badge", "cell", "officer" };
string[] level3Passwords = { "templars", "assassins", "agent" };
// Gamestate
bool level1Cleared = false;
bool level2Cleared = false;
bool levelsCleared = false;
bool isValidLevelNumber = false;
int level;
enum Screen { MainMenu, Password, Win };
Screen currentScreen;
string password;
string input;
// Start is called before the first frame update
void Start()
{
ShowMainMenu();
}
void ShowMainMenu()
{
currentScreen = Screen.MainMenu;
Terminal.ClearScreen();
Terminal.WriteLine("What would you like to hack into?");
Terminal.WriteLine(" ");
Terminal.WriteLine("Press 1 for the local library");
Terminal.WriteLine("Press 2 for the police station");
WriteLevel3();
Terminal.WriteLine(" ");
Terminal.WriteLine("Enter your selection:");
}
//private void update()
//{
// int index = random.range(0, level1passwords.length);
// print(index);
//}
void OnUserInput(string input)
{
if (input.Contains("menu"))
{
ShowMainMenu();
}
else if (currentScreen == Screen.MainMenu)
{
RunMainMenu(input);
}
else if (currentScreen == Screen.Password)
{
CheckPassword(input);
}
}
void CheckClearance()
{
if (level1Cleared == true && level2Cleared == true)
{
levelsCleared = true;
}
}
void WriteLevel3() {
if (levelsCleared)
{
Terminal.WriteLine("Press 3 for Abstergo");
}
}
void RunMainMenu(string input)
{
if (isValidLevelNumber = (input == "1" || input == "2" || input == "3" && levelsCleared))
{
level = int.Parse(input);
AskForPassword();
}
else if (input.Contains("007")) // easter egg
{
Terminal.WriteLine("Welcome Mister Bond");
}
else
{
Terminal.WriteLine(input + " is not an option");
Terminal.WriteLine(menuHint);
}
}
void AskForPassword()
{
//print(level1Passwords.Length);
//print(level2Passwords.Length);
currentScreen = Screen.Password;
Terminal.ClearScreen();
SetRandomPassword();
Terminal.WriteLine("You have chosen level " + level);
Terminal.WriteLine(menuHint);
Terminal.WriteLine("Enter your password, hint: " + password.Anagram());
}
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 CheckPassword(string input)
{
if (input == password)
DisplayWinScreen();
else
{
Terminal.WriteLine("Invalid password");
}
}
void DisplayWinScreen()
{
currentScreen = Screen.Win;
Terminal.ClearScreen();
ShowLevelReward();
}
void ShowLevelReward()
{
switch (level)
{
case 1:
Terminal.WriteLine("Have a book...");
Terminal.WriteLine(@"
______
/ /|
/ //
/_____//
(_____(/
");
Terminal.WriteLine(menuHint);
level1Cleared = true;
CheckClearance();
break;
case 2:
Terminal.WriteLine("Have 2 books..., prison key is inside, shhhhh");
Terminal.WriteLine(@"
_____
/ /|
/ //|
/_____///
(_____(//
(_____(/
"
);
Terminal.WriteLine(menuHint);
level2Cleared = true;
CheckClearance();
break;
case 3:
Terminal.WriteLine("What have you done?");
Terminal.WriteLine(@"
/\
//\\
// \\
// \\
.__// \\__.
\ / \ /
/ | | \
'-__\_ _/__-'
"
);
Terminal.WriteLine(menuHint);
break;
default:
Debug.LogError("error");
break;
}
}
}