Here's My Solution

This is my very first project, and I’m coming at this completely new-no coding experience whatsoever! I ended up bashing my head into this challenge until I got fairly close to a solution. The biggest issue I had was when I would go into either level 1 or 2, it would not only display the anagram to solve, but also two instances of my “failure” message. Even if I entered the correct answer, I would get one “success” message and one “failure” message. Additionally, whenever I would go back to the main menu, it would now display the regular menu plus two “failure” messages. I spoke to some friends and they steered me in the right direction. Firstly, I added some new cases to my enum to coincide with my levels 1 and 2, for clarity. Next, I added some new cases to my OnUserInput function to properly detect what screen the player was on. I was apparently getting the responses from multiple levels and scenarios to show at the same time. Finally, I had to change two instances of currentScreen from Password (redundant and pointed to the wrong thing) to the new parts of my enum, Level1 and Level2. Very challenging, but incredibly rewarding to have done it my way and then being able to compare it to the provided example from Ben. I went a bit further and made the screen clear at certain points so it would look neater for the player. At the end, I took some time to clean up the readability of the code by moving things around and deleting extra spacing. I’ve attached my code below (the spacing looks fine in VS and the game, I swear!) and I’d be happy to share the finished .exe file for the playable game if anyone is interested to see how my version worked in the end. Also, if anyone has any tips or suggestions on how to make this code more concise etc, please let me know!

edit: After moving on to the next lesson about arrays, I realize this code only works for one password per level.

using UnityEngine;

public class Hacker : MonoBehaviour
{   //game state
    int level;
    enum Screen { MainMenu, Level1, Level2, Win };
    Screen currentScreen;
    // Start is called before the first frame update
    void Start()
    {
        ShowMainMenu();
    }

    void ShowMainMenu()
    {
        Terminal.ClearScreen();
        Terminal.WriteLine("You have found two chests, but they are" + "locked with magical cyphers!");
        Terminal.WriteLine("Choose a chest to unlock:");
        Terminal.WriteLine("1. Easy Chest");
        Terminal.WriteLine("2. Challenging Chest");
        Terminal.WriteLine("Enter your choice:");
    }
    void StartGame()
    {
        Terminal.ClearScreen();
        Terminal.WriteLine("You chose chest #" + level + "!");
        Terminal.WriteLine("Unscramble to unlock:");
        Terminal.WriteLine("");

    }
    void OneWin()
    {
        Terminal.ClearScreen();
        Terminal.WriteLine("You opened the Easy Chest");
        Terminal.WriteLine("Claim your reward!");
        Terminal.WriteLine("*~*~*~*~*~*~*");
    }
    void TwoWin()
    {
        Terminal.ClearScreen();
        Terminal.WriteLine("You opened the Challenging chest-");
        Terminal.WriteLine("Claim your reward!");
        Terminal.WriteLine("*~*~*~*~*~*~*");
    }
   void Lose()
    {
        Terminal.WriteLine("Incorrect, please try again.");
    }
    void OnUserInput(string input)
    {
        if (input == "menu")
        {
            currentScreen = Screen.MainMenu;
            ShowMainMenu();
        }
        else
        {
            if (currentScreen == Screen.MainMenu)
            {
                RunMainMenu(input);
            }
            else if (currentScreen == Screen.Level1)
            {
                Level1(input);
            }
            else if (currentScreen == Screen.Level2)
            {
                Level2(input);
            }

        }
    }
        void Level1(string input)
        {
        currentScreen = Screen.Level1;
            if (input == "magic")
            {
                currentScreen = Screen.Win;
                OneWin();
            }
            else
            {
                Lose();
            }
        }
        void Level2(string input)
        {
        currentScreen = Screen.Level2;
            if (input == "treasure")
            {

                currentScreen = Screen.Win;
                TwoWin();
            }
            else
            {
                Lose();
            }
        }

        void RunMainMenu(string input)
        {
            if (input == "menu")
            {
                currentScreen = Screen.MainMenu;
                ShowMainMenu();
            }
            else if (currentScreen == Screen.MainMenu)
            {
                if (input == "1")
                {
                    currentScreen = Screen.Level1;
                    level = 1;
                    StartGame();
                    Terminal.WriteLine("gmcai");
                    Terminal.WriteLine("");

                }
                else if (input == "2")
                {
                    currentScreen = Screen.Level2;
                    level = 2;
                    StartGame();
                    Terminal.WriteLine("esretaru");
                    Terminal.WriteLine("");
                }
                else if (input == "69")
                {
                    Terminal.WriteLine("How clever of you.");
                }
                else
                {
                    Terminal.WriteLine("Please choose a chest.");
                }
            }
        }
    }
1 Like

Congrats! Your code looks really nice, pretty good refactor and your solution to the challenge was amazing!

If you want to make your code even clearer (which is hard because you already did an amazing job) try ordering all of your methods by order of who is calling who. For instance, your OnUserInput method is at the bottom of the script, try putting it right below your Start message/method, it may sound a little weird, but it makes everything easier to read.

Please, do share your final game, this is actually the biggest and more important thing, I can’t even express in words how important it is to share your games.

There are 3 main platforms to upload your games:



https://gamejolt.com/

Share my game is great for super easy uploads, but be careful, they only stay there for a month or so.
If you want to create a portfolio or simply upload your games for free and keep them there permanently, try using itch or gamejolt, this sites are a litte convoluted tho.

1 Like

Thanks for the encouraging words- they definitely makes me want to keep going! Ill definitely look into your reformatting idea, it sounds like it would make things nice and clean. Once I completely finish the game, I’ll post the .exe. Right now, it just covers one anagram per level.

1 Like

Privacy & Terms