如何转回之前的if语句

本文关键字:if 语句 何转回 | 更新日期: 2023-09-27 17:54:51

我正在用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语句?

如何转回之前的if语句

如果不了解你想要做的事情的更多背景,就很难知道。您可能希望将一些代码提取到它自己的方法中,并在两个地方调用它,或者创建一个循环(for, while等)来处理控制。甚至可以使用goto命令,尽管我几乎不建议这样做。

也许你应该有某种状态机来运行你的游戏,以帮助处理条件和动作。


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

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语句中的一段代码。您可以学习这个Python文本冒险游戏库的代码,以获得更高级和完整的示例。

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

这里没有太多内容,但是您可以做很多事情。

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

在你的游戏中,你有很多行动,或者我们可以称之为选择。也许这段伪代码能让你找到正确的思路。

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