Solved the challenge + really enjoyed it!

So I solved the challenge it was very fun! it took me about an hour or so but I got it! The way I did it was I created a new function that stores the passwords, added an if else statement within the ‘OnUserInput’ message checking if the ‘currentScreen’ enum variable is set to the password screen the user can then input a password. Within the password function there is logic using if and else statements to check if the user has inputted the correct details. The program also knows which level we are on based on the level variable also stored within the password function(similar to what is in the ‘runMainMenu’ function). I also created a win screen when the user enters the correct password. The tryAgain" function prompts the user to try again if the user entered an incorrect password. I also added a “playAgain” function bringing you back to the main menu if you have completed the game. Overall the challenge was good it gave me a good understanding of enums and how a program flows, the only problem I would like to try solve is when we are within the “tryAgain” function the user can type a password to try again if correct—> win screen, if incorrect —> nothing when you press enter on keyboard the text goes blank and you can enter another guess however I would like to prompt the user saying something along the lines of “Incorrect password 2 more attempts” something like that I think would be cool but couldn’t think of how to do it. Anyway here is my code!

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

public class Hacker : MonoBehaviour {

//Game State
int level;
enum Screen { MainMenu, PasswordScreen, WinScreen}
Screen currentScreen;



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

void showMainMenu()
{
    Terminal.ClearScreen();
    currentScreen = Screen.MainMenu;
    Terminal.WriteLine(" Select from one of the following to hack into.");
    Terminal.WriteLine(" ");
    Terminal.WriteLine(" 1. State Exams Commission ");
    Terminal.WriteLine(" 2. Facebook ");
    Terminal.WriteLine(" 3. The Pentagon ");
    Terminal.WriteLine(" Enter Selection: ");
 }

void OnUserInput(string input)
{

    if (input == "menu")
    {
     
        showMainMenu();
        
    }

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

    else if(currentScreen == Screen.PasswordScreen)
    {
        passwords(input);
    }

    else if(currentScreen == Screen.WinScreen)
    {
        playAgain(input);
    }
} 

    void RunMainMenu(string userInput) { 
    {

        if (userInput == "1")
        {
            level = 1;
            startGame();
        }

        else if (userInput == "2")
        {
            level = 2;
            startGame();
        }

        else if (userInput == "3")
        {
            level = 3;
            startGame();
        }

        else if (userInput == "Conor")
        {
            Terminal.WriteLine("Welcome Master Conor");
        }
        else
        {
            Terminal.WriteLine("Select a valid input");
        }
    }
}

void passwords(string passwordInput)
{
    if (passwordInput == "Conor")
    {
        level = 1;
        win();
    }

   else if (passwordInput == "Darragh")
    {
        level = 2;
        win();
    }

    else if (passwordInput == "Saoirse")
    {
        level = 3;
        win();
    }

    else
    {
    
        Terminal.ClearScreen();
        tryAgain();
    }

}

void startGame()
{
    currentScreen = Screen.PasswordScreen;
    Terminal.WriteLine(" You have chosen level: " + level);
    Terminal.WriteLine(" Please Enter a Password: ");
}

void tryAgain()
{
    currentScreen = Screen.PasswordScreen;
    Terminal.WriteLine(" Incorrect Password please try Again");
    Terminal.WriteLine(" You are on level: " + level);
    Terminal.WriteLine(" Please Enter a Password: ");

}

void win()
{
    Terminal.ClearScreen();
    currentScreen = Screen.WinScreen;
    Terminal.WriteLine("Correct you Win!");
    Terminal.WriteLine("To play again Press d");    
}

void playAgain(string playInput)
{
    if (playInput == "d")
    {
        showMainMenu();
    }
    else
    {
        Terminal.WriteLine(" woops try pressing d ");
    }
}

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

}

}
`

Privacy & Terms