2015-06-24 170 views
-1

我想做一個簡單的霰彈槍遊戲,用戶與CPU和兩者都選擇使用枚舉的鏡頭,屏蔽或重新加載,但是當我去運行我的代碼時,它只是循環用戶和計算機採摘。我不知道對於如何解決這一問題無限循環的麻煩

任何指導,將不勝感激

//Declare Variables 
     Console.Title = "Welcome To The Shotgune Game"; 
     int CPUBullets = 3, userBullets = 3; 
     ShotgunOption UserOption; 
     int userScore = 0; 
     bool QUIT = false; 
     double gameCount = 0.0; 
     Random computer = new Random(); 

     Console.Clear(); 
     Console.WriteLine("SHOOT RELOAD SHIELD"); 

     UserOption = GetOptionFromUser(); 
     ShotgunOption CPUOption = (ShotgunOption)computer.Next(1, 3); // 1 is Shot, 2 is Reload, 3 is Shield 

     do 
     { 


      // if (UserOption == "QUIT") 
      //{ 
       //break; 
      //} 

      do 
      { 
      //Console.Write("Please enter choice, or enter QUIT to quit: "); 




       switch (UserOption) 
      { 
       case ShotgunOption.Shoot: 
        if((int)CPUOption == 1) 
        { 
         Console.WriteLine("You chose {0} and the computer chose Shoot. It was a tie!", UserOption); 
         ; userBullets --;CPUBullets --; ++gameCount; 
        } 
        else if ((int)CPUOption == 2) 
        { 
         Console.WriteLine("You chose {0} and the computer chose Reload. You win!", UserOption); 
         ++userScore; ++gameCount; 
        } 
        else if ((int)CPUOption == 3) 
        { 
         Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", UserOption); 
         ++gameCount; 
        } 
        break; 
       case ShotgunOption.Reload: 
        if((int)CPUOption == 1) 
        { 
         Console.WriteLine("You chose {0} and the computer chose Shoot. You lose!", UserOption); 
         ++userScore; ++gameCount; 
        } 
        else if ((int)CPUOption == 2) 
        { 
         Console.WriteLine("You chose {0} and the computer chose Reload. You Both Gain A bullet", UserOption); 
         userBullets++; CPUBullets++; ++gameCount; 
        } 
        else if ((int)CPUOption == 3) 
        { 
         Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", UserOption); 

        } 
        break; 
        case ShotgunOption.Shield: 
        if((int)CPUOption == 1) 
        { 
         Console.WriteLine("You chose {0} and the computer chose Shoot. You lose!", UserOption); 
         ++gameCount; 
        } 
        else if ((int)CPUOption == 2) 
        { 
         Console.WriteLine("You chose {0} and the computer chose Reload. You win!", UserOption); 
         ++userScore; ++gameCount; 
        } 
        else if ((int)CPUOption == 3) 
        { 
         Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", UserOption); 
         ++gameCount; 
        } 
        break;     

      } 
      } 
      while (UserOption != ShotgunOption.Shield || CPUOption != ShotgunOption.Shield); 
     } while (QUIT == false || gameCount != 3 || UserOption != ShotgunOption.Shield || CPUOption != ShotgunOption.Shield); 



     if (gameCount > 1) 
     { 
      DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets); 
     } 
+2

您提示輸入** OUTSIDE **您的循環,然後從不提示輸入循環內。因此,無論在開始時選擇什麼都會成爲永久的選擇。 –

+5

通常使用調試器可以很快解決這類問題。你應該試試。很快,你會問自己如何在沒有它的情況下進行編程 – Steve

回答

0

你永遠更新環路內的用戶的輸入。一旦用戶做出選擇,它永遠不會改變。
UserOption = GetOptionFromUser();也應該出現在你的循環中。至少這是我可以從快速瀏覽中得知的。

+0

另外,我的答案和我的答案一樣,我應該問:爲什麼要使用do/while而不是常規?如果用戶想在第一輪之前退出,你是否強迫他們發揮第一輪? –