Level 1 & 2 Win Screens and an extra gimmick

I designed the win screens (level 1 is the police station and 2 is NASA)
I also added a fail mechanic,
which is not yet implemented as a fourth game state but could easily be.
edit: in the first reply, after cleanup, it is.

using UnityEngine;

public class Hacker : MonoBehaviour
{
    //Game configuration data
    //string[  ] level0Passwords = { "book", "author", "shelf", "borrow", "research" };
    string[  ] level1Passwords = { "intervention", "harassment", "security", "profiler" };
    string[  ] level2Passwords = { "armstrong", "pleiades", "andromeda", "endeavour", "supermassive", "interplanetary" };
    
    //Game state
    string chosenName = "stranger";
    string password;
    int level;
    int attempts;
    enum Screen { MainMenu, Password, Win }
    Screen currentScreen = Screen.MainMenu;
    
    // Start is called before the first frame update
    void Start()
    {
        ShowMainMenu("stranger");
    }

    private void Update()
    {
        //print("0 level: " + level0Passwords[Random.Range(0, level1Passwords.Length)]);
        //print("1st level: " + level1Passwords[Random.Range(0, level2Passwords.Length)]);
        //print("2nd level: " + level2Passwords[Random.Range(0, level3Passwords.Length)]);
    }

    void ShowMainMenu(string username)
    {
        Terminal.ClearScreen();
        Terminal.WriteLine("Hello, " + username);
        Terminal.WriteLine("You've made it this far");
        Terminal.WriteLine("Choose your battles wisely");
        Terminal.WriteLine("");
        Terminal.WriteLine("Enter 1 for police station archives;");
        Terminal.WriteLine("Enter 2 for NASA admin;");
        Terminal.WriteLine("");
    }

    void RunMainMenu(string input)
    {
        bool isValidLevelNumber = (input == "1" || input == "2");
        if (isValidLevelNumber)
        {
            level = int.Parse(input);
            StartGame();
        }
        else if (input == "my name is Bogdan") //easter egg
        {
            chosenName = "Bogdan";
            ShowMainMenu(chosenName);
        }
        else
        {
            Terminal.WriteLine("input unrecognized");
        }
    }

    private void StartGame()
    {
        currentScreen = Screen.Password;
        Terminal.ClearScreen();
        switch (level)
        {
            case 1:
                password = level1Passwords[Random.Range(0, level1Passwords.Length)];
                attempts = 5;
                break;
            case 2:
                password = level2Passwords[Random.Range(0, level2Passwords.Length)];
                attempts = 3;
                break;
            default:
                Debug.LogError("Invalid level number");
                break;
        }
        Terminal.WriteLine("Please enter your password");
    }

    void CheckPassword(string input)
    {
        if (input == password)
        {
            DisplayWinScreen();
        }
        else if (attempts > 0)
        {
            attempts--;
            if (attempts > 1)
            {
                Terminal.WriteLine("Wrong. Please mind your own business.");
            }
            else if (attempts == 1)
            {
                Terminal.WriteLine("This is your last warning.");
            }
            else
            {
                Terminal.ClearScreen();
                if (level == 1)
                {
                    Terminal.WriteLine("Resisting arrest will have worse consequences."); 
                }
                else
                {
                    Terminal.WriteLine("The authorities have been informed.");
                }
            }
        }
    }

    void DisplayWinScreen()
    {
        if (attempts > 0)
        {
            Terminal.ClearScreen();
            currentScreen = Screen.Win;
            ShowLevelReward();
        }
    }

    void ShowLevelReward()
    {
        switch(level)
        {
            case 1:
                Terminal.WriteLine("Welcome back, officer!");
                Terminal.WriteLine(@"
    ,   /\   ,
     / '-'  '-' \
    |   POLICE   |
    \    .--.    /
     |  ( 19 )  |
     \   '--'   /
      '--.  .--'
   jgs    \/        
                ");
                break;
            case 2:
                Terminal.WriteLine(@"
                ,,)))))));, 
            __))))))))))))));,
           -\(((((''''(((((((( >~
  --==//////((''  .     `)))))), 
          ))| @    ;-.     (((((
          ( `|    /  )      )))
             |   |   |       (
             o_);   ;       
                   ;       
                          ");
                Terminal.WriteLine("It is safe to answer your phone.");
                break;
            default:
                Debug.LogError("Invalid level number");
                break;
        }
    }

    void OnUserInput(string input)
    {
        if (input == "menu")
        {
            currentScreen = Screen.MainMenu;
            ShowMainMenu(chosenName);
        }
        else if (currentScreen == Screen.MainMenu)
        {
            RunMainMenu(input);
        }
        else if (currentScreen == Screen.Password)
        {
            CheckPassword(input);
        }
    }

}

>

Cleaned the code as instructed in the next lecture.

using UnityEngine;

public class Hacker : MonoBehaviour
{
    //Game configuration data
    //string[] level0Passwords = { "book", "author", "shelf", "borrow", "research" };
    string[] level1Passwords = { "intervention", "harassment", "security", "profiler" };
    string[] level2Passwords = { "armstrong", "pleiades", "andromeda", "endeavour", "supermassive", "interplanetary" };
    int[] attemptsPerLevel = { 5, 3 };

    //Game state
    string chosenName = "stranger";
    string password;
    int level;  
    int attempts;
    enum Screen { MainMenu, Password, Win, Fail }
    Screen currentScreen = Screen.MainMenu;

    // Start is called before the first frame update
    void Start()
    {
        ShowMainMenu("stranger");
    }

    void OnUserInput(string input)
    {
        if (input == "menu")
        {
            ShowMainMenu(chosenName);
        }
        else
            switch (currentScreen)
            {
                case Screen.MainMenu:
                    RunMainMenu(input);
                    break;
                case Screen.Password:
                    CheckPassword(input);
                    break;
                case Screen.Win:
                    ShowLevelReward();
                    MenuReminder();
                    break;
                case Screen.Fail:
                    ShowLevelFail();
                    MenuReminder();
                    break;
                default:
                    Debug.LogError("Invalid currentScreen state");
                    break;
            }
    }

    void ShowMainMenu(string username)
    {
        currentScreen = Screen.MainMenu;
        Terminal.ClearScreen();
        Terminal.WriteLine("Hello, " + username);
        Terminal.WriteLine("You've made it this far");
        Terminal.WriteLine("Choose your battles wisely");
        Terminal.WriteLine("");
        Terminal.WriteLine("Enter 1 for police station archives;");
        Terminal.WriteLine("Enter 2 for NASA admin;");
        Terminal.WriteLine("");
        Terminal.WriteLine("Enter 'menu' at any time to restart");
    }

    void RunMainMenu(string input)
    {
        bool isValidLevelNumber = (input == "1" || input == "2");
        if (isValidLevelNumber)
        {
            level = int.Parse(input);
            StartLevel();
        }
        else if (input == "my name is Bogdan") //easter egg
        {
            chosenName = "Bogdan";
            ShowMainMenu(chosenName);
        }
        else
        {
            Terminal.WriteLine("Please enter a valid number.");
            MenuReminder();
        }
    }

    void MenuReminder()
    {
    Terminal.WriteLine("(enter 'menu' to restart)");
    }

    void StartLevel()
    {
        currentScreen = Screen.Password;
        Terminal.ClearScreen();
        GenerateRandomPassword();
        AskForPassword();
    }

    void GenerateRandomPassword()
    {
        switch (level)
        {
            case 1:
                password = level1Passwords[Random.Range(0, level1Passwords.Length)];
                break;
            case 2:
                password = level2Passwords[Random.Range(0, level2Passwords.Length)];
                break;
            default:
                Debug.LogError("Invalid level number");
                break;
        }
        attempts = attemptsPerLevel[level - 1];
    }

    void AskForPassword()
    {
        if (attempts == attemptsPerLevel[level - 1]) //if the level has just started
        {
            Terminal.WriteLine("Please enter your password");
            Terminal.WriteLine("hint: " + password.Anagram());
        }
        else
           if (attempts > 1)
        {
            Terminal.WriteLine("Incorrect. You have " + attempts + " more tries");
            Terminal.WriteLine("hint: " + password.Anagram());
        }
        else if (attempts == 1)
        {
            Terminal.WriteLine("Incorrect. You have 1 more try.");
            Terminal.WriteLine("hint: " + password.Anagram());
        }
        else
            Debug.LogError("Tried to ask for password with attempts == " + attempts);
    }

    void CheckPassword(string input)
    {
        if (input == password)
        {
            WinLevel();
        }
        else
        {
            attempts--;
            if (attempts > 0)
            {
                AskForPassword();
            }
            else
            {
                FailLevel();
            }
        }
    }

    void FailLevel()
    {
            currentScreen = Screen.Fail;
            ShowLevelFail();
    }

    void ShowLevelFail()
    {
        Terminal.ClearScreen();
        switch (level)
        {
            case 1:
                Terminal.WriteLine("You should not be here. Leave.");
                break;
            case 2:
                Terminal.WriteLine("You should not have tried this.");
                break;
            default:
                Debug.LogError("Reached level fail screen with invalid level number " + level);
                break;
        }
    }

    void WinLevel()
    {
            currentScreen = Screen.Win;
            ShowLevelReward();
    }

    void ShowLevelReward()
    {
        Terminal.ClearScreen();
        switch (level)
        {
            case 1:
                Terminal.WriteLine("Welcome back, officer!");
                Terminal.WriteLine(@"
     / '-'  '-' \
    |   POLICE   |
    \    .--.    /
     |  ( 19 )  |
     \   '--'   /
      '--.  .--'
   jgs    \/        
                ");
                break;
            case 2:
                Terminal.WriteLine(@"
                ,,)))))));, 
            __))))))))))))));,
           -\(((((''''(((((((( >~
  --==//////((''  .     `)))))), 
          ))| @    ;-.     (((((
          ( `|    /  )      )))
             |   |   |       (
             o_);   ;       
                   ;       
                          ");
                Terminal.WriteLine("It is safe to answer the phonecall ;)");
                break;
            default:
                Debug.LogError("Reached win screen with invalid level number " + level);
                break;
        }
    }

    private void Update()
    {
        //print("0 level: " + level0Passwords[Random.Range(0, level1Passwords.Length)]);
        //print("1st level: " + level1Passwords[Random.Range(0, level2Passwords.Length)]);
        //print("2nd level: " + level2Passwords[Random.Range(0, level3Passwords.Length)]);
    }

}

Privacy & Terms