Code for first solo challenge

terminal-hacker
#1

// here is my version of the solo challenge,
// using stuff I picked up from the Unity manual or from google
// or had discovered before while messing around with coding or Unity
// (not sure why the board only sees part of it as code)

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

public class Hacker : MonoBehaviour
{
//Game state
string chosenName = “stranger”;
int level;
enum Screen { MainMenu, Password, Win }
Screen currentScreen;

// Start is called before the first frame update
void Start()
{
    ShowMainMenu("stranger");
}

void ShowMainMenu(string username)
{
    currentScreen = Screen.MainMenu;
    Terminal.ClearScreen();
    Terminal.WriteLine("Hello, " + username);
    Terminal.WriteLine("You've made it this far");
    Terminal.WriteLine("Choose your battles wisely");
    Terminal.WriteLine("");
    Terminal.WriteLine("You can crack one of three passwords");
    Terminal.WriteLine("");
    Terminal.WriteLine("Enter 1 for library;");
    Terminal.WriteLine("Enter 2 for police station;");
    Terminal.WriteLine("Enter 3 for NASA.");
    Terminal.WriteLine("");
}

void RunMainMenu(string input)
{
        int.TryParse(input, out level);
        if ((1 <= level) && (level <= 3))
        {
            StartGame();
        }
        else
            Terminal.WriteLine("input unrecognized");
}

private void StartGame()
{
    Terminal.WriteLine("You have chosen level " + level);
    currentScreen = Screen.Password;
    Terminal.WriteLine("Please enter your password");
}

void WinGame()
{
    Terminal.WriteLine("Password correct.");
    currentScreen = Screen.Win;
}

void OnUserInput(string input)
{
    if (input == "menu")
    {
        ShowMainMenu(chosenName);
    }
    else if (currentScreen == Screen.MainMenu)
    {
        RunMainMenu(input);
    }
    else if (currentScreen == Screen.Password)
    {
        if ((level == 1 && input == "book") || (level == 2 && input == "intervention") || (level == 3 && input == "armstrong"))
        {
            WinGame();
        }
        else
        {
            Terminal.WriteLine("Password incorrect, try again");
        }
    }
    if (input == "my name is Bogdan")
    {
        chosenName = "Bogdan";
        ShowMainMenu(chosenName);
    }
}

}

#2

Looking good Bogdan.

I’m just wondering, what happens on the first screen if you type 1.5 or something like that?
I see your conditional is:

if ((1 <= level) && (level <= 3))

will it get through with 1.5? Or is that what the int.TryParse prevents?

#3

I don’t have Unity installed where I’m at now, so I can’t test on the spot, but the point of Int.TryParse is that the out argument (in this case, the integer level) gets set to 0 if the string can’t be parsed to an integer. so in theory, entering 1.5 would make level == 0
level being an int cannot be 1.5 anyway, but the difference between Parse and TryParse is that Parse would in that situation return an error and abort the game. also the method TryParse itself returns a boolean value (true if successfully parsed, false otherwise) which in this case I didn’t find a use for, but which can, for example, help make the difference between parsing the string “0” and parsing a non-integer value (both storing 0 in the variable used as the out argument).
I hope my explanation made sense.

#4

Yeah it made sense :slight_smile: Good explanation.

I had a little play with it and you’re absolutely right, anything other than a whole number returns 0. Nice code.

1 Like