Hacker game code review requested

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;

			}
		}
}

Hi Michael,

Welcome to our forum. :slight_smile:

Thank you for sharing your game. You did a great job, and I’ve noticed that you added additional functionality. Well done!

The following is not a real improvement, just a little refactoring in case you want to make your game more flexible. Have you already heard about arrays, return types and loops? If not, take a look at the following. I’m sure you can undestand it, even if you do not know these features/techniques yet.

bool[] levelsCleared = { false, false }; // two levels, thus two entries

bool AreAllLevelsCleared()
{
    foreach (bool element in levelsCleared)
    {
        // If one of the elements in the array is false,
        // not all levels are cleared
        if (!element) { return false; }
    }

    return true;

}

// And then as an example:
void WriteLevel3() {
    if (AreAllLevelsCleared()) // if the method returns true
    {
        Terminal.WriteLine("Press 3 for Abstergo");
    }
}

This way, you do not have to rely on a levelsCleared variable which could easily be manipulated.

The values in the array can be manipulated like this:

levelsCleared[0] = true; // first element in array
levelsCleared[1] = false; // second element in array

I hope this helps a bit. Your code is fine as it is because it is working. Keep it up! :slight_smile:

1 Like

Privacy & Terms