Here is my code for the “Solo Challenge”. I have completed the challenge and the game is working properly, but I would still like to know if there is any scope of improvement.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class Hacker : MonoBehaviour
{
//Username
string gamingName;
//Game states
int level;
enum Screen {HomeScreen, MainMenuScreen, GamePlayScreen, WinScreen, TryAgainScreen};
Screen currentScreen;
//Passwords and Response Messages
string[] passwordsForLevels = {"LibraryPassword", "PoliceStationPassword"};
string[] congratulationsMsg = { ", You just hacked into the Library's Network. I see why you did that. You have fine written all over you.", ", You just hacked into the Police Station's Network. I see guilty people." };
string[] tryAgainMsg = { "I think you are cool, but this is not working.", "It doesn't work...... why?" };
// Start is called before the first frame update
void Start() {
//AskDetails Screen
ShowHomeScreen();
}
//Taking User Input
void OnUserInput(string input) {
print("The user has entered " + input + " as input");
if (currentScreen == Screen.HomeScreen)
{
input = input.Trim(' ');
if (input != null && input != "")
{
gamingName = input;
ShowMainMenuScreen();
}
else
{
Terminal.WriteLine("Entered username cannot be blank");
}
}
else if (currentScreen == Screen.MainMenuScreen)
{
if (input == "1" || input == "2")
{
level = Int32.Parse(input);
ShowGamePlayScreen();
}
else if (input == "3")
{
ShowHomeScreen();
}
else
{
Terminal.WriteLine("Entered level does not exist. Please enter a valid level.");
}
}
else if (currentScreen == Screen.GamePlayScreen)
{
CheckPassword(input);
}
else if (currentScreen == Screen.WinScreen)
{
ShowMainMenuScreen();
}
else if(currentScreen == Screen.TryAgainScreen)
{
if(input == "1")
{
ShowGamePlayScreen();
}
else if(input == "2")
{
ShowMainMenuScreen();
}
else
{
Terminal.WriteLine("Entered Input is Invalid.");
}
}
}
//Methods to compute
void CheckPassword(string enteredPassword)
{
if (enteredPassword == passwordsForLevels[level-1])
{
ShowWinScreen();
} else
{
ShowTryAgainScreen();
}
}
//Screens
//Home Screen
void ShowHomeScreen()
{
Terminal.ClearScreen();
currentScreen = Screen.HomeScreen;
Terminal.WriteLine("*****Welcome to Terminal Hacker*****");
Terminal.WriteLine("Please enter you username. Please make sure that there are no blank spaces in \nyour username");
}
//MainMenu Screen
void ShowMainMenuScreen()
{
Terminal.ClearScreen();
currentScreen = Screen.MainMenuScreen;
Terminal.WriteLine("*****Welcome to Terminal Hacker*****");
Terminal.WriteLine("Welcome " + gamingName);
Terminal.WriteLine("What would like to hack into?");
Terminal.WriteLine("Press 1 for the Local Library");
Terminal.WriteLine("Press 2 for the Police Station");
Terminal.WriteLine("Press 3 to go back to Home Screen.");
Terminal.WriteLine("Enter you selection");
}
//GamePlay Screen
void ShowGamePlayScreen()
{
Terminal.ClearScreen();
currentScreen = Screen.GamePlayScreen;
Terminal.WriteLine("*****Welcome to Terminal Hacker*****");
Terminal.WriteLine("Hey " + gamingName + ", Welcome to level " + level);
Terminal.WriteLine("Please enter the Password......");
}
//WinScreen
void ShowWinScreen()
{
Terminal.ClearScreen();
currentScreen = Screen.WinScreen;
Terminal.WriteLine("*****Welcome to Terminal Hacker*****");
Terminal.WriteLine("Congratulations " + gamingName + congratulationsMsg[level - 1]);
Terminal.WriteLine("\n\nDo you wanna hack something else?... Please any key to go to the Menu");
}
//TryAgainScreen
void ShowTryAgainScreen()
{
Terminal.ClearScreen();
currentScreen = Screen.TryAgainScreen;
Terminal.WriteLine("*****Welcome to Terminal Hacker*****");
Terminal.WriteLine(tryAgainMsg[level - 1]);
Terminal.WriteLine("\n\nDo you wanna try again... Press 1 to try again.");
Terminal.WriteLine("Press 2 for going to the Menu");
}
}