2011-08-05 302 views
0

我寫了一個小型Shotgun應用程序,然而,當AI(稱爲Genius),用戶或兩者都被拍攝時,需要停止遊戲的代碼段,我無法工作。我究竟做錯了什麼?我覺得我通過增加大量不同的布爾值返回來使得我的代碼過度複雜,其中一些是通過的,而另一些則不是。End while while循環

在測試中,無論用戶移動(稱爲「字符串移動」)等於「f」,循環都會結束。在任何其他情況下,我無法讓循​​環結束。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 


     public static void Main(string[] args) 
     { 
      Start("r"); 
     } 

     public static string Start(string move) 
     { 

      Console.Write("Welcome to the Shotgun App\nEnter s for single player and m for multiplayer: "); 
      string gameType = Console.ReadLine(); 

      if (gameType == "s") 
      { 

       Console.Write("Single Player Controls:\n r = reload\n s = shield\n f = fire\nYou start with 1 ammo\nReady to play?"); 
       Console.ReadLine(); 

       int ammo = 1; 
       int geniusAmmo = 1; 
       string geniusMove = ""; 
       bool done = false; 
       while (!done) 
       { 
        Console.Write("\nEnter your move: "); 
        move = Console.ReadLine(); 


        switch (move) 
        { 
         case "r": 
          Console.Write("\nYou have reloaded, press enter for Genius\n"); 

          ammo++; 
          Console.Write("Your ammo is " + ammo); 

          Console.ReadLine(); 
          Genius(geniusMove, move, geniusAmmo, done); 


          break; 
         case "s": 
          Console.Write("\nYou have shielded, press enter for Genius\n"); 

          Console.Write("Your ammo is " + ammo); 

          Console.ReadLine(); 
          Genius(geniusMove, move, geniusAmmo, done); 


          break; 
         case "f": 
          if (ammo != 0) 
          { 
           Console.Write("\nYou have fired, press enter for Genius\n"); 

           ammo--; 
           Console.Write("Your ammo is " + ammo); 

           Console.ReadLine(); 

           Genius(geniusMove, move, geniusAmmo, done); 

          } 
          else 
          { 
           Console.Write("You don't have enough ammo, try again"); 
           done = false; 
          } 
          break; 
         default: 
          Console.Write("\nInvalid move, try again\n"); 
          done = false; 
          break; 
        } 
        done = EndLoop(move, geniusMove, done); 
        Console.ReadLine(); 

       } 
       return move; 
      } 
      else 
      { 
       return move; 
      } 
     } 

     public static string Genius(string geniusMove, string move, int geniusAmmo, bool done) 
     { 
      Random RandomNumber = new Random(); 
      int x = RandomNumber.Next(0,3); 
      if (x == 0) 
      { 
       geniusMove = "f"; 
       geniusAmmo--; 
       Console.Write("Genius had decided to fire.\nGenius ammo is " + geniusAmmo + "\n"); 
       TestMoves(move, geniusMove); 
      } 
      else if (x == 1) 
      { 
       geniusMove = "r"; 
       geniusAmmo++; 
       Console.Write("Genius had decided to reload.\nGenius ammo is " + geniusAmmo + "\n"); 
       TestMoves(move, geniusMove); 
      } 
      else if (x == 2) 
      { 
       geniusMove = "s"; 
       Console.Write("Genius had decided to shield.\nGenius ammo is " + geniusAmmo + "\n"); 
       TestMoves(move, geniusMove); 
      } 

      return geniusMove; 

     } 


     public static void TestMoves(string move, string geniusMove) 
     { 
      bool done = false; 
      if (move == "s" && geniusMove == "f") 
      { 
       Console.Write("Nice shield, no one has died yet"); 


      } 
      else if (move == "f" && geniusMove == "f") 
      { 
       Console.Write("You both died! Good game!"); 


      } 
      else if (move == "r" && geniusMove == "f") 
      { 
       Console.Write("No shield!? You died! Good game!"); 


      } 
      else if (move == "f" && geniusMove == "s") 
      { 
       Console.Write("Genius is too good, no one has died yet"); 


      } 
      else if (move == "f" && geniusMove != "s") 
      { 
       Console.Write("Genius let his guard down! Good game!"); 


      } 
      else if (move != "f" && geniusMove != "f") 
      { 
       Console.Write("Keep playing it safe."); 


      } 
      else 
      { 


      } 

     } 

     static bool EndLoop(string move, string geniusMove, bool done) 
     { 
      done = false; 
      if (move == "s" && geniusMove == "f") 
      { 
       return false; 
      } 
      else if (move == "f" && geniusMove == "f") 
      { 
       return true; 
      } 
      else if (move != "s" && geniusMove == "f") 
      { 
       return true; 
      } 
      else if (move == "f" && geniusMove == "s") 
      { 
       return false; 
      } 
      else if (move == "f" && geniusMove != "s") 
      { 
       return true; 
      } 
      else if (move != "f" && geniusMove != "f") 
      { 
       return false; 
      } 
      else 
      { 
       return done; 
      } 
     } 
    } 
} 
+0

這應該終止,如果你打的那個返回true ENDLOOP中列出的條件之一。當你在調試器中執行該例程時會發生什麼? –

+0

只有當用戶輸入爲「f」時,while循環纔會結束 –

回答

2

要設置在幾個不同的地方done,無論是在開關情況下,一些執行分支,並呼籲ENDLOOP時。來自EndLoop的作業將覆蓋以前的任務,因此請設置「您設置的一個地方」done

在TestMoves中使用EndLoop設置done不會產生任何影響,因爲您在調用EndLoop後立即返回一個硬編碼值。

我建議你在調試器中通過EndLoop。如果它使您更容易查看發生的情況,則可以考慮在控制檯上輸入EndLoop的輸入參數,以及在最終選擇的情況下輸出參數。

+0

所以我編輯了我的代碼,同樣的東西仍在工作。我拿出了很多回報。 –

+0

您是否添加了Console.WriteLine()來幫助您可視化程序流,或者在調試器中逐步瞭解您爲什麼沒有看到您期望的行爲? –

1

geniusMove將始終爲空字符串,因爲您沒有將調用結果存儲到Genius方法。

無論結果存儲在geniusMove變量或引用傳遞它

public static string Genius(ref string geniusMove, string move, int geniusAmmo, bool done) 

geniusMove = Genius(geniusMove, move, geniusAmmo, done);