Terminal Hacker with extra features and lots of code comments

hacker.cs code :

using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Hacker : MonoBehaviour
{
    // Game configuration data
    string[,] passArray = new string[,] {
        { "book", "shelf", "archive", "study", "reading"},
        { "officer", "uniform", "storeroom", "detective", "sergeant" },
        { "astronaut", "astronomy", "universe", "andromeda", "telescope" }
    };
    const string congratsASCII = @"

     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     |C|o|n|g|r|a|t|u|l|a|t|i|o|n|!|
     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    ";
    // Game State
    int level;
    int hintsCount;
    int hintsUsed = 0;
    int wrongPasswordInput = 0;
    string playerName;
    string password;
    enum Screen { Intro, MainMenu, Password, Win };
    List<string> tempHints = new List<string>();
    List<string> noDupesHints = new List<string>();    
    Screen currentScreen;
    // Start is called before the first frame update
    void Start()
    {        
        ShowIntroScreen();
    }    
    void ShowMainMenu(string name)
    {
        currentScreen = Screen.MainMenu;
        Terminal.ClearScreen();        
        Terminal.WriteLine("Hello " + name + "\n");
        Terminal.WriteLine("What would you like to hack into?\n");
        Terminal.WriteLine("Press 1 for the local library");
        Terminal.WriteLine("Press 2 for the police station");
        Terminal.WriteLine("Press 3 for NASA");
        Terminal.WriteLine("Type \"name\" to change your name");
        Terminal.WriteLine("Type \"menu\" to reach menu\n");
        Terminal.WriteLine("Enter your selection:");
    }    
    void OnUserInput(string input)
    {
        // Handle null input
        if (input == null)
        {
            input = "";
        }
        if (currentScreen != Screen.Intro)
        {
            if (input == "menu") // we can always go direct to main menu
            {
                ShowMainMenu(playerName);
            }         
            else if (input == "name")
            {
                ShowIntroScreen();
            }
            else if (currentScreen == Screen.MainMenu)
            {
                RunMainMenu(input);
            }
            else if (currentScreen == Screen.Password)
            {
                if (input == "hint")
                {
                    Terminal.WriteLine(GetHint());
                }
                else
                {
                    CheckPassword(input);
                }
            }
            else
            {
                Terminal.WriteLine("I can't recognize the \"" + input + "\" command");
            }
        }
        else
        {
            GetPlayerName(input);
        }
    }
    void ShowIntroScreen()
    {
        currentScreen = Screen.Intro;
        Terminal.ClearScreen();
        Terminal.WriteLine("Please introduce yourself!\nType your name then press \"enter\"");
    }
    void GetPlayerName(string input)
    {
        // check if player chose a name with at least 3 character
        if (input.Length >= 3)
        {
            playerName = input;
            ShowMainMenu(playerName);
        }
        else
        {
            Terminal.WriteLine("Please choose a name with at least 3 characters!");
        }
    }
    void RunMainMenu(string input)
    {
        bool isValidLevel = (input == "1" || input == "2" || input == "3");
        if (isValidLevel)
        {
            // convert input to int and assign to level variable
            level = int.Parse(input);
            StartGame();
        }
        else if (input == "007") {
            Terminal.WriteLine("Please select a level Mr Bond!");
        }
        else
        {
            Terminal.WriteLine("Please choose a valid level!");
        }            
    }
    void StartGame()
    {
        currentScreen = Screen.Password;
        ResetState();
        GeneratePassword();
        Terminal.ClearScreen();
        Terminal.WriteLine("Type: \"hint\" for help");
        Terminal.WriteLine("Please enter your password!");
    }
    void ResetState()
    {
        // clear lists
        tempHints.Clear();
        noDupesHints.Clear();
        // reset variables
        hintsUsed = 0;
        wrongPasswordInput = 0;
    }
    void GeneratePassword()
    {    
        // Get the length of passArray 2nd dimension
        int dimensionLength = passArray.GetLength(1);
        // Get a random password from passArray[,]
        password = passArray[level - 1, Random.Range(0, dimensionLength)];
        // shuffle password
        Shuffle(password);
    }
    void Shuffle(string password)
    {
        // create array of charachters from password
        char[] charArray = password.ToCharArray();
        // Get the length of charArray
        int charArrayLength = charArray.Length;
        while (charArrayLength > 1)
        {
            charArrayLength--;
            // Create a random index
            int randomIndex = Random.Range(0, charArrayLength + 1);
            // Get the character at randomindex from charArray to temp variable
            var value = charArray[randomIndex];
            // swap the characters
            charArray[randomIndex] = charArray[charArrayLength];
            charArray[charArrayLength] = value;
            // Convert charArray to hint string
            string hint = new string(charArray);
            // add to tempHints list
            tempHints.Add(hint);
        }
        // Remove duplicates from tempHints list (using System.Linq; for Distinct() method)
        noDupesHints = tempHints.Distinct().ToList();
        // count the elements of noDupesHints list
        hintsCount = noDupesHints.Count;
    }
    void CheckPassword(string input)
    {
        // convert input and password to lowercase then compare
        if (input.ToLower() == password.ToLower())
        {
            RunWinScreen();
        }
        else
        {
            // add +1 to wrongPasswordinput counter
            wrongPasswordInput++;
            Terminal.WriteLine("Wrong Password try again!");
            Terminal.WriteLine("Type: \"hint\" for help");
        }
    }
    void RunWinScreen()
    {
        currentScreen = Screen.Win;
        Terminal.ClearScreen();
        Terminal.WriteLine(congratsASCII);
        Terminal.WriteLine("Good Job " + playerName + "! You got it!\n");
        Terminal.WriteLine("You typed wrong password " + wrongPasswordInput + " times.");
        Terminal.WriteLine("You used " + hintsUsed + " hints.");
        Terminal.WriteLine("Type \"name\" to change your name");
        Terminal.WriteLine("Type \"menu\" to reach main menu");
    }
    string GetHint()
    {
        // decrease hintsCount to get the last unused hint index in noDupesHints list
        hintsCount--;
        // check if there are still unused hints avaible
        if (hintsCount >= 0)
        {
            // add +1 to hintsUsed counter
            hintsUsed++;
            Terminal.WriteLine("You have " + hintsCount + " hints left");
            // return the last (not used) input from noDupesHints list
            return noDupesHints[hintsCount];
        }
        else
        {
            return "Sorry no more hints";
        }
    }
}

Privacy & Terms