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); }
}