Adding Hints

Had issues with index but i realized it was because I was keeping Int index on all 3 levels, changed it to int index1/2 and it seems to work for now.

This part of the course seemed to click with me so I decided to add in a hints option on the 3rd level as some of the words are ridiculously hard.

   switch (level)
    {
        case 1:
            int index = Random.Range(0, 8);
            password = level1Passwords[index];
            break;
        case 2:
            int index1 = Random.Range(0, 9);
            password = level2Passwords[index1];
            break;
        case 3:
            int index2 = Random.Range(0, 8);
            password = level3Passwords[index2];
            Hint = level3Hints[index2];
            break;
        default:
            Debug.LogError("Ivalid Level Number");
            break;
    }
    Terminal.WriteLine("Please enter the password: ");
    Terminal.WriteLine("");
    if (level == 3)
    {
        Terminal.WriteLine("Type hint if you are stuck");
        Terminal.WriteLine("");
    }`
1 Like

just watched the next part of the video, and saw the solution to the index problem…

I feel as if I still have to set level 3 passwords as an int index to make sure both hint and password are the same value

 void StartGame()
    {
        currentScreen = Screen.Password;
        Terminal.ClearScreen();
        switch (level)
        {
            case 1:
                password = level1Passwords[Random.Range(0, 8)];
                break;
            case 2:
                password = level2Passwords[Random.Range(0, 9)];
                break;
            case 3:
                int index = Random.Range(0, 8);
                password = level3Passwords[index];
                Hint = level3Hints[index];
                break;
            default:
                Debug.LogError("Ivalid Level Number");
                break;
        }
        Terminal.WriteLine("Please enter the password: ");
        Terminal.WriteLine("");
        if (level == 3)
        {
            Terminal.WriteLine("Type hint if you are stuck");
            Terminal.WriteLine("");
        }
    }
1 Like

Yup, since you made the data set in “pairs” that’s going to be required. One thing that is probably in the next lecture, but I’m noticing it so I’ll mention it: you can make the code more robust with a small change, such as:

            case 1:
                password = level1Passwords[Random.Range(0, level1Passwords.Length)];
                break;

This allows you to add/remove list members, without getting errors such as index out of range, or the omission of members above the “magic-number” range you specify for each case.

My next suggestion is to ignore the above and just continue the course; as I say, I’m pretty sure you’ll be covering it or something similar soon enough. By the same token, ignore the following comments as well (lol) because I feel compelled to say a word about your level 3 “issue” where you need to assign the temporary int index variable to make use of it twice…

Firstly, there is nothing wrong with doing it the way you’ve done it. While it leaves you with the need to maintain the two independent lists, yet synchronously, it’s only two small lists so it’s what we call, “Not a big deal.” BUT, you could do something like use a struct or a class that can contain both the password and the hint together, providing an improved readability/maintainability for the data structures, perhaps…

I’m just winging it here, but your case 3: could be something like:

  case 3:
     MetaData thisMeta = GetMetaThree();
     password = thisMeta.password;
     hint = thisMeta.hint;
     break;

It would require a bit of structure underlying it, but if you were planning to do much in the way of extending the variety of passwords, I think it would pay-off because it’s really bloody obvious what data goes where:

   private class MetaData
   {
      public string password = "defaultPassword";
      public string hint = "defaultHint";
   }

   private const int LEVEL_3_MEMBER_COUNT = 3;
   private MetaData[] levelThreeMeta = new MetaData[LEVEL_3_MEMBER_COUNT];

   private void InitLevelThreeMeta()
   {
      levelThreeMeta[0].password = "discombobulated";
      levelThreeMeta[0].hint = "John didn't want to be LATE; he quickly COMBed his office for the DISC.";

      levelThreeMeta[1].password = "password2";
      levelThreeMeta[1].hint = "hint2";

      levelThreeMeta[2].password = "password3";
      levelThreeMeta[2].hint = "hint3";
   }

   private MetaData GetMetaThree()
   {
      int index = Random.Range(0, LEVEL_3_MEMBER_COUNT);
      MetaData metadata = new MetaData();
      metadata.password = levelThreeMeta[index].password;
      metadata.hint = levelThreeMeta[index].hint;
      return metadata;
   }

   private void Start()
   {
      InitLevelThreeMeta();
   }

Of course, as you will soon learn, there are a million and a half ways to do anything when it comes to programming; I have no doubt there are far better ways to manage your password/hint pairs, but this is what came to mind when I read your post.

2 Likes

Thanks for the reply.

  case 1:
                password = level1Passwords[Random.Range(0, level1Passwords.Length)];
                break;

Makes sense, it was covered in the same episode but I didn’t change it over. Makes sense to do it just as future proofing though.

As for level 3 hints I see how you have done it and understand (i think) how it works. I will try to incorporate your code into a version and see if I can get it working. It’s interesting to see other methods on how it can work and is useful knowing other option so thanks for the input.

In case anyone is searching for help with adding hints I just want to update this section including the version of code I used for the hints section.

All the sections added that has anything to do with hints has a //COMMENT. next to it for easier reference and a quick explanation as to what it should do.

using UnityEngine;

public class Hacker : MonoBehaviour
{
    // Game config
    string[] level1Passwords = { "rome", "london", "paris", "dublin", "tokyo", "berlin", "madrid", "moscow" }; 
    string[] level2Passwords = { "baghdad", "budapest", "zagreb", "prague", "havana", "canberra", "ottawa", "beijing", "reykjavik" }; 
    string[] level3Passwords = { "yamoussoukro", "yaounde", "gaborone", "thimphu", "belmopan", "tegucigalpa", "antananarivo", "nouakchott" }; 
    string[] level3Hints = { "Cote d'Ivoire", "Cameroon", "Botswana", "Bhutan", "Belize", "Honduras", "Madagascar", "Mauritania" }; // ANSWERS IN ORDER 0-7

    int level;
    enum Screen { MainMenu, Password, Win }
    Screen currentScreen;
    string password;
    string hint; // SETS UP HINT

    // Use this for initialization
    void Start()
    {
        ShowMainMenu();
    }
    void ShowMainMenu()
    {
        currentScreen = Screen.MainMenu;
        Terminal.ClearScreen();
        Terminal.WriteLine("            Capital Cities          ");
        Terminal.WriteLine("======================================");
        Terminal.WriteLine("      Hello! Hola! Bonjour! Ciao! ");
        Terminal.WriteLine("======================================");
        Terminal.WriteLine("");
        Terminal.WriteLine("1 = Well known Capitals.");
        Terminal.WriteLine("2 = Obscure Capitals.");
        Terminal.WriteLine("3 = 'Where is that?' Capitals (V.HARD)");
        Terminal.WriteLine("");
        Terminal.WriteLine("Please pick a difficulty and hit enter");
    }
    void OnUserInput(string input)
    {
        if (input == "menu")
        {
            ShowMainMenu();
            level = 0;
        }
        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);
            AskForPassword();
        }
        else if (input == "hello")
        {
            Terminal.WriteLine("Thanks for playing");
        }
        else if (input == "hola")
        {
            Terminal.WriteLine("gracias por jugar");
        }
        else if (input == "bonjour")
        {
            Terminal.WriteLine("Merci d'avoir joué");
        }
        else if (input == "ciao")
        {
            Terminal.WriteLine("grazie per aver giocato");
        }
        else if (input == "Where is Carmen San Diego")
        {
            Terminal.WriteLine("Hey Lucas! Thanks for the support!");
        }
        else
        {
            Terminal.WriteLine("Please pick a level");
        }
    }
    void CheckPassword(string input)
    {
        if (input == password)
        {
            DisplayWinScreen();
        }
        else if (input == "hint") // ELSE IF ON PASSWORD ATTEMPT - TODO LOCK TO LEVEL 3
        {
            Terminal.WriteLine("The capital of " + hint); //WILL DISPLAY AFTER HINT IS INPUT - hint IS LATER USED IN INDEX TO MATCH PASSWORD 
        }
        else
        {
            AskForPassword();
        }
    }
    void DisplayWinScreen()
    {
        currentScreen = Screen.Win;
        Terminal.ClearScreen();
        ShowLevelReward();
        Terminal.WriteLine("Type menu to return to start");

    }
    void ShowLevelReward()
    {
        switch (level)
        {
            case 1:
            Terminal.WriteLine("Congratulations!");
            Terminal.WriteLine("I see you have a Passport!");
            Terminal.WriteLine(@"
     ______________________
    |  Mr Traveller   |[xx]|
    |  =-=-=-=-=-=-===|(< )|
    |  ---==------====|_`\||
    |     My passport      |
    | ---==-=---=-=-=--=-- |
    |______________________|");
            break;
            case 2:
                Terminal.WriteLine("Wow, Well Done!");
                Terminal.WriteLine("You must travel a lot!");
                Terminal.WriteLine(@"
    _________-==-____________  
   /___=/___//__\\_____=/___/|
   |                        ||
   |      My Suitcase!      ||
   |                        ||
   |                        ||
   |________________________|/");
            break;
            case 3:
                Terminal.WriteLine("AMAZING! Do you have a private jet?");
                Terminal.WriteLine(@"
_______________.__
         _      |#~\     
 o o o  | |      |Q_\_ _
 . . .  |_|. . . .    ` \            
=======|---|============ )
_______|---|____________/
       |---|    ||
       |---|    () ");
                break;

        }
    }
    void AskForPassword()
    {
        currentScreen = Screen.Password;
        Terminal.ClearScreen();
        SetRandomPassword();
        Terminal.WriteLine("Type menu to return to start");
        Terminal.WriteLine("");
        Terminal.WriteLine("Please enter the password: ");
        Terminal.WriteLine(password.Anagram());
        Terminal.WriteLine("");
        if (level == 3)
        {
            Terminal.WriteLine("Type hint if you are stuck"); // ADDS IN HINT OPTION ON LEVEL 3 
            Terminal.WriteLine("");
        }
    }

    void SetRandomPassword()
    {
        switch (level)
        {
            case 1:
                password = level1Passwords[Random.Range(0, level1Passwords.Length)];
                break;
            case 2:
                password = level2Passwords[Random.Range(0, level2Passwords.Length)];
                break;
            case 3:
                int index = Random.Range(0, level3Passwords.Length);
                password = level3Passwords[index];
                hint = level3Hints[index]; //CALLS FROM LIST ON TOP, USES SAME NUMBER FROM INDEX. IMPORTANT HINTS IN RIGHT ORDER
                break;
            default:
                Debug.LogError("Ivalid Level Number");
                break;
        }
    }
}

Privacy & Terms