I already spent too much time on this challange so I’m not going to deal with the duplicate hints.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hacker : MonoBehaviour
{
// Game State
int level;
string playerName;
string password;
List<string> hints = new List<string>();
int hintsCount;
string[,] passArray = new string[,] {
{ "book", "shelf", "archive", "study", "reading"},
{ "officer", "uniform", "storeroom", "detective", "sergeant" },
{ "astronaut", "astronomy", "universe", "andromeda", "telescope" }
};
enum Screen { Intro, MainMenu, Password, Win };
Screen currentScreen;
// Start is called before the first frame update
void Start()
{
ShowIntroScreen();
}
void ShowMainMenu(string name)
{
currentScreen = Screen.MainMenu;
Terminal.ClearScreen();
Terminal.WriteLine("Hello " + name + "\n");
Terminal.WriteLine("What would you like to hack into?\n");
Terminal.WriteLine("Press 1 for the local library");
Terminal.WriteLine("Press 2 for the police station");
Terminal.WriteLine("Press 3 for NASA");
Terminal.WriteLine("Type \"name\" to change your name");
Terminal.WriteLine("Type \"menu\" to reach menu\n");
Terminal.WriteLine("Enter your selection:");
}
void OnUserInput(string input)
{
// Handle null input
if (input == null)
{
input = "";
}
if (currentScreen != Screen.Intro)
{
if (input == "menu") // we can always go direct to main menu
{
ShowMainMenu(playerName);
}
else if (currentScreen == Screen.MainMenu)
{
RunMainMenu(input);
}
else if (currentScreen == Screen.Password)
{
if (input == "hint")
{
Terminal.WriteLine(GetHint());
}
else
{
CheckPassword(input);
}
}
else
{
Terminal.WriteLine("I can't recognize the \"" + input + "\" command");
}
}
else
{
GetPlayerName(input);
}
}
private void ShowIntroScreen()
{
currentScreen = Screen.Intro;
Terminal.ClearScreen();
Terminal.WriteLine("Please introduce yourself!\nType your name then press \"enter\"");
}
void GetPlayerName(string input)
{
if (input.Length >= 3)
{
playerName = input;
ShowMainMenu(playerName);
}
else
{
Terminal.WriteLine("Please choose a name with at least 3 characters!");
}
}
void RunMainMenu(string input)
{
switch (input)
{
case "1":
level = 1;
StartGame();
break;
case "2":
level = 2;
StartGame();
break;
case "3":
level = 3;
StartGame();
break;
case "007":
Terminal.WriteLine("Please select a level Mr Bond!");
break;
case "name":
ShowIntroScreen();
break;
default:
Terminal.WriteLine("Please choose a valid level!");
break;
}
}
private void StartGame()
{
currentScreen = Screen.Password;
hints.Clear();
GeneratePassword();
Terminal.WriteLine("You have choosen level " + level);
Terminal.WriteLine("Type: \"hint\" for help");
Terminal.WriteLine(GetHint());
}
private void GeneratePassword()
{
// Get the length of passArray 2nd dimension
int passArrayLength = passArray.GetLength(1);
// Get a random word from passwordArray[,]
password = passArray[level - 1, UnityEngine.Random.Range(0, passArrayLength)];
Shuffle(password);
hintsCount = hints.Count;
}
void Shuffle(string password)
{
char[] charArray = password.ToCharArray();
System.Random rnd = new System.Random();
int n = charArray.Length;
while (n > 1)
{
n--;
int k = rnd.Next(n + 1);
var value = charArray[k];
charArray[k] = charArray[n];
charArray[n] = value;
// record hints
string hint = new string(charArray);
Debug.Log(hint);
hints.Add(hint);
}
}
void CheckPassword(string input)
{
if (input.ToLower() == password.ToLower())
{
RunWinScreen();
}
else
{
Terminal.WriteLine("Wrong Password try again!");
Terminal.WriteLine("Type: \"hint\" for help");
}
}
void RunWinScreen()
{
currentScreen = Screen.Win;
Terminal.ClearScreen();
Terminal.WriteLine("Good Job " + playerName + "! You got it!\n");
Terminal.WriteLine("Type \"name\" to change your name");
Terminal.WriteLine("Type \"menu\" to reach main menu");
}
string GetHint()
{
hintsCount--;
if (hintsCount >= 0)
{
Terminal.WriteLine("You have " + hintsCount + " hints left");
return hints[hintsCount];
}
else
{
return "Sorry no more hints";
}
}
}