2017-11-18 85 views
0

我會簡化它:基本的hang子手遊戲,它通過玩家2字母選擇循環,如果它不匹配玩家1的單詞的第一個字符,它會移除生命,第二個,直到它找到一個匹配或者只是刪除了這個玩家1個字長的許多生命。C# - 基本的hang子手遊戲生命問題

我明顯不希望這樣,我想要它檢查數組 - 如果沒有匹配,則刪除一條生命。

for (int i = 0; i < playerTwoGuesses.Length; i++) 
     { 
      Thread.Sleep(1400); 
      Console.Write("Guess: "); 
      count = 0; 

      do 
      { 
       try 
       { 
        playerTwoGuesses[i] = char.Parse(Console.ReadLine()); 
        validGuess = true; 
       } 
       catch (Exception) 
       { 
        Console.WriteLine("Please enter a single character only."); 
       } 
      } while (validGuess == false); 


      for (int j = 0; j < playerOneDisguised.Length; j++) 
      { 
       if (playerOneCharacters[j] == playerTwoGuesses[i]) 
       { 
        playerOneDisguised[j] = playerTwoGuesses[i]; 

       } 
       else 
       { 
        lives = lives - 1; 

       } 
      } 


      if (lives == 0) 
      { 
       Console.WriteLine("Oh no! It seems you've lost. Closing game in 5 seconds."); 
       Thread.Sleep(5000); 
       Environment.Exit(0); 
      } 


      Console.WriteLine(playerOneDisguised); 


      for (int k = 0; k < playerOneDisguised.Length; k++) 
      { 
       if (playerOneDisguised[k] != '*') 
       { 
        count = count + 1; 
        if (count == playerOneDisguised.Length) 
        { 
         Console.WriteLine("Congratulations you've won!"); 
         Thread.Sleep(1000); 
         Console.WriteLine("Closing game in 5 seconds."); 
         Thread.Sleep(5000); 
         Environment.Exit(0); 
        } 
       } 
      } 
     } 
+0

所以你可能想檢查輸入的字母**是否包含在單詞**中。如果沒有,生命被刪除... –

回答

0

試試這個:

 bool match = false; 
     for (int j = 0; j < playerOneDisguised.Length; j++) 
     { 
      for (int y = 0; y < playerTwoGuesses.Length; y++) 
      { 
       if (playerOneCharacters[j] == playerTwoGuesses[y]) 
       { 
        playerOneDisguised[j] = playerTwoGuesses[y]; 
        match = true; 
       } 
      } 

     } 
     if (match == false) { 

      lives = lives - 1; 
     } 
     // Reset it back to false 
     match = false; 
+0

是的,它實際上已經修復它 - 非常感謝:) –

0

而不是在陣列上自己循環,你可以問,如果猜對焦炭存在於它。然後你只需要一個if語句來處理猜測

if (Array.Exists(playerTwoGuesses, element => element == lastGuess) { 
+1

我可能誤讀了一些東西,但爲什麼'playerTwoGuesses.Contains(lastGuess)'工作? – 2017-11-18 10:34:36

+0

你說得對,那會很好。這是一個更好的答案,更具可讀性 –