Solo Challenge - took a while but got there

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

public class Hacker_ii : MonoBehaviour
{
    //Game State variables
    public int level;
    public string levelMessage;
    public string levelHint;
    public string password;

    //this is the variable that holds the different screen states. 
    Screen currentScreen;


    //capital letters used for custom enumerations
    enum Screen { MainMenu, Password, Win };


    void Start()
    {
        //so that your custom funciton is 'called' and executed in the game
        //  "hello user" is the parameter. you need to define a parameter for 
        //  the function to work properly. 
        ShowMainMenu("Hello User");
    }

    //User main menu selection screen
    void ShowMainMenu(string greeting)
    {
        //this makes sure that if the user enters "menu" in the password screen, that
        //  the screem is correctly changed to the MainMenu screen. 
        currentScreen = Screen.MainMenu;

        Terminal.ClearScreen();
        Terminal.WriteLine(greeting);

        Terminal.WriteLine("" +
            "Hack Into Stuff \n\r \n\r" +
            "******************************** \n\r" +
            "Press 1 to hack into a random cell\n\r" +
            "Press 2 to hack Boston Games server\n\r" +
            "Press 3 to hack the entire universe\n\r" +
            "********************************\n\r\n\r" +
            "What? Yeah sure it's 'legal'!\n\r" +
             "Enter your selection here: ");
    }

    //I think this one just makes sure the right method is being initialized for what screen is currently active                                                             
    void OnUserInput(string input)
    {

        if (input == "menu")
        {
            level = 0;

            Debug.Log("Main Menu was requested by user");
            ShowMainMenu("Restarting");

        }
        else if (currentScreen == Screen.MainMenu)
        {
            RunMainMenu(input);
        }
        else if (currentScreen == Screen.Password)
        {
            RunPasswordChecker(input);
        }
    }

        //this method sets the pasword and level values based on what level the player selects
        void RunMainMenu(string input)
        {
            if (input == "1")
            {
            //level message
                levelMessage = ("You have chosen to hack \n\rinto a cell phone." +
            "\n\r\n\rPassword attained but remains slightly encrypted.\n\r" +
            "Try to decrypt the following:\n\r\n\r " + levelHint);
                levelHint=("nrogae");

                //new game state variable assignment
                level = 1;
                password = "orange";

                //let's me know the level has been entered
                Debug.Log("Level 1 selected");
                StartGame();
            }
            else if (input == "2")
            {
                levelMessage = ("You have chosen to hack into\n\rthe Boston Games server" +
            "\n\r\n\rPassword attained but remains slightly encrypted.\n\r" +
            "Try to decrypt the following:\n\r\n\r " + levelHint);
                levelHint = ("assacsshemtut");

                level = 2;
                password = "massachusetts";


                Debug.Log("Level 2 selected");
                StartGame();
            }
            else if (input == "3")
            {
                levelMessage = ("You have chosen to hack \n\rthe universe. Wow." +
            "\n\r\n\rPassword attained but remains slightly encrypted.\n\r" +
           "Try to decrypt the following:\n\r\n\r " + levelHint);
                levelHint = ("namgalacemioa");

                level = 3;
                password = "megalomaniac";


                Debug.Log("Level 3 selected");
                StartGame();
            }
            else if (input == "69")
            {
                Terminal.ClearScreen();
                Terminal.WriteLine("\n\r lol. You typed '69' \n\r<Beavis and Butthead laughter> ");
            }
            else if (input == "why" || input.ToLower() == "why?" || input.ToLower() == "why")
            {
                Terminal.ClearScreen();
                Terminal.WriteLine("Because.. \n\rit will be fun");
            }
            else
            {
                Terminal.WriteLine("Error - Invalid Selection - Try again..reloading program");
                //this isn't working for some reason >> Invoke("ShowMainMenu", 5f);
                ShowMainMenu("Error - try again:");

            }
        }

        //this method changes the Screen to the Password screen after the player makes a 
        //  valid level selection
        void StartGame()
        {
            currentScreen = Screen.Password;
            Terminal.ClearScreen();
            Terminal.WriteLine(levelMessage);
            Terminal.WriteLine("\n\rStart Guessing: ");

        }

        //this checks if the password was guessed correctly
        void RunPasswordChecker(string input)
        {
            Debug.Log("PasswordChecker function initiated");

            if (input.ToLower() == password || input == password)
            {
                Terminal.WriteLine("HACKED! Nice job");
            }
            else
            {
                //I added the levelHint in again becuase the initial hint kept scrolling up
                //  and eventually went out of the players view. 
                Terminal.WriteLine("Try again, you are decrypting this: \n\r" + levelHint);
                Terminal.WriteLine("");
            }
        }
    
}

Initially I could not find a way to check if the player input was a password guess (it kept leading me back to an incorrect menu item selection screen) so I hit a wall at that point. Then started looking at what other students had put together before looking at the solution in the video. I still have not looked at the solution, I wanted to post up my own solution before I move on. I leaned pretty heavily on @HeathClose’s solution, I liked how clean it was and it helped me understand how the code structure worked.

For my own solution I added on some more game state variables (level-specific messages and hints) to make the UI a little easier to customize with various menu selections. I tried to label everything as well as I could in the code more for my sake because I kept on getting lost in what method is activated based on what enum Screen was active. I would like to learn alternatives to using if/else statements though. I tried a switch structure and got some weird errors back…so for time sake decided to go back to if/else for now. :alien:

4 Likes

Privacy & Terms