Added limited tries but how to prevent further input?

So I decided to up a pressure a bit by limiting the input attempts. My code works fine but I’d like to prevent further input after the game is over. Any ideas on how I would go about this please?

using UnityEngine;

public class Hacker : MonoBehaviour
{
    // Game Configuration Data
    const string menuHint = "Type menu at any time to return.";
    string[] level1Passwords = { "beer", "wine", "chips", "pint" };
    string[] level2Passwords = { "hospital", "bovine", "feline", "animals" };
    string[] level3Passwords = { "extreme", "hardcore", "government", "investigate" };

    // Game State
    int level;
    int lives;
    string password;
    enum Screen { MainMenu, Password, Win, Lose };
    Screen currentScreen;

	// Use this for initialization
	void Start ()
    {
        ShowMainMenu ();
    }

    void ShowMainMenu ()
    {
        currentScreen = Screen.MainMenu;
        lives = 3;
        Terminal.ClearScreen();
        Terminal.WriteLine("What system would you like to hack?");
        Terminal.WriteLine("");
        Terminal.WriteLine("1. The pub");
        Terminal.WriteLine("2. The vet clinic");
        Terminal.WriteLine("3. The FBI Mainframe");
        Terminal.WriteLine("");
        Terminal.WriteLine("Select a number: ");
    }

    void OnUserInput(string input)
    {
        if (input == "menu")
        {
            ShowMainMenu();
        }
        else if (currentScreen == Screen.MainMenu)
        {
            RunMainMenu(input);
        }
        else if (currentScreen == Screen.Password)
        {
            RunPassword(input);
        }
        else if (currentScreen == Screen.Lose)
        {
            RunLose();
        }
        else if (currentScreen == Screen.Win)
        {
            RunWin(); 
        }
    }

    void RunMainMenu(string input)
    {
        bool isValidLevelNumber = (input == "1" || input == "2" || input == "3");
        if (isValidLevelNumber)
        {
            level = int.Parse(input);
            AskForPassword();
        }
        else
        {
            Terminal.WriteLine("Invalid Input!");
            Terminal.WriteLine(menuHint);
        }
    }

    void RunPassword(string input)
    {
        if (input == password)
        {
            RunWin();
        }
        else
        {
            lives = lives - 1;
            if (lives == 0)
            {
                RunLose();
            }
            else
            {
                AskForPassword();
            }
        }
    }

    void AskForPassword()
    {
        currentScreen = Screen.Password;
        Terminal.ClearScreen();
        Terminal.WriteLine("You have " + lives + " tries remaining!!");
        SetRandomPassword();
        Terminal.WriteLine("Enter your password, hint: " + password.Anagram());
        Terminal.WriteLine(menuHint);
    }

    void SetRandomPassword()
    {
        switch (level)
        {
            case 1:
                password = level1Passwords[Random.Range(0, level1Passwords.Length)];
                break;
            case 2:
                password = level2Passwords[Random.Range(0, level2Passwords.Length)];
                break;
            case 3:
                password = level3Passwords[Random.Range(0, level3Passwords.Length)];
                break;
            default:
                Debug.LogError("Invalid level number");
                break;
        }
    }

    void RunLose()
    {
        currentScreen = Screen.Lose;
        Terminal.ClearScreen();
        Terminal.WriteLine(@"


       __           __           
      / _ _ _  _   /  )   _ _ || 
     (__)(///)(-  (__/ \/(-/  ..  
                ");
    }

    void RunWin()
    {
        currentScreen = Screen.Win;
        Terminal.ClearScreen();
        ShowLevelReward();
        Terminal.WriteLine(menuHint);
    }

    void ShowLevelReward()
    {
        switch (level)
        {
            case 1:
                Terminal.WriteLine("Cheers!");
                Terminal.WriteLine(@"
.~~~~.
i====i_
|cccc|_)
|cccc|  
`-==-'
                ");
                break;
            case 2:
                Terminal.WriteLine("Meow!");
                Terminal.WriteLine(@"
 /\_/\
( o.o )
 > ^ <
                ");
                break;
            case 3:
                Terminal.WriteLine("Welcome to the FB Aight!");
                Terminal.WriteLine(@"
  __ _     _ 
 / _| |   (_)
| |_| |__  _ 
|  _| '_ \| |
| | | |_) | |
|_| |_.__/|_|
                ");
                break;
            default:
                Debug.LogError("Invalid level reward");
                break;
        }
        
    }
}
1 Like

If you prevent further input, how would the player play again?


See also;

They won’t. It’s a do or die game! :smiling_imp:

(Bad design I know but as it’s only testing I think it’s probably ok)

PS. Thanks for fixing my code block

Well, if that’s the case then, you could add a member variable to the Hacker class which will indicate whether it’s game over or not, perhaps;

public class Hacker : MonoBehaviour
{
    bool gameOver;

    // ...
}

Bools default to being false so we don’t need to worry about setting it specifically.

Then, in your OnUserInput method you could add a check around the existing code to test for the value of gameOver;

void OnUserInput(string input)
{
    if(gameOver == false)
    {
        // run existing code in here
    }
}

Then finally you need to set gameOver to be true in the relevant location(s), presumably at least the RunLose method, but potentially in your RunWin method also, depending on your level of evilness.

void RunLose()
{
    gameOver = true;

    // existing code
}

The above will prevent any of your game functionality from happening but will probably still allow the characters to be displayed on the screen when the player types, as this is being handled within the WM2000, the project which you imported. You can expand the WM2000 GameObject and you’ll see various child GameObjects, from memory there’s one called Display and one called Keyboard I think. Try disabling the Keyboard one and see if that prevents the user input.

If it does, you could then disable it via code.

private void GameOver()
{
    if(gameOver == false)
    {
        GameObject.FindObjectOfType<Keyboard>().gameObject.SetActive(false);
        gameOver = true;
    }
}

If you want to use that, you don’t really need to test gameOver in the OnUserInput method, as you won’t be getting any input anyway. Replace the gameOver = true; statement in RunLose with a call to the GameOver method.

Hope this helps. :slight_smile:

Disabling the keyboard object did it thanks Rob!

1 Like

No worries :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms