【问题标题】:How to transfer back to previous if statement?如何转移回之前的if语句?
【发布时间】:2016-11-05 14:17:09
【问题描述】:

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

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

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

我会用什么让它从嵌套的 if 到顶部的 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 个 If 语句..您是否熟悉递归或 while 循环或 for 循环无法告诉您要做什么你刚刚发布的内容..
  • 我的道歉。将更新更具体的信息

标签: c#


【解决方案1】:

如果不了解您正在尝试做什么的更多背景信息,就很难知道。您可能希望将一些代码提取到它自己的方法中并在两个地方调用它,或者创建一个循环(forwhile 等)来处理控制。甚至可以使用goto 命令,尽管我几乎不会推荐。

也许您应该让某种state machine 运行您的游戏来帮助处理条件和操作。


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

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

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

顺便说一句,您可能希望忽略所有大小写(就像我在最后的比较中所做的那样),而不仅仅是检查“北”和“北”。

【讨论】:

  • 所以分配一个名为'position'的变量会更节俭吗?像“while position = x”一样做一个while循环,并将他们想要的方向分配给位置?
  • 谢谢,您的回答提供了很多信息,我将使用它来重做我迄今为止所做的事情并继续它。再次感谢您。
【解决方案2】:

嗯,这里没什么可做的,但是您可以做很多事情。

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

在您的游戏中,您有很多动作,或者如果您愿意,我们可以称它们为选择。也许这个伪代码可能会让你走上正轨。

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

【讨论】:

    猜你喜欢
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多