2016-08-16 25 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のコメントを虚しくするなら、あなたはこれを必要としません(本当に感謝の言葉です、それを休める)。 "rps.userSettings();"

まずあなたがクラス1

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

この行を削除でエラーが発生していますこれはprintHeader()プロシージャでこれを持っているからです。

int computerSelection = r.Next(1,3); 
: - 私が使用してcatched次のエラー

クラスのための3

int computerSelection = r.Next(4); 

変更をして、この行に「デバッガがブレークポイントを設定し、コードをステップ実行してみてください」:)ました

このhttps://msdn.microsoft.com/es-es/library/2dx6wyd4(v=vs.110).aspxを読めば、私はcatched最後のエラー今

:)間違っていると、コンソールを閉じてもらうため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)"をこの文のuse(1,4)に調整する必要があります。 –

+0

は多くの助けとなりました。どうもありがとうございました –