Code Help

I would like the Hacker game to run through all the scrambled words in each level before going to the “Win” screen. I’m trying to use a “for” loop, but I’m really not getting anywhere.
Hopefully I’m on the right path, but does anyone know how this could be done?
I guess i’m not sure where to actually place the loop.

1st I initialized a variable: win up at the top on line 10:

    bool win = false;

Then I changed the Ask For Password to include the for loop:

 void AskForPassword()
    {
        currentScreen = Screen.Password;
        Terminal.ClearScreen();
        for (int k = 0; k <= level01Passwords.Length; k++)
        {
            Password = level01Passwords[k];

            //SetPassword();
            Terminal.WriteLine(menuHint);
            Terminal.WriteLine(" ");
            Terminal.WriteLine("Level: " + level);
            Terminal.WriteLine("");
            Terminal.WriteLine("Enter your password. Hint: " + Password.Anagram());
            if (k == level01Passwords.Length)
                win = true;
    }

Lastly I tried to change the CheckPassword:

void CheckPassword(string input) 
    {
        if (input == Password) & (win == true)
        {
            DisplayWinScreen();
        }
        else
        {
            AskForPassword();
        }
    }

I’ve worked on this a while… any help would be greatly appreciated!!

Hi @aura0rus,

First of all, good job on challenging yourself. You are definitely on the right way with your approach. The only problem is that the AskForPassword method (or any method) does not wait for user input meaning the for-loop gets executed within milliseconds.

You have a control variable, which is a good start. The value of that variable must persist, though, thus you cannot use a local variable. Instead, use declare the variable at the top of your class.

I don’t know what exactly your idea is but you could increment the variable whenever the player guess a password. And the current password is determined by the value of the control variable.

This way, you don’t have to rely on any delays. Instead, you look up the data you need when you need it, and use a value of a “persistent” variable.

Did this help? :slight_smile:

Yes, that worked great. No for loop. I initialized a variable at the top, each correct answer increments it by 1. So easy. Variables initialized at top were:
wordNumber, listLength, lives.

Here is what i came up with… it works!

void AskForPassword()
    {
        currentScreen = Screen.Password;
        Terminal.ClearScreen();
        Password = level01Passwords[wordNumber];
        Terminal.WriteLine("You have " + lives + " tries left.");
        Terminal.WriteLine(menuHint);
        Terminal.WriteLine(" ");
        Terminal.WriteLine("Level: " + level);
        Terminal.WriteLine("");
        Terminal.WriteLine("Enter your password. Hint: " + Password.Anagram());              
    }

    void CheckPassword(string input)

    {
        if (input == Password)
        {
            if (wordNumber == listLength)
            {
                DisplayWinScreen();
            }
            else
            {
                wordNumber = wordNumber + 1;
                AskForPassword();
            }
        }
        else
        {
            lives = lives - 1;
            if (lives == 0)
            {
                Terminal.WriteLine("That was your last try, Please start over.");
                lives = 3;
                ShowMainMenu();
            }
            else
            {
                AskForPassword();
            }

            
        }
    }

Good job. I knew you would be able to solve the problem yourself. Keep it up! :slight_smile:

1 Like

Nina,
Thank you for your help, I’m really impressed. I’ve been on so many different forums where my question will sit for weeks… until I finally just move on to somewhere else. Your response was quick and just what I needed.

so far, the course is awesome… and the community wonderful to be part of.
Thanks again.

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

Privacy & Terms