About Terminal Hacker problem

This is my code and this looks fine I think. But I tried to play it after switch if lesson. When I played it , it asked passwords there is no problem. But after I wrote Menu and pick one of the 1-2-3 sections it will give “oh no that is false” message that I wrote in the code. Is there any problem in my code ? Or do you guys know why is that happening ? Thanks.


public class Hacker : MonoBehaviour
{
    string[] level1Passwords = {"kolay","matematik","kablo","uzaylı","gözlük" };
    string[] level2Passwords = { "patates", "sentetik", "dilemma", "diferansiyel", "tantana" };
    string[] level3Passwords = { "karbüratör", "laboratuvar", "pestenkerani", "profiterol", "muvaffakiyetsizleştiricileştiriveremeyebileceklerimizdenmişsinizcesine" };

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

    void Start(){


        ShowMainMenu();
        
    }
    void ShowMainMenu() {
        Terminal.ClearScreen();
        Terminal.WriteLine("Her şey TDK'deki kelimelerin harflerinin değiştirilmesi ile başladı.");
        Terminal.WriteLine("Kürtlerin başlangıç hamleleri bunlarmış ve fark edemedik.");
        Terminal.WriteLine("Kürd Land in kurulmasını engellemek için elimizden geleni yapmalıyız.");
        Terminal.WriteLine("Lütfen arşivdeki kelimeleri düzelt.");
        Terminal.WriteLine("Arsiv 1 icin 1 e bas.");
        Terminal.WriteLine("Arsiv 2 icin 2 e bas.");
        Terminal.WriteLine("Arsiv 3 icin 3 e bas.");

    }

    void OnUserInput(string input)
    {
        if (input == "Menu")
        {
            ShowMainMenu();
        }
        else if(currentScreen == Screen.MainMenu)
        {
            RunMainMenu(input);
        }
        else if (currentScreen == Screen.Password)
        {
            CheckPassword(input);
        }
    }
    void RunMainMenu(string input)
        {
        bool isValidLevelNumber = (input == "1" || input == "2" || input == "3");
        if (isValidLevelNumber)
        {

            level = int.Parse(input);
            StartGame();
        }
            
        else if (input == "007")
            {
                Terminal.WriteLine("James Bond şakası he, hiç eskimez.");
            }
        else if (input == "Kürdistan")
            {
                Terminal.WriteLine("K'yı büyük yazmışsın hata mesajı almayasın sonra ?");
            }
        else if (input == "31")
            {
                Terminal.WriteLine("Gerçekten mi ?");
            }
        else
            {
                Terminal.WriteLine("Lütfen arsivlerden birini seçin.");
            }
        }

        

    
    void StartGame()
    {
        print(level1Passwords.Length);
        print(level2Passwords.Length);
        print(level3Passwords.Length);
        currentScreen = Screen.Password;
        Terminal.ClearScreen();
        switch(level)
        {
            case 1:
                password = level1Passwords[0];

                break;
            case 2:
                password = level2Passwords[0];
                break;
            case 3:
                password = level3Passwords[0];
                break;
            default:
                Debug.LogError("Bilgisayar imha başlatılıyor.");
                break;

        }
           
        Terminal.WriteLine("Lütfen şifreyi girin:");
    }
        void CheckPassword(string input)
    {
        Debug.Log("Password: " + password);
        Debug.Log("Input: " + input);
        if (input == password)
        {
            Terminal.WriteLine("Congrats!!");
        }
        else
        {
            Terminal.WriteLine("Oh no that is false");
        }
        
    }
}

Hi Kepiti,

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

Add the following to your CheckPassword method:

Debug.Log("Password: " + password);
Debug.Log("Input: " + input);

Play your game and check the console. Maybe password does not have the expected value.


See also:

I couldn’t check my code with Lecture Project Changes because I couldn’t find the whole code has written from now on. (But I tried to look on the video and there was no difference .)Also I added these codes to my CheckPassword but it didn’t work either.

What do you mean by that? Did you get an error message?

The code I suggested does not fix anything. It logs information in your console. What is the output in your console?

The output was the same “oh no that was false” that I write for wrong password. It hapens after I choose 1-2-3 sections and go back to Menu section . On the right in debug it looks my current screen stuck in password section

Please disable “Collapse” in the Console, so you can see the messages in the correct order.

If you want to change the “screen”, assign a different value to currentScreen. At the moment, your code shows only this: currentScreen = Screen.Password;. And since this never seems to change in your code, your game will always remain in the password checking state.

Thanks for your hard work on me Nina. But I couldn’t understand one thing.
My code never changes ? Do you have any solution for me to get out of this problem ? Like delete these lines and change them with bla bla or sth like that ? I am new at coding and I cannot understand what you are saying properly . Thanks again .

Let’s recapitulate what your code is supposed to do when you start your game. The first I would do is to declare a default state at the top of the code: Screen currentScreen = Screen.MainMenu;

Alternatively, you could do this in Start:

void Start(){
    currentScreen = Screen.MainMenu;
    ShowMainMenu();      
}

This way, we ensure that the game always starts with the correct state/screen.

Your code calls ShowMainMenu(), the text gets displayed.

Is that clear so far?


Fine. When you type something, OnUserInput gets called. Assuming you typed “2” (like in your screenshot), the first else if gets executed because we set the currentScreen to Screen.MainMenu in Start.

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

RunMainMenu gets called. Since input is “2”, isValidLevelNumber becomes true. The if-block gets executed. level gets set to 2, then StartGame() gets called.


In StartGame, your code sets currentScreen = Screen.Password;. With the switch, we determine a password based on our level value, which is 2. The password value is “patates” now. “Lütfen şifreyi girin:” gets displayed on your terminal screen.


When you type something again, e.g. “patates”, OnUserInput gets called. This time, the second else-if-block gets executed because currentScreen == Screen.Password.

Now CheckPassword gets called. input == password is true, thus the if-block gets executed.


What is supposed to happen then? You don’t have a Win screen or method yet. Since currentScreen does not get set back to Screen.MainMenu, the program will continue checking whether your input matches the current password. That’s fine so far because Ben and Rick implement a win screen in the next lectures.

If you understood the concept and if assigning a default value to currentScreen did not fix the issue, take another look at your code and check if it matches the concept I tried to describe here. I haven’t spotted any issue in your code, but it might well be that I simply missed it.

1 Like

Thanks for abosuletly insane explanation Nina ! Before getting the message from you I tried to change something and it matches what you said in this text . (some of them :smiley:)
Thanks again I appreciate your effort on me thank you so much <3

I’m glad that I was able to help. :slight_smile:

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

Privacy & Terms