-2

i am try to make a mini password game to learn c# on a course and i keep getting this error when im try to make the game read the password. here is the code anything i need to get rid of? or maybe add? if this helps im using unity as my engine. thanks

// Game state
int level;
enum Screen { MainMenu, Password, Win };

Screen currentScreen;

string password;
// Use this for initialization
void Start()
{
    ShowMainMenu();
}

void ShowMainMenu()
{
    currentScreen = Screen.MainMenu;
    Terminal.ClearScreen();
    Terminal.WriteLine("What would you like to hack into?");
    Terminal.WriteLine("Press 1 for the local library");
    Terminal.WriteLine("Press 2 for the police station");
    Terminal.WriteLine("Enter your selection:");
}
void OnUserInput(string input)
{
    if (input == "menu")
    {
        ShowMainMenu();
    }
    else if (currentScreen == Screen.MainMenu)
    {
        Runmainmenu(input);
    }
    else if (currentScreen == Screen.Password)
    {
        Checkpassword(input);

    }
}



void CheckPassword(string input)
{
    if (input == password)
    {
        checkpassword(input);
        Terminal.WriteLine("Sorry, wrong password!");
    }
    else
    {
        Terminal.WriteLine("WELL DONE!");


    }


}

private void checkpassword(string input)
{
    Checkpassword(input);
}

private void Runmainmenu(string input)
{
    {
        if (input == "1")
        {
            level = 1;
            password = "book";
            StartGame();
        }
        if (input == "2")
        {
            level = 2;
            password = "handcuffs";
            StartGame();
        }
        else if (input == "007")
        {
            Terminal.WriteLine("Please select a level Mr Bond!");
        }
        else
        {
            
        }
    }
}

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

void Checkpassword(string input)
{
    checkpassword(input);

}

private void runmainmenu(string input)
{
    runmainmenu(input);
    

}

string GetDebuggerDisplay()
{
    return ToString();
}
SBFrancies
  • 3,987
  • 2
  • 14
  • 37
  • 1
    `checkpassword()` calls `CheckPassword()` which then calls `checkpassword()` again and that probably goes on endlessly. – 41686d6564 stands w. Palestine Aug 11 '20 at 22:22
  • 1
    Please review [MCVE] guidance on posting code - there is a lot of unrelated code in the question and some insane number of "check password" calls with varying case calling each other. I.e. `private void runmainmenu(string input){ runmainmenu(input);}` would be solid minimal example of infinite recursion leading to stack overflow. – Alexei Levenkov Aug 11 '20 at 22:38
  • 1
    Having three different methods named `Checkpassword`, `CheckPassword` and `checkpassword`, or two methods named `Runmainmenu` and `runmainmenu` is a recipe for confusion. Don't do that. Follow the .Net [naming conventions](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/capitalization-conventions) and use PascalCase for your method names. If you had, this wouldn't have happened. (Specifically `Checkpassword` calls `checkpassword` which calls `Checkpassword`. And `runmainmenu` calls itself.) – dbc Aug 11 '20 at 22:44
  • Oh, I didn't even notice that there were three. @OP, please don't do that. Give your methods meaningful names. – 41686d6564 stands w. Palestine Aug 11 '20 at 22:54
  • Have you [asked a rubber duck](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? You started stepping through the program in the debugger and things went awry when ... . – HABO Aug 12 '20 at 03:12

1 Answers1

2

You've caused yourself an infinite loop, which causes this error.

void Checkpassword(string input)
{
    checkpassword(input);

}
private void checkpassword(string input)
{
    Checkpassword(input);
}
ChilliPenguin
  • 710
  • 7
  • 14