I followed the tutorial but also tried to implement my own features on the go.
I’m bad with titles so I’ll let my code do the explaining (I commented the stuff I need help with):
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class Hacker : MonoBehaviour
{
//code will be mader cleaner soon (once i figure out how to implement all the features)
string[] level1Passwords = { "book", "books", "student", "students", "learning", "teacher", "teachers", "degree", "degrees", "learn", "class", "classes" };
string[] level2Passwords = { "money", "banker", "bankers", "power", "suit", "suits", "paper", "debt", "loan", "safe", "cash", "evil", "credit" };
string[] level3Passwords = { "conspiracy", "secret", "secrets", "service", "services", "surveillance", "mission", "missions", "crime", "villain", "alien", "aliens", };
string[] level1Gamewords = { "knowledge", "master", "professor" };
string[] level2Gamewords = { "society", "credit card", "commercial" };
string[] level3Gamewords = { "extraterrestial", "secret service", "conspiracy theory" };
int level;
float currentCountdownVal;
int tryCounter = 0;
string password;
//almost the same concept as the password but I want gameword1 to always be 'knowledge' if case of level 1.
//I want the game to have 2 stages: first you have to figure out the password, then you have to figure out the gamewords.
//Only the gamewords will use the anagram function. So for example: you get the password right -> the real level starts ->
//You now have limited time to figure out the 3 gamewords (take a look at the enumerator StartLevelExecution if you want to get an idea) which, like I said,
//will use the anagram function to "displace" the letters -> display win screen, if you run out of time display game over screen and wait for user input to restart
//or exit the game (how do I do that?).
string gameword1;
string gameword2;
string gameword3;
enum Screen { MainMenu, Password, /*Level,*/ Win, GameOver }
Screen currentScreen;
void Start()
{
ShowMainMenu();
}
void ShowMainMenu()
{
currentScreen = Screen.MainMenu;
Terminal.WriteLine("What would you like to hack?\n");
StartCoroutine(MainMenuExecution(1.0f));
}
void Update()
{
}
IEnumerator MainMenuExecution(float time)
{
yield return new WaitForSeconds(time);
Terminal.WriteLine("Enter 'university',\nto hack into the local university\n");
yield return new WaitForSeconds(time);
Terminal.WriteLine("Enter 'national bank',\nto hack into the national bank\n");
yield return new WaitForSeconds(time);
Terminal.WriteLine("Enter 'Federal Bureau of Intelligence',\nto hack into the FBI\n");
yield return new WaitForSeconds(time);
Terminal.WriteLine("Enter your selection:");
}
void OnUserInput(string input)
{
if (input == "menu")
{
Terminal.ClearScreen();
ShowMainMenu();
}
else if (currentScreen == Screen.MainMenu)
{
RunMainMenu(input);
}
else if (currentScreen == Screen.Password)
{
CheckPassword(input);
}
//else if (currentScreen == Screen.Level)
//{
// Level(input);
//}
}
void RunMainMenu(string input)
{
switch (input)
{
case "university":
level = 1;
SelectingPassword();
break;
case "national bank":
level = 2;
SelectingPassword();
break;
case "Federal Bureau of Intelligence":
case "federal bureau of intelligence":
level = 3;
SelectingPassword();
break;
case "your selection":
Terminal.WriteLine("\nHA. You're really funny.");
break;
default:
Terminal.WriteLine("\nNot a valid input. Please try again:");
break;
}
}
void SelectingPassword()
{
currentScreen = Screen.Password;
Terminal.ClearScreen();
switch (level)
{
case 1:
password = level1Passwords[Random.Range(0, level1Passwords.Length)];
Terminal.WriteLine("You chose the university. This will be easy.\n");
Terminal.WriteLine("*Selecting random password*\n(Hint: Words that fit to university.)");
Terminal.WriteLine("Please enter the password: ");
break;
case 2:
password = level2Passwords[Random.Range(0, level2Passwords.Length)];
Terminal.WriteLine("This bank is pretty secure. Good Luck.\n");
Terminal.WriteLine("*Selecting random password*\n(Hint: Words that fit to national bank.)");
Terminal.WriteLine("Please enter the password: ");
break;
case 3:
password = level3Passwords[Random.Range(0, level3Passwords.Length)];
Terminal.WriteLine("The FBI is unhackable. Or is it?\n");
Terminal.WriteLine("*Selecting random password*\n(Hint: Words that fit to the FBI.)");
Terminal.WriteLine("Please enter the password: ");
break;
default:
Terminal.WriteLine("No level selected. How did this happen?");
Terminal.WriteLine("Restarting game...");
ShowMainMenu();
break;
}
}
void CheckPassword(string input)
{
if (input == password)
{
Terminal.WriteLine("Starting level..");
switch (level)
{
case 1:
//StartCoroutine(StartLevelExecution(120));
break;
case 2:
//StartCoroutine(StartLevelExecution(90));
break;
case 3:
//StartCoroutine(StartLevelExecution(60));
break;
default:
Terminal.WriteLine("No level selected. How did this happen?");
Terminal.WriteLine("Restarting game...");
ShowMainMenu();
break;
}
}
else
{
Terminal.WriteLine("Sorry wrong password. Try again:");
tryCounter++;
if (tryCounter == 5)
{
Terminal.WriteLine("You already took " + tryCounter + " tries.");
}
else if (tryCounter >= 5 && tryCounter <= 10)
{
switch (level)
{
case 1:
Terminal.WriteLine("Hint: Words that fit to university");
break;
case 2:
Terminal.WriteLine("Hint: Words that fit to national bank");
break;
case 3:
Terminal.WriteLine("Hint: Words that fit to the FBI");
break;
default:
Terminal.WriteLine("No level selected. How did this happen?");
Terminal.WriteLine("Restarting game...");
ShowMainMenu();
break;
}
if (tryCounter == 10)
{
ShowPasswordHelp();
}
}
}
}
void ShowPasswordHelp()
{
Terminal.ClearScreen();
Terminal.WriteLine("You already took 10 tries.");
Terminal.WriteLine("Here is some help for you.");
switch (level)
{
case 1:
Terminal.WriteLine("Words that fit:");
foreach(string password in level1Passwords)
{
Terminal.WriteLine(password.ToString());
}
break;
case 2:
Terminal.WriteLine("Words that fit:");
foreach (string password in level2Passwords)
{
Terminal.WriteLine(password.ToString());
}
break;
case 3:
Terminal.WriteLine("Words that fit:");
foreach (string password in level3Passwords)
{
Terminal.WriteLine(password.ToString());
}
break;
default:
Terminal.WriteLine("No level selected. How did this happen?");
Terminal.WriteLine("Restarting game...");
ShowMainMenu();
break;
}
StartCoroutine(PasswordHelpDone(1.5f));
}
IEnumerator PasswordHelpDone(float time)
{
yield return new WaitForSeconds(time);
Terminal.WriteLine("\nThis screen won't be shown again. Memorize these words.");
yield return new WaitForSeconds(10.0f);
Terminal.ClearScreen();
yield return new WaitForSeconds(time);
Terminal.WriteLine("Now enter the password: ");
}
//how do I display the gamewords and then check for the right input?
//void Level(string input)
//{
// Terminal.ClearScreen();
// switch(level)
// {
// case 1:
// break;
// case 2:
// break;
// case 3:
// break;
// }
//}
//start level and start timer
public IEnumerator StartLevelExecution(float countdownVal)
{
//currentScreen = Screen.Level;
yield return new WaitForSeconds(2.0f);
Terminal.ClearScreen();
currentCountdownVal = countdownVal;
yield return new WaitForSeconds(1.5f);
Terminal.WriteLine("You have " + countdownVal + " seconds to guess the words.");
yield return new WaitForSeconds(1.0f);
Terminal.WriteLine("GO!");
while (currentCountdownVal > 0)
{
yield return new WaitForSeconds(1.0f);
currentCountdownVal--;
}
if (currentCountdownVal == 30)
{
Terminal.WriteLine("You have 30 seconds left.");
}
else if (currentCountdownVal == 0)
{
GameOver();
}
}
void DisplayWinScreen()
{
currentScreen = Screen.Win;
Terminal.ClearScreen();
ShowLevelReward();
}
//the ascii art is not from me btw
void ShowLevelReward()
{
Terminal.WriteLine("Well done!");
switch (level)
{
case 1:
Terminal.WriteLine("Have a book:");
Terminal.WriteLine(@"
__...--~~~~~-._ _.-~~~~~--...__
// `V' \\
// | \\
//__...--~~~~~~-._ | _.-~~~~~~--...__\\
//__.....----~~~~._\ | /_.~~~~----.....__\\
====================\\|//====================
`---`
");
break;
case 2:
Terminal.WriteLine("Have a dollar:");
Terminal.WriteLine(@"
___________________________________
|#######====================#######|
|#(1)*UNITED STATES OF AMERICA*(1)#|
|#** /===\ ******** **#|
|*# {G} | (~) | #*|
|#* ****** | /v\ | O N E *#|
|#(1) \===/ (1)#|
|##=========ONE DOLLAR===========##|
------------------------------------
");
break;
case 3:
Terminal.WriteLine("Have an FBI badge:");
Terminal.WriteLine(@"
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣀⣀⣀⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣀⣤⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣤⣀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠀⠀⠀⠀⠀
⠀⠀⠀⢠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡄⠀⠀⠀
⠀⠀⢠⣿⡿⠿⠿⠿⠿⠿⠿⠿⣿⠿⠿⠿⠿⠿⠿⢿⣿⣿⣿⠿⠿⢿⣿⡄⠀⠀
⠀⢀⣿⣿⡇⠀⠀⣠⣤⣄⣀⣠⣿⠀⠀⢀⣤⣀⡀⠀⠘⣿⣿⠀⠀⢸⣿⣿⡀⠀
⠀⢸⣿⣿⡇⠀⠀⣿⣿⣿⣿⣿⣿⠀⠀⢸⣿⣿⠟⠀⠀⣿⣿⠀⠀⢸⣿⣿⡇⠀
⠀⢸⣿⣿⡇⠀⠀⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⠀⠀⠀⠺⣿⣿⠀⠀⢸⣿⣿⡧⠀
⠀⢸⣿⣿⡇⠀⠀⣿⣿⣿⣿⣿⣿⠀⠀⢸⣿⣿⣿⠀⠀⢹⣿⠀⠀⢸⣿⣿⡇⠀
⠀⠈⣿⣿⡇⠀⠀⣿⣿⣿⣿⣿⣿⠀⠀⠈⠛⠛⠉⠀⢀⣾⣿⠀⠀⢸⣿⣿⠃⠀
⠀⠀⠸⣿⣷⣶⣶⣿⣿⣿⣿⣿⣿⣶⣶⣶⣶⣶⣶⣾⣿⣿⣿⣶⣶⣾⣿⠇⠀⠀
⠀⠀⠀⠘⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⠀⠀⠀
⠀⠀⠀⠀⠀⠛⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠁⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠙⠻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠋⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠙⠛⠛⠋⠉⠉⠀⠀⠀⠀⠀⠀
");
break;
default:
Terminal.WriteLine("No level selected. How did this happen?");
Terminal.WriteLine("Restarting game...");
ShowMainMenu();
break;
}
}
void GameOver()
{
currentScreen = Screen.GameOver;
Terminal.ClearScreen();
Terminal.WriteLine("GAME OVER!\nYou didn't make it in time.\nThe police is on their way.\n");
//need help here. if you want to restart -> run main menu again. if not -> exit application completely. I know I could just type "menu" but I want to find another way.
Terminal.WriteLine("Do you want to restart the game or exit?");
}
}
I really enjoy the course by the way. Is there a way to change my rating on Udemy? I’ve already learned a lot but in the beginning I gave 4 stars. I want to change that to 5.