Says password incorrect on default

On my game when I select a level it just puts incorrect password right away, but when I enter the right password after that it will still say that is right, and if I enter the wrong password it will say it was incorrect again. It’s quite annoying.

Here is my code: https://pastebin.com/YywB5CDL

1 Like

Hi,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

Yes I have and it doesn’t look like there is anything different. I did add an incorrect password counter that displays the amount of incorrect password guesses, but I doubt that would cause the issue.

Okay so i took your code and implemented it on my project until i finally, Finally found the error xD. took about 30 mins straight lol.

The problem is in this part

bool isValidLevelNumber = (input == "1" || input == "2" || input == "3");
        if (isValidLevelNumber)
        {
            level = int.Parse(input);
            StartGame();
        }
       
        if (input == "Menu")
        {
            ShowMainMenu();
            currentScreen = Screen.MainMenu;
        }
       
        else if (input == "Method0901")
        {
            Terminal.WriteLine("Follow Method0901 on twitch: twitch.tv/METHOD0901");
            currentScreen = Screen.EasterEgg;
        }
       
        else if (currentScreen == Screen.Password)
        {
            Password();
        }
       
        else
        {
            Terminal.WriteLine("Please make a valid selection.");
        }
       

What happens here is that you check if the level is correct and if so you start the game but you don’t stop there. the code continues because he finds another if condition so it’s similar to

if(a == b) print(1);
if(b == c) print(2);

what will happen is that you’ll find in the console the output “12” have occurred because the code reads the whole block. but if we change it to

if(a == b) print(1);
else if(b == c) print(2);

you will find the first one which is “1” and print it.

Relating this to your code, you start the game but the code keeps going to the next If-else block which is from line 48~68… so it takes the same input and checks for both parts.

The solution is simply change line 48 from
if (input == "Menu")
to
else if (input == "Menu")

i know this was a lot to take in but i hope it was simple enough, Good luck throughout the project ")

1 Like

Thank you so much for your efforts! It worked! Surprising how simple that was :laughing:

1 Like

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

Privacy & Terms