Check out my options!

Edit: forgot link- whoops!
W0RD_M4ST3R_2k - Terminal Hacker Game

I think it’s pretty nifty. My code is quite different, but I’ve been programming for a while :wink:

I’m trying hard not to use case and do it the “class way”. No enumerators, stuck with a really shifty game state manager and “anykey” mode that I hastily coded.

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

public class Hacker : MonoBehaviour
{
// ########## -- START OF CLASS -- ########## //
    private string terminalInput;
    private string tempInput;

    private string loginName;

    private bool isOnLoginScreen;
    private string loginScreenState;

    // 0 - Load > Main Menu; 1 - Difficulty #1, 2 - #2, etc; 4 is fail state, 999 - Difficulty Linus (special mode), 99 -- Derivative of #2 (special message) (difficulty set to 2 with 007)
    private int currentGameState = 0;

    private bool flagTorvaldQuit = false;
    private bool flagTorvaldShowSuccess = false;
    private bool flagTryAgainSubmit = false;

    private int numberofFrames = 0;
    private int tempFrames = 0;

    bool startedTryAgain = false;

    //REFACTOR TO GAMESTATEMANAGER
    void RunGameStateManager()
    {
        if (currentGameState == 1)
        {
            Terminal.ClearScreen();

            Terminal.WriteLine("Hacking your grading system... ");
        }
        else if (currentGameState == 2)
        {
            Terminal.ClearScreen();

            Terminal.WriteLine("Hacking the Hacker Fleet Headquarters! \n" +
                               "Caution!");
        }
        else if (currentGameState == 3)
        {
            Terminal.ClearScreen();

            Terminal.WriteLine("HACKING THE MATRIX. \n\n\n\n" +
                               "BEWARE: \n" +
                               "ABILITY TO ALTER THE UNIVERSE AHEAD");
        }
        // James Bond State
        else if (currentGameState == 99)
        {
            if (flagTryAgainSubmit == false)
            {
                Terminal.ClearScreen();

                Terminal.WriteLine("Welcome Back, Mr. Bond.");
                loginName = "James Bond 007";

                Terminal.WriteLine("\nYou are now hacking S.P.E.C.T.R.E 's \n" +
                                   "Hacker Fleet Headquarters. \n\n" +
                                   "Press any key to continue.");

                flagTryAgainSubmit = true;
                tempFrames = numberofFrames;
            }


            if (Input.GetButtonDown("Submit") && flagTryAgainSubmit == true && numberofFrames - tempFrames >= 1)
            {
                flagTryAgainSubmit = false;
                currentGameState = 0;
                RunGameStateManager();
            }


        }
        // Linus State
        else if (currentGameState == 999)
        {
            if (flagTorvaldShowSuccess == false && flagTorvaldQuit == false)
            {
                Terminal.ClearScreen();

                Terminal.WriteLine("Welcome back, Linus of Linux. \n" +
                                   "Access immediately granted.");
                loginName = "Linus Torvalds";

                Terminal.WriteLine("\n\nYou are now hacking THE MATRIX\n" +
                                   "You wrote the Matrix, so you \nalready have access.\n\n" +
                                   "Press any key to continue.");

                flagTorvaldShowSuccess = true;
                tempFrames = numberofFrames;
            }
            if (Input.GetButtonDown("Submit") && flagTorvaldShowSuccess == true && numberofFrames - tempFrames >= 1)
            {
                flagTorvaldShowSuccess = false;
                flagTorvaldQuit = true;
                Debug.Log("Success Reached. ShowSuccess = {flagTorvaldShowSuccess} : FlagQuit = {flagTorvaldQuit}");
                ShowSuccessScreen();
                tempFrames = numberofFrames;
            }
            else if (Input.GetButtonDown("Submit") && flagTorvaldQuit == true && numberofFrames - tempFrames >= 1) // this last && check might not be necessary
            {
                flagTorvaldQuit = false;
                Debug.Log("Quit Reached. ShowSuccess = {flagTorvaldShowSuccess} : FlagQuit = {flagTorvaldQuit}");
                currentGameState = 0;
                RunGameStateManager();
            }
        }
        // Start State -- Main Menu
        else if (currentGameState == 0)
        {
            ResetVars();
            ShowMainMenu();
        }
        // Fail State
        else if (currentGameState == 4)
        {

            ShowTryAgainScreen();

            if (tempFrames == 0)
            {
                startedTryAgain = true;
                tempFrames = numberofFrames;
            }
            else if (Input.GetButtonDown("Submit") && numberofFrames - tempFrames >= 1 && startedTryAgain == true)
            {   
                currentGameState = 0;   
                RunGameStateManager();
            }



        }
        else
            Debug.Log("Invalid Game State\n" +
                      "GameState = {gameState}");
            //Reset gamestate as failsafe.
            //currentGameState = 0;       
    }

    private void ResetVars()
    {
        flagTorvaldQuit = false;
        flagTorvaldShowSuccess = false;
        flagTryAgainSubmit = false;
        startedTryAgain = false;
        loginName = "guest";

        numberofFrames = 0;
        tempFrames = 0;
    }

    void ChangeDifficultyFromMainMenu()
    {

        if (terminalInput == "menu")
        {
            currentGameState = 0;
        }
        else if (terminalInput == "007")
        {
            currentGameState = 99;
        }
        else if (terminalInput == "Linus Torvalds")
        {
            currentGameState = 999;
        }
        else if (terminalInput == "1")
        {
            currentGameState = 1;
        }
        else if (terminalInput == "2")
        {
            currentGameState = 2;
        }
        else if (terminalInput == "3")
        {
            currentGameState = 3;
        }
        else
        {
            // Invalid Entry State
            currentGameState = 4;
        }
    }

    void ShowMainMenu()
    {
        // Reset Screen
        Terminal.ClearScreen();

        // Introduction
        Terminal.WriteLine("WELCOME HACKER! USER-ID: " + loginName);

        // Game Mode Select Question
        Terminal.WriteLine("What would you like to hack?\n");

        // Easy
        Terminal.WriteLine("Press 1 for Grading System");
        // Normal
        Terminal.WriteLine("Press 2 for H4CK3R_FL33T HQ");
        // Hard
        Terminal.WriteLine("Press 3 for T.H.E. M.A.T.R.I.X.");

        // Continue Instuctions
        Terminal.WriteLine("\n");
        Terminal.WriteLine("Enter your selection >>>  ");
    }

// ########## -- START OF LOOP -- ########## //
    void Start()
    {
        ResetVars();

        currentGameState = 0;
        RunGameStateManager();
    }

    void Update()
    {
        numberofFrames += 1;

        // Just a failsafe. Not accurate 32-int size, but it gets the job done. If user keeps the game running too long, can screw up the count and not trigger the Enter press. / Submit.
        // Keep at 2bil roughly, don't allow overflow plz. The compiler on this project doesn't allow it. Idk why. I ran into that issue with the logline List<string> ordeal.
        // Low numbers break it. above a 5 second mark per framerate.
        if (numberofFrames >= 2000000000)
        {
            numberofFrames = 0;
        }

    }

    void OnUserInput(string input)
    {
        terminalInput = input;

        if (terminalInput == "quit")
        {
            Application.Quit();
        }

        if (currentGameState == 0)
        {
            // Change modes if already on Main Menu
            ChangeDifficultyFromMainMenu();
            RunGameStateManager();
        }
        else if (terminalInput == "menu")
        {
            // if User Types Menu, return to main.
            currentGameState = 0;
            RunGameStateManager();
        }
        else
            // else, must run GSM at least once.
            RunGameStateManager();

    }
// ########## -- END OF LOOP -- ##########//

    void ShowSuccessScreen()
    {
        Terminal.ClearScreen();
        Terminal.WriteLine("\n\n\n\n\n\n            ACCESS_GRANTED");
        //Terminal.WriteLine("ACCESS_GRANTED");
    }

    void ShowTryAgainScreen()
    {
        Terminal.ClearScreen();
        Terminal.WriteLine("ACCESS_DENIED");
        Terminal.WriteLine("\n\n\n\n\n\n             ACCESS_DENIED");
        Terminal.WriteLine("\n\nPlease try again.\nSecurity here is awful.\nWe won't stop you.");
    }

    // ########## -- END OF CLASS -- ########## //
}

1 Like

Updated the game –

Privacy & Terms