Added Points System (3 guesses to win)

I have no idea what I am doing. I just started “learning” programming a few hours ago. I’ve binge watched a lot of these videos and have just finished the Terminal Hacker set. I tend to learn best when I modify things to suit my own needs, so I decided I wasn’t happy with answering a single question and winning the game… so I added a simple points system that requires 3 correct guesses to win.

I’m sure there’s a better way to do this; as I said, I have no idea what I’m doing so I just took a guess at how this could work.

First I added “int points” up top. Then I changed my win condition to increase “points” by 1 when a correct answer was given, instead of going to the win screen. To get to the win screen, I added a line to the password screen that requires 3 points to trigger. Lastly, I found that typing “menu” was causing problems because it wasn’t resetting the points. So after winning once, you’d immediately win again as soon as you tried to restart the game. I fixed this by just adding “points = 0” to the user input function.

I haven’t cleaned up my code yet, and I haven’t finished tinkering with the passwords or the artwork for winning… but I wanted to post this anyway.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hacker : MonoBehaviour {

// Game Config
const string menuHint = "Type 'menu' to return to the start.";
string[] level1Pass = { "signal", "text", "data", "number", "hotspot" };
string[] level2Pass = { "virus", "desktop", "laptop", "monitor", "keyboard", "mousepad" };
string[] level3Pass = { "router", "ethernet", "firewall", "wireless" };

// Game States
int level;
int points; // ADDS A BASIC POINTS SYSTEM
enum Screen { Main, Password, Win };
Screen currentScreen;
string password;

// Use this for initialization
void Start ()
{
ShowMainMenu();
}

void OnUserInput(string input)
{
    if (input == "menu")
    {
        ShowMainMenu();
        points = 0; // RESETS POINTS TO 0 WHENEVER THE MAIN MENU IS ACCESSED
    }

    else if (currentScreen == Screen.Main)
        MainMenu(input);

    else if (currentScreen == Screen.Password)
        CheckPassword(input);
}

void MainMenu(string input)
{
    bool isValidLevel = (input == "1" || input == "2" || input == "3");

    if (isValidLevel)
    {
        level = int.Parse(input);
        InputPassword();
    }

    else
    {
        Terminal.WriteLine("Please choose a valid level");
    }
}

void ShowMainMenu()
{
    currentScreen = Screen.Main;
    Terminal.ClearScreen();
    Terminal.WriteLine("Select your hacking target.");
    Terminal.WriteLine("");
    Terminal.WriteLine("Press 1 for cellphone");
    Terminal.WriteLine("Press 2 for computer");
    Terminal.WriteLine("Press 3 for network");
    Terminal.WriteLine("");
    Terminal.WriteLine("Enter selection: ");
}

void InputPassword()
{
    currentScreen = Screen.Password;
    Terminal.ClearScreen();

    SetPassword();

    Terminal.WriteLine(menuHint);
    Terminal.WriteLine("");
    Terminal.WriteLine("Please enter the password.");
    Terminal.WriteLine("Hint: " + password.Anagram());

    if (points == 3) // SETS A REQUIREMENT OF THREE CORRECT GUESSES BEFORE LOADING THE WIN FUNCTION
    {
        WinScreen();
    }
}

void SetPassword()
{
    switch (level)
    {
        case 1:
            password = level1Pass[Random.Range(0, level1Pass.Length)];
            break;
        case 2:
            password = level2Pass[Random.Range(0, level2Pass.Length)];
            break;
        case 3:
            password = level3Pass[Random.Range(0, level3Pass.Length)];
            break;
        default:
            Debug.LogError("Invalid Level");
            break;
    }
}

void CheckPassword(string input)
{
    if (input == password)
    {
        points++; // INCREASES THE CURRENT POINTS BY +1 EACH TIME A CORRECT ANSWER IS GIVEN
        InputPassword(); // SELECTS A NEW WORD TO UNSCRAMBLE
    }

    else
    {
        InputPassword();
    }
}

void WinScreen()
{
    currentScreen = Screen.Win;
    Terminal.ClearScreen();
    Terminal.WriteLine("Hacking successful!");
    Terminal.WriteLine("");
    Terminal.WriteLine(menuHint);
}

}

Looks good to me. That’s pretty much how you learn and this is also said in the course. You want to add a feature, you need to think about it how that would need to go in code, and then you write it and test to see if it actually works. And then tweak it if it doesnt.

1 Like

I actually ended up not being happy with the “3 correct guesses to win” system… so I added a few other things;

a) A ‘Lose’ scene based on a timer. You have 60 seconds to solve three answers or your hack fails and you have to start over.
b) A “high score” system where your completion time is displayed if you win.
c) Dynamic text that updates with your current score (couldn’t figure out how to do this with the timer, unfortunately).

I’m sure there are better ways to do what I did, but I didn’t want to Google around too much for things that haven’t been covered yet (exception being Time.deltaTime because I really, really wanted a timer and that was easy enough to search for).

Making these simple changes not only added more depth and replay value to the game, but also helped me better understand some of the functions that have been covered so far in the course. Win for the players AND the developer.

I think if you want a dynamic timer you need to write out / refresh the screen every time. So instead of working under start you need to have the code that writes the timer (and everything else that you want on the screen at that stage under update() rather than start() and onuserinput().

As the course has you write basically everything under on userinput means the screen refreshes only when you press enter (and depending on what the string that the player typed do some actions).

Update(){ }actually runs the code within it each “frame”. The Update line is standard when you make a script in unity, but i think it is removed somwhere during the course.

Privacy & Terms