如何转回以前的if语句?

时间:2016-07-03 22:00:59

标签: c#

我正在用C#制作我的第一个文字冒险游戏,我只是想知道如何循环回到之前的if语句。 例如:

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

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

我将使用什么来使其从嵌套的if if到top if语句?

编辑:更具体地说:

     //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());

比方说,如果有人在这种情况下前往北方,然后又想回去。不是重新编码前面的if语句,而是如何让它循环回到之前的if语句?

2 个答案:

答案 0 :(得分:2)

如果不了解更多有关您尝试做的事情的背景,很难知道。您可能希望将一些代码提取到自己的方法中并在两个位置调用它,或者创建一个循环(nilscrollView.removeFromSuperview()等)来处理控制。它甚至可以使用goto命令,但我几乎不会推荐它。

也许你应该有某种state machine运行游戏来帮助处理条件和行为。

更新:正如您所发现的那样,您编写代码的方式几乎完全无法用于文本冒险游戏。让你朝着正确方向前进的方法是:将每个房间重构为自己的方法。当你想从一个房间走到另一个房间时,你可以调用该房间的方法。

for

这并不完美,但它更好:您已将每个区域设为自己的可调用实体,而不仅仅是while语句中的一段代码。您可以研究this Python text adventure game base的代码,以获得更高级和完整的示例。

顺便说一下,你可能想要忽略所有情况(就像我在最后做的比较一样),而不仅仅是检查" north"和" North"。

答案 1 :(得分:0)

嗯这里不多,但你可以做很多事情。

提到了一台状态机,但最初可能有点抽象。但是把它想象成城市和道路,你作为一个人可以在一个地方,一次只走一条路。有时你最终会进入圈子,有时你会到达终点。

在你的游戏中,你有很多动作,或者如果你愿意,我们可以给他们选择。也许这个伪代码可能会让你走上正确的道路。

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")
}