2017-11-15 11 views
0

私はこれを心配しています。プレイヤーが間違った推測をするたびに、ベットから、の初期残高を差し引く必要があります。 ループしているので、最初から常に最初のバランスが取られ、毎回同じバランスを逸らします。 (明らかに) 私はさまざまな変数を割り当てようとしましたが、それを理解できないようです。シンプルコンソールゲームのポイントを把握する

私はこれを理解するまで、今は使用していないので、中程度の困難な方法は省略しました。

My Main()はsetDifficulty()を呼び出します。他に何もない。

class Control 
{ 
    int selectedNumber = 0; 
    Random num = new Random(); 
    bool playAgain = true; 
    int difficulty = 0; 
    int bet = 0; 
    int initialBalance = 20; 
    int runningCredits = 0; 
    int credits = 0; 

    public Control() 
    { } 

    //sets game difficulty 
    public void SetDifficulty() 
    { 


     Console.Clear(); 
     Console.WriteLine("Please select level of difficulty between 1 - 100"); 
     difficulty = int.Parse(Console.ReadLine()); 


     if (difficulty >= 1 && difficulty <= 20) 
      LetsPlayEasy(); 


     else if (difficulty >= 21 && difficulty <= 50) 
      LetsPlayMedium(); 


     else if (difficulty >= 51 && difficulty <= 100) 
      LetsPlayHard(); 


     else 
     { 
      SetDifficulty(); 
     } 


    } 

    //easy level method 
    public void LetsPlayEasy() 
    { 
     //variables 
     int userGuess; 
     int numGuesses = 0; 
     selectedNumber = num.Next(1, 101); 

     Console.BackgroundColor = ConsoleColor.DarkYellow; 
     Console.Clear(); 
     Console.ForegroundColor = ConsoleColor.White; 

     Console.WriteLine("Difficulty level = EASY"); 

     Console.WriteLine("\nBeggining credit balance = " + initialBalance); 
     Console.WriteLine("\nPlease place a bet. You will lose those credits for every incorrect guess!"); 
     bet = int.Parse(Console.ReadLine()); 

     do 
     { 
      Console.WriteLine("\nGuess a number between 1 and 100."); 
      userGuess = Convert.ToInt32(Console.ReadLine()); 
      numGuesses++; 

      UI output = new UI(); 
      output.CompareNumbers(userGuess, ref selectedNumber, ref playAgain, ref numGuesses); 


      runningCredits = (initialBalance - bet); 
      Console.WriteLine("\nYou have " + runningCredits + " credits remaining."); 

     } while (playAgain == true); 
    } 

class UI 
{ 
    Random num = new Random(); 
    int keepGoing; 

    public UI() 
    { } 

    //compare user's guess to selected number 
    public void CompareNumbers(int userGuess, ref int selectedNumber, ref bool playAgain, ref int numGuesses) 
    { 
     Control difficulty = new Control(); 
     Admin say = new Admin(); 


     if (userGuess > selectedNumber) 
     { 
      Console.Beep(600, 300); 
      Console.WriteLine("\nToo High! Guess Again!"); 

     } 

     else if (userGuess < selectedNumber) 
     { 
      Console.Beep(300, 300); 
      Console.WriteLine("\nToo Low! Guess Again!"); 

     } 
     else 
     { 
      Console.Beep(350, 300); 
      Console.Beep(380, 200); 
      Console.Beep(380, 100); 
      Console.Beep(500, 1100); 
      Console.WriteLine("\n\nCongrats! It took you " + numGuesses + " guesses to win."); 
      numGuesses = 0; 
      Console.WriteLine("Press 1 to play again or 2 to quit."); 
      keepGoing = int.Parse(Console.ReadLine()); 


      while (keepGoing != 1 && keepGoing != 2) 
      { 
       Console.WriteLine("\n\nPlease type either 1 or 2 only!"); 
       keepGoing = int.Parse(Console.ReadLine()); 
      } 

      if (keepGoing == 2) 
      { 
       playAgain = false; 
       say.Goodbye(); 
      } 

      else 
      { 
       Console.Clear(); 
       difficulty.SetDifficulty(); 
      } 

     } 

    } 
} 

}初期化runningBalance initialBalanceにのみ計算にこの値を使用

答えて

2

。 initialBalanceが一度だけ必要な場合は、単にrunningBalanceをinitialBalanceに変更して、runningBalanceを初期化せずに行うこともできます。

Console.WriteLine("\nBeggining credit balance = " + initialBalance); 
Console.WriteLine("\nPlease place a bet. You will lose those credits for every incorrect guess!"); 
bet = int.Parse(Console.ReadLine()); 
runningBalance = initialBalance 
do 
{ 
    Console.WriteLine("\nGuess a number between 1 and 100."); 
    userGuess = Convert.ToInt32(Console.ReadLine()); 
    numGuesses++; 

    UI output = new UI(); 
    output.CompareNumbers(userGuess, ref selectedNumber, ref playAgain, ref numGuesses); 


    runningCredits -= bet; 
    Console.WriteLine("\nYou have " + runningCredits + " credits remaining."); 

} while (playAgain == true); 
0
runningCredits = (initialBalance - bet); 

あなたはので、すべての反復がrunningCreditsの同じ値を持ち、ループ内でinitialBalanceまたはbetを変更しないでください。ループの外

、このです:

runningCredits -= bet; 

注:あなたが見てループ内でチェックする任意のコードを持っていないループ内

runningCredits = initialBalance; 

は、これを行いますユーザーが正しいかどうかを推測した場合(したがって、ユーザーは常に敗北し、常に賭けを引き出す)。

+0

私はもともとそれを変更しましたが、再びそれは元々は何とか宣言されていたので、動作しませんでした。 –

+0

答えを比較するためのメソッドは、他のクラスのif文で呼び出されます...また、 - =は何をしますか?申し訳ありませんが、あなたがすでに伝えることができなかった場合、これは全く新しいです。助けてくれてありがとう@crashmstr –

+0

'runniningCredits - = bet;'は 'runningCredits = runningCredits - bet;'と同じです。答えのテストまでは、このレベルで必要なので、boolを返すか、別の参照パラメータ(戻り値を優先)を追加して、勝ったか失われたかを知り、 'runningCredits'を追加または削除します。 – crashmstr

関連する問題