First Solo - My attempt before watching Ben's solution

Hi all, I know most people are probably way further into the course but thought I would throw this up here.
My attempt at checking the players passwords. I haven’t seen how Ben does it yet, I wanted to post my own up here first as it seems to be working as intended but i’m sure it is different from the ‘official’ way so I’ll change it to match Ben’s before continuing in case I struggle down the line.

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

public class Hacker : MonoBehaviour {

    //Game State
    int level;
    enum Screen {MainMenu,Password,Win};
    Screen currentScreen = Screen.MainMenu;
    string passwordOne = "milton";
    string passwordTwo = "coppers";

	// Use this for initialization
	void Start ()
    {
        ShowMainMenu();
    }
	
	// Update is called once per frame
	void Update ()
    {
     
	}

    void ShowMainMenu() //Player entry point and reset point
    {
        Terminal.ClearScreen();             
        Terminal.WriteLine("Who are we hacking today?");
        Terminal.WriteLine("\n 0: Return to Main Menu");  
        Terminal.WriteLine(" 1: Local Library");
        Terminal.WriteLine(" 2: Local Police Station");        
        Terminal.WriteLine(" Enter your selection number now : ");
    }

    void OnUserInput(string input)
    {
        if (input == "0") // Lets always be able to return to menu from any screen
        {
            level = 0;
            currentScreen = Screen.MainMenu;
            ShowMainMenu();
        } else if (currentScreen == Screen.MainMenu)       
        {
            RunMainMenu(input);
        } else if (currentScreen == Screen.Password)
        {
            CheckPasswordGuess(input);
        }
    }

    void CheckPasswordGuess(string input)
    {
        if (level == 1 && input == passwordOne)
        {
            Terminal.WriteLine("\nCongratulations, succesfully logged in!\n" +
                               "If you want to try again press 0 to \nreturn" +
                               " to main menu\n");
        }else if (level == 2 && input == passwordTwo)
        {
            Terminal.WriteLine("\nCongratulations, succesfully logged in!\n" +
                               "If you want to try again press 0 to \nreturn" +
                               " to main menu\n");
        }else
        {
            Terminal.WriteLine("Incorrect, have another go\n");
        }
    }

    void RunMainMenu(string input)
    {
        if (input == "1")
        {
            level = 1;
            StartGame();
        }
        else if (input == "2")
        {
            level = 2;
            StartGame();
        }
        else
        {
            Terminal.WriteLine("Ummm does not compute I guess");
        }
    }

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

At first I wasn’t sure how to approach checking the user input at the same time as checking what the correct password should be so I had if statement inside if statement like this:

if (level == 1)
{
  if (input == password one)
  {
    Terminal.WriteLine("Yadda Yadda you win")
  }else
  {
    Terminal.WriteLine("You Suck")
  }
}if (level == 1)
{
  if (input == passwordTwo)
  {
    Terminal.WriteLine("Yadda Yadda you win")
  }else
  {
    Terminal.WriteLine("You Suck")
  }
}

But it was looking a bit gross so I changed it to what you see above that. It works so I’m happy and I think I got things like the new CheckPassword method correct formatting wise. Just interested if others think it looks like a logical way to have approached it. I’m sure i’m going to watch Ben’s method now and be ‘Okay that makes more sense than what I did’ Still proud I got mine to work though.

Thanks all,
Simon.

Edit: Removed had an issue posting in the right place, worked it out, i’m a dunce

2 Likes

Well done! Take a look at my solution before I watch Ben’s: Simple way using switch statements

I first approached this the same way (first code snippet).
I do like Ben’s approach though of using password state variable and settings it’s value in the same place place as the difficulty.

Privacy & Terms