Solo Challenge

Hey everyone,

So I managed to achieve the challenge somehow by proceeding with the same steps we used for the MainMenu. I must also acknowledge the fact that I got a little hint on how to make all passwords work by only their own levels thanks to a friend at work.

This challenge was complicated since I’m relatively new to Unity and C# and it gave me a hard time!

So here’s the code and I’ll explain my steps just below:

void OnUserInput(string input)
    {
        if (input == "menu")
        {
            ShowMainMenu(); // calls void start() { ShowMainMenu() }
        } 
        else if (currentScreen == Screen.MainMenu)
        {
            RunMainMenu(input);
        }
        else if (currentScreen == Screen.Password)
        {
            RunPassword(input);
        }

    }

    void RunPassword(string input) // responsible to run the levels and passwords
    {
        if (level == 1)
        {
            GameLevel(input, level);            
        }
        else if (level == 2)
        {
            GameLevel(input, level);
        }
        else if (level == 3)
        {
            GameLevel(input, level);
        }
    }

    void RunMainMenu(string input) // responsible to run the main menu
    {
        if (input == "007")
        {
            Terminal.WriteLine("Welcome back Mister Bond!");
        }
        else if (input == "666")
        {
            Terminal.WriteLine("Satan, really?");
        }
        else if (input == "777")
        {
            Terminal.WriteLine("Jackpot!");
        }
        else if (input == "1")
        {
            level = 1;
            StartGame();
            Terminal.WriteLine("Hint: zcugebrerk");
        }
        else if (input == "2")
        {
            level = 2;
            StartGame();
            Terminal.WriteLine("Hint: sutbifo");
        }
        else if (input == "3")
        {
            level = 3;
            StartGame();
            Terminal.WriteLine("Hint: skotc");
        }
        else
        {
            Terminal.ClearScreen();
            Terminal.WriteLine("Please select a correct answer!");
        }
    }

    void StartGame() // calls StartGame(); from OnUserInput
    {
        Terminal.ClearScreen();
        currentScreen = Screen.Password; // levels 1-3 are defined as Password (call to the enum function)
        Terminal.WriteLine("You chose level " + level);
        Terminal.WriteLine("Please enter your password:" + nl);
    }

    void GameLevel(string guess, int lvl) // added a second variable which is lvl in order for the password to only work in their respective level
    {
        if (guess == "zuckerberg" & lvl == 1) // use logical operator & (and) to use 2 variables
        {
            Terminal.WriteLine("Congratulation!");
        }
        else if (guess == "ubisoft" & lvl == 2)
        {
            Terminal.WriteLine("Congratulation!");
        }
        else if (guess == "stock" & lvl == 3)
        {
            Terminal.WriteLine("Congratulation!");
        }
        else Terminal.WriteLine("Try Again!");
    }

Explanations:

First I made an if statement (in void OnUserInput(string input){ }) for each level with a GameLevel containing 2 variables (input, level) which is calling a method (void GameLevel(string guess, int lvl){ })

Then I created that method (GameLevel) in which I defined both the password and the level:
e.g: if (guess “zuckerberg” & lvl == 1),
in which I also stated what happened if the user succeeds : Terminal.Writeline(“Congratulation!”);
and then what would happen if he fails: else Terminal.Writeline(“Try Again!”);

Once this is done the game should work just fine but the code is a little messy. That’s why I re-factored the code which was inside OnUserInput (the same way we learned inside the refactoring course video). Therefor we have: else if (currentScreen == Screen.Password) { RunPassword(input);} and a void method called: void RunPassword(input){ })

I hope this wasn’t too difficult to understand, I’m not very good with coding and I also tend to forget what each thing is called (method, statement,…).

I wish all of you a nice day and stop by to ask questions if you have some :smile: :sunny:

2 Likes

This is helpful, I am trying to get this working by looking at other student examples before I get the answer from the video. I need to work on structuring better code.

Privacy & Terms