2016-08-16 79 views
-2

我正在岩石剪刀控制檯應用程序上工作。我可以讓遊戲正常運行,並顯示單個遊戲的贏家。我無法獲得所有結果顯示在最後。在遊戲結束時,它應該顯示所有遊戲的結果,並且當有人贏得足夠的遊戲時它應該停止。應用程序目前在上一場比賽結束後關閉,我無法弄清楚原因。這是我有的代碼,有3個不同的類。如何顯示岩石紙剪刀遊戲的結果

1個

namespace ConsoleApplication1 
    { 
     class Program 
    { 
     static void Main(string[] args) 
     { 
      Game rps = new Game(); 
      rps.printHeader(); 
      rps.userSettings(); 
      rps.gameStart(); 
     } 
    } 
} 

2級

namespace ConsoleApplication1 
    { 
     class GameDetails 
     { 
      public string Name; 
      public int game; 
      public string Result; 

     public GameDetails() 
     { 
      Name = "unknown"; 
      game = 0; 
     } 

     public GameDetails(string winner) 
     { 
      Result = winner; 
     } 
    } 
} 

最後3類

namespace ConsoleApplication1 
{ 
    class Game 
    { 
     string name; 
     string winner; 
     int numPlays; 
     int game; 
     GameDetails[] gameArray; 



     public int NumGames 
     { 
      get 
      { 
       return numPlays; 
      } 
      set 
      { 
       numPlays = value; 
      } 

     } 

     public string Winner 
     { 
      get 
      { 
       return winner; 
      } 
      set 
      { 
       winner = value; 
      } 
     } 

     public void printHeader() 
     { 
      Console.WriteLine("Welcome to rock, paper, scissors"); 
      this.userSettings(); 
     } 

     private void InitializeArrays() 
     { 
      gameArray = new GameDetails[game]; 
      for (int game = 0; game < numPlays; game++) 
      { 
       gameArray[game] = new GameDetails(); 
      } 

     } 

     public void userSettings() 
     { 
      Console.WriteLine("What is your name: "); 
      name = Console.ReadLine(); 

      Console.WriteLine("How many games would you like to play?: "); 
      Int32.TryParse(Console.ReadLine(), out numPlays); 
      while (numPlays < 10 && numPlays % 2 == 0) 
      { 
       Console.WriteLine("\nNumber is not odd try again."); 
       Console.WriteLine("How many games would you like to play?: "); 
       Int32.TryParse(Console.ReadLine(), out numPlays); 
      } 

     } 


     public void gameStart() 
     { 
      Random r = new Random(); 
      for (game = 1; game <= numPlays; game++) 
      { 
       Console.WriteLine("Please choose Rock, Paper, or Scissors"); 
       string userSelection = Console.ReadLine(); 


       int computerSelection = r.Next(4); 


       if (computerSelection == 1) 
       { 
        if (userSelection == "rock") 
        { 
         Console.WriteLine("Computer Choice: Rock\n"); 
         Console.WriteLine("Game [{0}] is a tie", game); 
        } 
        else if (userSelection == "paper") 
        { 
         Console.WriteLine("Computer Choice: Paper\n"); 
         Console.WriteLine("Game[{0}] is a tie", game); 
        } 
        else if (userSelection == "scissors") 
        { 
         Console.WriteLine("Computer Choice: Scissors\n"); 
         Console.WriteLine("Game [{0}] is a tie", game); 
        } 
        else 
        { 
         Console.WriteLine("You must choose either rock, paper or scissors"); 
        } 

       } 

       else if (computerSelection == 2) 
       { 
        if (userSelection == "rock") 
        { 
         Console.WriteLine("Computer Choice: Paper\n"); 
         Console.WriteLine("You lose game [{0}], papaer beats rock", game); 

        } 
        else if (userSelection == "paper") 
        { 
         Console.WriteLine("Computer Choice: Scissors\n"); 
         Console.WriteLine("You lose game [{0}], scissors beats paper", game); 

        } 
        else if (userSelection == "scissors") 
        { 
         Console.WriteLine("Computer Choice: Rock\n"); 
         Console.WriteLine("You lose game [{0}], Rock beats scissors", game); 
        } 
        else 
        { 
         Console.WriteLine("You must choose either rock, paper or scissors"); 
        } 

       } 


       else if (computerSelection == 3) 
       { 
        if (userSelection == "rock") 
        { 
         Console.WriteLine("The computer chose scissors"); 
         Console.WriteLine("You win game [{0}], rock beats scissors", game); 

        } 
        else if (userSelection == "paper") 
        { 
         Console.WriteLine("The computer chose rock"); 
         Console.WriteLine("You win game [{0}],paper beats rock", game); 

        } 
        else if (userSelection == "scissors") 
        { 
         Console.WriteLine("The computer chose paper"); 
         Console.WriteLine("You win game [{0}], scissors beats paper!", game); 

        } 
        else 
        { 
         Console.WriteLine("You must choose either rock, paper or scissors"); 

        } 

        winner = Console.ReadLine(); 

       } 


      } 
     } 

     public override string ToString() 
     { 
      int arrayIndex = game - 1; 
      gameArray[arrayIndex].Result = winner; 

      string outputString = game + "\n"; 

      for (int game = 1; game < numPlays; game++) 
      { 
       int index = game - 1; 
       outputString += "Game " + game + ":" + gameArray[index].Result + "\n"; 
       } 

       return outputString; 
     } 


    } 
} 
+4

您應該瞭解調試器 - 嘗試設置斷點並逐步執行代碼。 – Blorgbeard

+2

從程序的外觀來看,你已經付出了努力。但是現在你已經遇到了一堵牆,或者你的挫折門檻,並且只是把整個東西扔在這裏,並要求我們調試幾百行代碼。您必須設置一些調試點,或者可以將變量的值打印到控制檯,並觀察程序執行時的情況。 –

+0

問題/提示:您如何/在哪裏打印結果?你的主要有gamestart ....然後呢? – kurakura88

回答

0

嘿,我的朋友,我會幫助的。但是,如果你對@Blorgbeard評論你不需要這個(真的是一個欣賞評論,休耕)。

首先你有一個錯誤在類1

namespace ConsoleApplication1 
{ 
    class Program 
{ 
    static void Main(string[] args) 
    { 
     Game rps = new Game(); 
     rps.printHeader(); 
     rps.userSettings(); 
     rps.gameStart(); 
    } 
} 

刪除此行 「rps.userSettings();」因爲你在printHeader()過程中已經有了這個。

我逮住使用下一個錯誤「調試器 - 嘗試設置斷點,單步調試代碼」 :)在此行中的3類

int computerSelection = r.Next(4); 

更改它:

int computerSelection = r.Next(1,3); 

如果你讀這https://msdn.microsoft.com/es-es/library/2dx6wyd4(v=vs.110).aspx你就會知道爲什麼錯了:)

現在最後一個錯誤我擦肩而過,是reazon因爲你關閉控制檯,只需添加(Console.ReadLine()):

if (userSelection == "rock") 
       { 
        Console.WriteLine("Computer Choice: Rock\n"); 
        Console.WriteLine("Game [{0}] is a tie", game); 
        Console.ReadLine(); 
       } 

我希望這會對你有所幫助。

+1

「int computerSelection = r.Next(1,3)」在這個句子中使用(1,4)你會有一個更好的結果。 –

+0

,這有很大的幫助。非常感謝你 –