2016-07-03 15 views
-1

我正在用C#編寫我的第一個文本冒險遊戲,我只是想知道如何循環回到以前的if語句。 例如:如何轉回以前的if語句?

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

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

我會用什麼來讓它從嵌套去,如果以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

你應該展示一個實際的用例,用於你想做的任何事情。 – poke

+0

來吧......你必須提供更多的信息,而不僅僅是展示2'If'語句..你是否熟悉遞歸或while循環或for循環不能告訴你剛剛發佈的內容。 – MethodMan

+0

我的appologies。將更新更具體的信息 –

回答

2

如果不知道更多關於您想要做什麼的背景知識,很難知道。您可能想要將一些代碼抽取到自己的方法中,並在兩處調用它,或者創建一個循環(for,while等)來處理控制。甚至可以使用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的代碼,以獲得更高級和完整的示例。順便說一句,你可能想忽略所有的情況(就像我最後的比較那樣),而不僅僅是檢查「北」和「北」。

+0

因此,如果可能指定一個名爲「位置」的變量,並且像「while position = x」這樣做一段時間循環,並確定他們希望分配給位置的方向,那麼它會更節儉? –

+0

@Deron我已經更新了我的回答,請檢查它。 –

+0

謝謝你,你的回答是非常具有信息性的,我將用它來重做迄今爲止完成的工作並繼續它。再次感謝你。 –

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