Need a little help

For some reason, when choosing 1, 2, or 3 for the level, I get that extra line that says “Please choose a valid level” even though I am choosing a valid level. I checked over my code and don’t see anything wrong with the code.

image

Here is my code:

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

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

    void ShowMainMenu()
    {
        currentScreen = Screen.MainMenu;
        Terminal.ClearScreen();
        Terminal.WriteLine("Welcome to the Hacker Challenge Game");
        Terminal.WriteLine("Choose 1 for Penny's Piggy Bank");
        Terminal.WriteLine("Choose 2 for Sherlock Detective Agency");
        Terminal.WriteLine("Choose 3 for Roswell Computer System");
        Terminal.WriteLine("Enter your selection: ");
    }

    void OnUserInput(string input)
    {
        if (input == "menu") //we can always go direct to main menu
        {
            ShowMainMenu();
        }
        else if (currentScreen == Screen.MainMenu)
        {
            RunMainMenu(input);
        }
        else if (currentScreen == Screen.Password)
        {
            CheckPassword(input);
        }
    }
    void RunMainMenu(string input)
    {
        if (input == "1")
        {
            level = 1;
            password = level1Passwords[2]; //todo make random later
            StartGame();
        }
        else if (input == "2")
        {
            level = 2;
            password = level2Passwords[3];
            StartGame();
        }
        if (input == "3")
        {
            level = 3;
            password = level3Passwords[2]; 
            StartGame();
        }
        else if (input == "007")
        {
            Terminal.WriteLine("Please select a level Mr. Bond!");
        }
        else
        {
            Terminal.WriteLine("Please choose a valid level");
        }
    }

    void StartGame()
    {
        currentScreen = Screen.Password;
        Terminal.WriteLine("You have chosen level " + level);
        Terminal.WriteLine("Please enter your password  ");
    }
    void CheckPassword(string input)
    {
        if (input == password)
        {
            Terminal.WriteLine("Congratulations!");
        }
        else
        {
            Terminal.WriteLine("Incorrect. Try Again?");
        }
    }
}

Hi,

In your RunMainMenu method you have and if followed by an else if, so that’s one set of conditions. Then you start again with another if, else if and finally and else. This creates a separate set of conditions.

So in the case of your input being “1”, your input is caught in the very first if condition and that code runs, the else if doesn’t. Then, you move into the second if statement, the input isn’t “3” so it runs the else if, the input isn’t “007” so it executes the code in your else statement, which produces the “Please choose a valid level” text.

To resolve this issue, change your second if to an else if, so the full code would read;

if (input == "1")
{
	level = 1;
	password = level1Passwords[2]; //todo make random later
	StartGame();
}
else if (input == "2")
{
	level = 2;
	password = level2Passwords[3];
	StartGame();
}
else if (input == "3")
{
	level = 3;
	password = level3Passwords[2]; 
	StartGame();
}
else if (input == "007")
{
	Terminal.WriteLine("Please select a level Mr. Bond!");
}
else
{
	Terminal.WriteLine("Please choose a valid level");
}

Hope this helps :slight_smile:


See also;

Thanks Rob

I was thinking there was something wrong with the statements there – just couldn’t quite put my finger on it. I thought I had looked over Ben’s code carefully but apparently, I still missed it.

1 Like

You’re very welcome Penny :slight_smile:

1 Like

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

Privacy & Terms