Game and code

Hi,
this is my second improvement of my Hacker game.
https://dontsharemygame.com/@DanF/~0153ea8b-c775-5190-ed55-e6be20175edf

Here is the corresponding code. I’m interested in your feedback to your understanding of this code as coding method style and coding for others is totally new for me.

using UnityEngine;

public class Hacker : MonoBehaviour
{
    //data
    string[] PasswordEasy = {"kid", "school", "image", "dog",
        "screen", "storm", "wind", "house", "virus"};
    string[] PasswordMedium = {"window", "America", "clothing",
        "computer", "teacher", "hunter", "thunder", "sheet"};
    string[] PasswordDiff = {"unfortunately", "difficulty", "terminal",
        "children", "pedestrian", "irregardless", "colonel", "nonplussed"};
    string[] PasswordSDiff = {"adumbrate", "contentious", "diaphanous",
        "ebullient", "impecunious", "legerdemain", "ostensible", "pulchritude"};

    // game state.
    string level;
    enum Screen {MainMenu, Password, Win};
    Screen currentScreen;

    string Password;
    int i;
    int times = 3;
    string sentenceEnd;
    int win;


    // Start is called before the first frame update.
    void Start()
    {
        PrepareInitialMainMenu();
        win = 0;
    }

    void PrepareInitialMainMenu()
    {
        Terminal.WriteLine("Hello.");
        Terminal.WriteLine("Do you like to play a game?");
        Terminal.WriteLine("It is a word puzzle that will teach ");
        Terminal.WriteLine("you how to hack into passwords.");
        // Terminal.WriteLine("Are you interested? Then ...");
        ShowMainMenu("");

    }

    void PrepareAgainMainMenu(string text)
    {
        Terminal.ClearScreen();
        ShowMainMenu(text);
    }

    void ShowMainMenu(string feedback)
    {
        times = 3;
        sentenceEnd = "attempts";
        currentScreen = Screen.MainMenu;

        Terminal.WriteLine(feedback);
        Terminal.WriteLine("Press 1 for an easy game");
        Terminal.WriteLine("Press 2 for a medium game");
        Terminal.WriteLine("Press 3 for a difficult game");

        // additional level.
        if (win > 3)
        {
            Terminal.WriteLine("Press 4 for a super difficult game");
        }

        Terminal.WriteLine("Press n for ending the game."); 
        Terminal.WriteLine("");
        Terminal.WriteLine("Enter your selection in the next line.");
    }

    void OnUserInput(string input)
    {
        if (input == "menu")
        {
            PrepareAgainMainMenu("");
        }
        else if (currentScreen == Screen.MainMenu)
        {
            RunWhatLevel(input);
        }
        else if (currentScreen == Screen.Password)
        {
            RunResult(input);
        }
        else if (currentScreen == Screen.Win)
        {
            RunNextGame(input);
        };
    }

    void RunWhatLevel(string input)
    {
        if (input == "1")
        {
            level = "easy";
            i = Random.Range(0, PasswordEasy.Length);
            RunGame(i);
        }
        else if (input == "2")
        {
            level = "medium";
            i = Random.Range(0, PasswordMedium.Length);
            RunGame(i);
        }
        else if (input == "3")
        {
            level = "difficult";
            i = Random.Range(0, PasswordDiff.Length);
            RunGame(i);
        }
        else if (input == "4")
        {
            level = "super difficult";
            i = Random.Range(0, PasswordSDiff.Length);
            RunGame(i);
        }
        else if (input == "n")
        {
            Terminal.ClearScreen();
            MenuHint();
            Terminal.WriteLine("You can close the tab in your browser.");
        }
        else
        {
            Terminal.WriteLine("Sorry. I don't know what to do.");
            Terminal.WriteLine("Try again.");
        };
    }

    void RunGame(int index)
    {
        currentScreen = Screen.Password;
        Terminal.ClearScreen();

        WriteFirstLines();

        switch (level)
        {
            case "easy":
                Password = PasswordEasy[index];
                break;
            case "medium":
                Password = PasswordMedium[index];
                break;
            case "difficult":
                Password = PasswordDiff[index];
                break;
            case "super difficult":
                Password = PasswordSDiff[index];
                break;
            default:
                Debug.LogError("invalid level");
                break;
        };
        Terminal.WriteLine("Please enter your password guess.");
        Terminal.WriteLine("You have " + times + " " + sentenceEnd + ".");
        Terminal.WriteLine("Here is the word with jumbled letters: " + Password.Anagram());
    }

    void WriteFirstLines()
    {
        if (times < 3)
        {
            Terminal.WriteLine("Sorry, this isn't correct.");
            Terminal.WriteLine("Please try again.");
        }
        MenuHint();
    }

    void RunResult(string input)
    {
        currentScreen = Screen.Win;
        Terminal.ClearScreen();

        if (input == Password)
        {
            if (times == 3)
            {
                PictureBestResult();
                win = win + 1;
            }
            else if (times == 2)
            { 
                PictureMiddleResult();
            }
            else if (times == 1)
            {
                PictureLastResult();
            }
            else if (level == "super difficult")
            {
                PictureSuperResult();
            }
            times = 3;
            sentenceEnd = "";  
    }
        else
        {
            times = times - 1;
            sentenceEnd = "more attempts";
            if (times == 0)
            {
                Terminal.WriteLine("Sorry the password is now closed.");
                Terminal.WriteLine("The solution was: " + Password);
                Terminal.WriteLine("Do you want another game? y / n");
            }
            else if (times == 1)
            {
                sentenceEnd = "more attempt";
                RunGame(i);
            }
            else
            {
                RunGame(i);
            }      
        };
    }

    void RunNextGame(string input)
    {
        MenuHint();

        if (input == "y")
        {
            PrepareAgainMainMenu("");
        }
        else if (input == "n")
        {
            Terminal.ClearScreen();
            Terminal.WriteLine("You can close the tab in your browser.");
        }
        else
        {
            PrepareAgainMainMenu("The menu because I don't understand.");
        };
    }

    void MenuHint()
    {
        Terminal.WriteLine("**************************************");
        Terminal.WriteLine("Type 'menu' to restart the main menu.");
        Terminal.WriteLine("**************************************");
    }

    void PictureSuperResult()
    {
        Terminal.WriteLine(@"
    Super. This is exactly right.

              \(*_*)
               (  (>
             __/__\_
            |       |
            |       |
     _______|       |_______
    ;   3   |   1   |   2   ;
    ;_______|_______|_______;

    Do you want another game? y / n");
    }

    void PictureBestResult()
    {
        Terminal.WriteLine(@"
    Super. This is exactly right.

              \(*_*)
               (  (>
             __/__\_
     _______|       |_______
    ;   3   |   1   |   2   ;
    ;_______|_______|_______;

    Do you want another game? y / n");
    }

    void PictureMiddleResult()
    {
        Terminal.WriteLine(@"
    Super. This is exactly right.

                      \(*_*)
             _______   (  (> 
     _______|       |__/__\_
    ;   3   |   1   |   2   ;
    ;_______|_______|_______;

    Do you want another game? y / n");
    }

    void PictureLastResult()
    {
        Terminal.WriteLine(@"
    Super. This is exactly right.

      \(*_*)  
       (  (> _______
     __/__\_|       |_______
    ;   3   |   1   |   2   ;
    ;_______|_______|_______;

    Do you want another game? y / n");
    }
}
1 Like

I like it a lot. It played well too.

Nice!

Privacy & Terms