Just showing understanding

This was pretty simple, initially. I just copy and pasted the one for level1passwords and change it to level2passwords. I ran into an issue where index was repeating. I solved this by just renaming them to randomPassword1 and 2. Pretty much, what I think is happening here is that Unity is just picking a random number out of the amount of passwords you have, it’s then matching that up to a password and it displays that one.

       ```
        case 1:
            int randomPassword1 = Random.Range(0, level1Passwords.Length);
            password = level1Passwords[randomPassword1];
            break;
        case 2:
            int randomPassword2 = Random.Range(0, level2Passwords.Length);
            password = level2Passwords[randomPassword2];
            break;
        default:
            Debug.LogError("Invalid level number");
            break;
    }
1 Like
using System;
using UnityEngine;

enum Screen {EnterName,MainMenu, Password, Win};

public class Hacker : MonoBehaviour
{
    string name;
    int level;
    string passwordInput;
    string passwordToGuess;
    Screen currentScreen;
    string[] validLevels = { "1","2","3"};
    string[] passwordLevel1 = {"Teeku", "Bubbu", "Pukku", "Wuggy"};
    string[] passwordLevel2 = {"HTTP", "FTP", "UML", "Hex", "Doc"};
    string[] passwordLevel3 = {"Echo", "Blue", "Rigid"};
    void Start()
    {
        if (string.IsNullOrEmpty(name))
        {
            EnterName();
        }
    }

    void EnterName()
    {
        currentScreen = Screen.EnterName;
        Terminal.ClearScreen();
        Terminal.WriteLine("Please enter your Name: ");
    }
    void ShowMainMenu()
    {
        currentScreen = Screen.MainMenu;
        Terminal.ClearScreen();
        Terminal.WriteLine("Hi "+ name + "!");
        Terminal.WriteLine("Welcome to The Hacking Game!\n Press 1 to hack Phone\n Press 2 to hack Server\n Press 3 to hack IBM \n\n Enter your selection:");
    }
    
    void OnUserInput(string input)
    {
        if (currentScreen == Screen.EnterName)
        {
            name = input;
            ShowMainMenu();
        }
        else if (currentScreen == Screen.MainMenu)
        {
       ;
            if (Array.IndexOf(validLevels, input) >= 0)
            {
                level = int.Parse(input);
                PlayLevel();
            }
            else
            {
                Terminal.WriteLine("Invalid level selected. Try Again.");
            }
        }
        else if (currentScreen == Screen.Password)
        {
            if(input == "menu"){
                ShowMainMenu();
            }
            else {
                CheckPassword(input);
            }
        }
        else if (currentScreen == Screen.Win)
        {
            ShowMainMenu();
        }
        else
        {
            print("Enter correct input");
        }
    }

    void PlayLevel()
   {
       currentScreen = Screen.Password;
       Terminal.ClearScreen();
       Terminal.WriteLine("You have selected level: "+ level);
       Terminal.WriteLine("To go to Main Menu type -> menu");
       switch(level){
           case 1:
           passwordToGuess = passwordLevel1[UnityEngine.Random.Range(0, passwordLevel1.Length)];
           break;
           case 2:
           passwordToGuess = passwordLevel2[UnityEngine.Random.Range(0, passwordLevel2.Length)];
           break;
           case 3:
           passwordToGuess = passwordLevel3[UnityEngine.Random.Range(0, passwordLevel3.Length)];
           break;
           default:
           break;
       }
       Terminal.WriteLine("Guess the password: ");
   }
   
   void CheckPassword(string input)
   {
       passwordInput = input;
       if (passwordInput == passwordToGuess)
       {
           currentScreen = Screen.Win;
           Terminal.ClearScreen();
           Terminal.WriteLine("You Win!");
       }
       else
       {
           Terminal.WriteLine(":( Try Again..");
       }
   }
}

Privacy & Terms