-1

I'm making my first text adventure game with C#, and I was just wondering how I would loop back to a previous if statement. For Example:

if (x == 2 || y == 3)
{
   //do this

   if ( z > 3 || v = 6)
     {
       //do this
     }
}

What would I use to make it go from the nested if, to the top if statement?

EDIT: more specifically:

     //start
                System.Console.WriteLine("You awake in a stupor. The sun blinds you as you roll over to breathe the new day.");
                System.Console.WriteLine("  ");
                System.Console.WriteLine("  ");
                System.Console.WriteLine("To your (west), you see a volcanic mountain, with a scraggly, dangerous looking path.");
                System.Console.WriteLine("  ");
                System.Console.WriteLine("In the (east), you see a thick forrest. there is nothing visible past the treeline.");
                System.Console.WriteLine("  ");
                System.Console.WriteLine("Looking (south), you see a long winding road that seems to have no end.");
                System.Console.WriteLine("  ");
                System.Console.WriteLine("Hearing a sound of celebration, you look to the (north). There appears to be a city, with a celebration going on.");
                System.Console.WriteLine("  ");
                System.Console.WriteLine("  ");
                System.Console.WriteLine("Which direction would you like to travel?");
                string direction1 = Convert.ToString(Console.ReadLine());

 //first decision (north)
            if (direction1 == "north" || direction1 == "North")
            {
                Console.WriteLine("You proceed towards the north, only to be stopped by a guard:");
                Console.WriteLine("Guard: 'the kingdom of al'arthar is off limits to outsiders today, its the queens birthday, and the kingdom is packed full");
                Console.WriteLine("  ");
                Console.WriteLine("  ");
                Console.WriteLine("you can either try to (swindle) the guard, or (leave). What will you do?");
                string guardConvo1 = Convert.ToString(Console.ReadLine());

Say, for example, if someone traveled North in this scenario, and then wanted to go back. Rather than recode the previous if statement, how would I make it just loop back to the previous if statement?

Jk Jensen
  • 339
  • 2
  • 4
  • 16
  • 2
    You should show a practical use case for whatever you want to do. – poke Jul 03 '16 at 22:04
  • come on dude.. you have got to provide more information than just showing 2 `If` statements.. are you familiar with recursion or while loops or for loops cant tell what you are trying to do with what you have just posted.. – MethodMan Jul 03 '16 at 22:05
  • my appologies. will update with more specific information – Deron EZ Lauver Jul 03 '16 at 22:09

2 Answers2

2

It's hard to know without knowing more context about what you're trying to do. You might want to extract some code into its own method and call it in both places, or create a loop (for, while, etc.) to handle control. It's even possible to use a goto command, though I would almost never recommend that.

Maybe you should have some sort of state machine running your game to help handle conditions and actions.


Update: As you've found, the way you're writing the code is almost entirely unworkable for a text adventure game. Something to get you going in the right direction would be this: refactor each room into its own method. When you want to go from one room to another, you call the method for that room.

void StartingArea()
{
    System.Console.WriteLine("You awake in a stupor. The sun blinds you as you roll over to breathe the new day.");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("To your (west), you see a volcanic mountain, with a scraggly, dangerous looking path.");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("In the (east), you see a thick forrest. there is nothing visible past the treeline.");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("Looking (south), you see a long winding road that seems to have no end.");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("Hearing a sound of celebration, you look to the (north). There appears to be a city, with a celebration going on.");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("Which direction would you like to travel?");
    string direction1 = Convert.ToString(Console.ReadLine());

    if (direction1 == "north" || direction1 == "North")
    {
        AlArtharGate();
    }
}
void AlArtharGate()
{
    Console.WriteLine("You proceed towards the north, only to be stopped by a guard:");
    Console.WriteLine("Guard: 'the kingdom of al'arthar is off limits to outsiders today, its the queens birthday, and the kingdom is packed full");
    Console.WriteLine("  ");
    Console.WriteLine("  ");
    Console.WriteLine("you can either try to (swindle) the guard, or (leave). What will you do?");
    string guardConvo1 = Convert.ToString(Console.ReadLine());
    if (string.Equals(guardConvo1, "leave", StringComparison.CurrentCultureIgnoreCase))
    {
        StartingArea();
    }
}

This isn't perfect, but it's better: you have made each area its own callable entity instead of just a section of code in an if statement. You can study the code of this Python text adventure game base for a more advanced and complete example.

By the way, you might want to be ignoring all case (like I did with the comparison at the end), not just checking for "north" and "North".

Community
  • 1
  • 1
Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • so would it be more frugal to maybe assign a variable called 'position', and do a while loop like "while position = x"?, and have the direction they would want to go assigned to position? – Deron EZ Lauver Jul 03 '16 at 22:26
  • thank you, your answer has been highly informational, and i will be using it to redo what ive done thus far and continue with it. thank you again. – Deron EZ Lauver Jul 03 '16 at 22:33
0

Hmm not much to go on here, but there are a lot of things you could do.

A State machine is mentioned, but it might be a little abstract to wrap around at first. But think of it as cities and roads, you as a person can be at one place and only walk one road at a time. Sometimes you end up going in circles, other times you reach the end.

In your game you have a lot of actions or we can call them choices if you like. and maybe this pseudocode might get you on the right track of mind.

void Init(){
    Print("Select Choice")
    int choice = Read()
    if(choice == 1)
    {
        Choice1()
    }
    else if(choice == 2)
    {
        Choice2()
    }
    else
    {
        InvalidChoice()
    }
}
void Choice1(){
    Print("Welcome to Choice1 perform action or choice")
    int choice = Read()
    if(choice == 1)
    {
        Choice2()
    }
    else if(choice == 2)
    {
        Choice3()
    }
    else
    {
        InvalidChoice()
    }
}
void Choice2(){
    Print("Welcome to Choice2 perform action or choice")
    int choice = Read()
    if(choice == 1)
    {
        Choice3()
    }
    else if(choice == 2)
    {
        Choice1()
    }
    else
    {
        InvalidChoice()
    }
}
void Choice3()
{
    InvalidChoice()
}
void InvalidChoice()
{
    Print("You died")
}
Thomas Andreè Wang
  • 3,379
  • 6
  • 37
  • 53