Hi, guys, I am new for gaming and c# but have experience in other languages. So there is my solution for password challenge:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
class PasswordRepository {
public int level;
public string[] passwords = {};
public PasswordRepository(int level, string[] passwords) {
this.level = level;
this.passwords = passwords;
}
}
public class Hacker : MonoBehaviour
{
enum Screen { MainScreen, Password, Win };
static string[] mainMenu = {
"\nAre you crazy enough 2 hack into ?\n",
"\tFBA - press 1",
"\tCIA - press 2",
"\tNSA - press 3\n",
"Enter your selection: "
};
PasswordRepository[] passwordRepos = {
new PasswordRepository(1, new string[] {"qwerty", "password"}),
new PasswordRepository(2, new string[] {"Sevriugin", "Sergei"}),
new PasswordRepository(3, new string[] {"Moscow", "Russia"}),
};
string userName = "Sergei";
int level = 0;
bool gameIsOn = false;
Screen currentScreen = Screen.MainScreen;
// Start is called before the first frame update
void Start()
{
ShowMainMenu(userName);
}
void ShowMainMenu(string name) {
gameIsOn = false;
currentScreen = Screen.MainScreen;
Terminal.ClearScreen();
Terminal.WriteLine($"Hello {name}! ");
foreach (string item in mainMenu)
{
Terminal.WriteLine(item);
}
}
void goToLevel(int level) {
print($"go to level {level}");
StartGame();
}
void generatePasswords() {
// TODO: update default repo and generate new passwords for each level
}
void StartGame() {
if (level == 0) {
ShowMainMenu(userName);
return;
}
generatePasswords();
Terminal.ClearScreen();
gameIsOn = true;
currentScreen = Screen.Password;
Terminal.WriteLine("Enter password: ");
}
void Win() {
gameIsOn = true;
currentScreen = Screen.Win;
Terminal.ClearScreen();
Terminal.WriteLine("U have won, hacccer!");
Terminal.WriteLine("\n\tpress Enter to start again...");
}
void ProcessGameInput(string input) {
if (currentScreen == Screen.Password) {
print($"Game is on, have got new password: {input}");
var filtered = passwordRepos.Where(r => r.level == level).ToArray();
var repo = filtered[0];
var passwords = repo.passwords;
var results = passwords.Where(p => p == input).ToArray();
if ( results.Length > 0 ) {
Win();
} else {
Terminal.WriteLine("Wrong password, try again!");
}
} else {
ShowMainMenu(userName);
}
}
void ProcessMenuInput(string input) {
if (int.TryParse(input, out level)) {
if (level > 0 && level < 4) {
goToLevel(level);
} else if (input == "007") {
Terminal.WriteLine("Agent 007 you are here again?");
level = 0;
} else {
Terminal.WriteLine("Wrong game level");
level = 0;
}
} else {
Terminal.WriteLine("Learn to type numbers first, hacccker");
}
}
void OnUserInput(string input) {
print(input);
if (input == "menu") {
ShowMainMenu(userName);
return;
} else if (input == "winwin") {
Win();
return;
}
if (gameIsOn) {
ProcessGameInput(input);
} else {
ProcessMenuInput(input);
}
}
// Update is called once per frame
void Update()
{
}
}