Randomised Password From List of Five For Each of the Three Levels

I’m quite pleased with mine - it has five passwords for each of the three levels of difficulty. With a little help from my good friend, Mr Google, I managed to create three arrays of five passwords (I tried to do a 3x5 array but got absolutely nowhere with that!), then it picks one at random each time you select a level. :grin:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = System.Random;

public class Hacker : MonoBehaviour
{
    int level;
    enum Screen { MainMenu, Password, Win };
    Screen currentScreen = Screen.MainMenu;
    string[] passwordsL1 = new string[5] { "books", "shelf", "password", "font", "borrow" };
    string[] passwordsL2 = new string[5] { "prisoner", "handcuffs", "holster", "uniform", "arrest" };
    string[] passwordsL3 = new string[5] { "starfield", "telescope", "environment", "exploration", "astronauts" };
    Random random = new Random();
    string strPassword;
    int passwordIndex;

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

    void ShowMainMenu()
    {
        currentScreen = Screen.MainMenu;
        passwordIndex = random.Next(1, 5);
        Terminal.ClearScreen();
        Terminal.WriteLine("Welcome to Hacker™ v0.21");
        Terminal.WriteLine("");
        Terminal.WriteLine("Which system would you like to hack?");
        Terminal.WriteLine("");
        Terminal.WriteLine("Press 1 for the local library");
        Terminal.WriteLine("Press 2 for the local police station");
        Terminal.WriteLine("Press 3 for NASA");
        Terminal.WriteLine("");
        Terminal.WriteLine("Enter your selection: ");
        Terminal.WriteLine("");
    }
    void OnUserInput(string input)
    {
        if (input == "menu")
        {
            level = 0;
            ShowMainMenu();
        }
        else if (currentScreen == Screen.MainMenu)
        {
            RunMainMenu(input);
        }
        else if (currentScreen == Screen.Password)
        {
            RunCheckPassword(input);
        }
    }

    private void RunMainMenu(string input)
    {
        if (input == "1")
        {
            level = 1;
            strPassword = passwordsL1[passwordIndex];
            StartGame();
        }
        else if (input == "2")
        {
            level = 2;
            strPassword = passwordsL2[passwordIndex];
            StartGame();
        }
        else if (input == "3")
        {
            level = 3;
            strPassword = passwordsL3[passwordIndex];
            StartGame();
        }
        else
        {
            Terminal.WriteLine("Syntax Error: " + input);
        }
    }

    private void StartGame()
    {
        currentScreen = Screen.Password;
        Terminal.WriteLine("You have chosen level " + level + ".");
        Terminal.WriteLine("");
        Terminal.WriteLine("Please enter the password:");
    }

    private void RunCheckPassword(string input)
    {
        if (input == strPassword)
        {
            Terminal.WriteLine("Congratulations! You have successfully guessed the password!");
            Terminal.WriteLine("");
            currentScreen = Screen.Win;
        }
        else
        {
            Terminal.WriteLine("Incorrect password entered - please try again.");
            Terminal.WriteLine("");
            Terminal.WriteLine("Please enter the password:");
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Ooh, just started the next video and noticed it’s about arrays.

Oh my, am I gonna be one of those students? :roll_eyes:

Privacy & Terms