Password randomisation solution with incapsulated object

Guys, I using password repository for each level and inside each object repo store random generated index, so, when game starts i will regenerate these indexes for all levels and use GetPassword method to get random selected password for given level. Have a look and comment, cheers !

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

class PasswordRepository {
    public int level;

    string[] passwords = {};

    int index = 0;

    public PasswordRepository(int level, string[] passwords) {
        this.level = level;
        this.passwords = passwords;
    }

    public string GetPassword() {

        if (passwords.Length == 0) {

            return "password";

        } 

        return passwords[index]; 
    }

    public void GeneratePassword() {

        if (passwords.Length == 0) {

            index = 0;

        } 

        index = Random.Range(0, passwords.Length);

    }
}

public class Hacker : MonoBehaviour
{
    enum Screen { MainScreen, Password, Win };

    static string[] mainMenu = { 
        "\nAre you crazy enough 2 hack into ?\n", 
        "\tFBA - press 1",
        "\tCIA - press 2",
        "\tNSA - press 3\n",
        "Enter your selection: "
    };

    PasswordRepository[] passwordRepos = { 
        new PasswordRepository(1, new string[] {"qwerty", "password"}),
        new PasswordRepository(2, new string[] {"Sevriugin", "Sergei"}),
        new PasswordRepository(3, new string[] {"Moscow", "Russia"}), 
    };

    string userName = "Sergei";

    int level = 0;

    bool gameIsOn = false;

    Screen currentScreen = Screen.MainScreen; 

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

    void ShowMainMenu(string name) {

        gameIsOn = false;

        currentScreen = Screen.MainScreen; 

        Terminal.ClearScreen();
        Terminal.WriteLine($"Hello {name}! ");

        foreach (string item in mainMenu)
        {
            Terminal.WriteLine(item);
        }
    }

    void goToLevel(int level) {
        print($"go to level {level}");
        StartGame();
    }

    void generatePasswords() {
        // TODO: update default repo and generate new passwords for each level

        foreach (PasswordRepository item in passwordRepos)
        {
            item.GeneratePassword();
        }
    }

    void StartGame() {

        if (level == 0) {

           ShowMainMenu(userName);
           return; 

        }

        generatePasswords();

        Terminal.ClearScreen();

        gameIsOn = true;

        currentScreen = Screen.Password;

        Terminal.WriteLine("Enter password: "); 

    }

    void Win() {

        gameIsOn = true;

        currentScreen = Screen.Win;

        Terminal.ClearScreen();

        Terminal.WriteLine("U have won, hacccer!");

        Terminal.WriteLine("\n\tpress Enter to start again...");

    } 

    void ProcessGameInput(string input) {

        if (currentScreen == Screen.Password) {

            print($"Game is on, have got new password: {input}");

            var filtered = passwordRepos.Where(r => r.level == level).ToArray();
            var repo = filtered[0];
            var password = repo.GetPassword();

            if ( password == input ) {
                Win();
            } else {
                Terminal.WriteLine("Wrong password, try again!"); 
            }

        } else {

            ShowMainMenu(userName); 

        }
        
    }

    void ProcessMenuInput(string input) {

        if (int.TryParse(input, out level)) {

            if (level > 0 && level < 4) {

               goToLevel(level);

            } else if (input == "007") {

               Terminal.WriteLine("Agent 007 you are here again?");

               level = 0;

            } else {

               Terminal.WriteLine("Wrong game level"); 

               level = 0; 

            }

        } else {

            Terminal.WriteLine("Learn to type numbers first, hacccker");

        }
    }

    void OnUserInput(string input) {

        print(input);

        if (input == "menu") {

            ShowMainMenu(userName);

            return;

        } else if (input == "winwin") {

            Win();

            return;

        }

        if (gameIsOn) {

            ProcessGameInput(input);

        } else {
            
            ProcessMenuInput(input);

        }

        
    }

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

1 Like

Clean code and very code optimization! Great job!

I think that your approach to using a password repository for each level is pretty clever. One suggestion to enhance security could be to use a password strength tester to ensure the passwords are strong enough. This way, even with random selection, you can be confident that each password meets high-security standards.

Privacy & Terms