2015-07-19 140 views
0

在Main方法中,在for循環中,如果您在visual中複製/粘貼/運行整個程序,您會看到我試圖結束遊戲。首先「如果」,如果用戶沒有猜到;然後嘗試減1並繼續猜測。如果用戶猜到PCArray,則第二個「else if」比遊戲結束和消息顯示。那些工作。C#mastermind遊戲

但是,如果用戶已經用盡猜測,並且沒有猜到PCArray,那麼第一個「else if」比應該說「哦不,你猜不到......」爲什麼是這個不工作。

我只是想讓它工作,所以如果: - 用戶沒有猜到但仍然有嘗試,嘗試減1,直到0. - 用戶猜對了正確的數字,它說恭喜。 - 嘗試次數爲0,用戶仍然沒有猜到號碼,然後顯示「哦,不......」的消息。

class Program 
     { 
      static void Main(string[] args) 
      { 
       string name; 

       Console.WriteLine("**************Let's play Master-Mined**************"); 
       Console.WriteLine(); 
       Console.Write("Please enter your name: "); 
       name = Console.ReadLine(); 
       Console.WriteLine("Welcome {0}. Have fun!! ", name); 

       int numberCount = 0; 
       int difficultyLevel = 0; 

       int digitNumber = GetRandomNumberCount(numberCount); 
       Console.Write(digitNumber + " it is. Let's play."); 
       Console.WriteLine(); 
       int[] PCArray = GenerateRandomNumbers(digitNumber); 

       Console.WriteLine("A " + digitNumber + "-digit number has been chosen. Each possible digit may be the number 1, 2, 3 or 4."); 
       Console.WriteLine(" ******"); 

       int difficulty = GetGameDifficulty(difficultyLevel); 
       int attempts = difficulty * digitNumber; 

       Console.WriteLine("Enter your guess ({0} guesses remaining)", attempts); 
       int remaining = attempts; 

       for (int i = 0; i < attempts; i++) 
       { 
        int[] userArray = GetUserGuess(digitNumber); 
        int hits = CountHits(PCArray, userArray, attempts); 

        if ((hits != PCArray.Length) && (attempts > 0)) 
        { 
         remaining--; 
         Console.WriteLine("Enter your guess ({0} guesses remaining)", remaining); 
        } 
        else if ((attempts < 1)) 
        {      
         Console.WriteLine("Oh no {0}! You couldn't guess the right number.", name); 
         Console.WriteLine("The correct number is: "); 
         for (int j = 0; j < PCArray.Length; j++) 
         { 
          Console.Write(PCArray[j] + " "); 
         } 
         Console.WriteLine("Would you like to play again (Y/N)? "); 
        } 
        else if (hits == PCArray.Length) 
        { 
         attempts = 0; 
         Console.WriteLine("You win {0}!", name); 
         Console.WriteLine("The correct number is:"); 
         for (int j = 0; j < PCArray.Length; j++) 
         { 
          Console.Write(PCArray[j] + " "); 
         } 
        } 
       } 
       Console.ReadLine(); 
      }   
      public static int GetRandomNumberCount(int numberCount) 
      { 
       int number = 0; 
       do 
       { 
        try 
        { 
         Console.Write("How many numbers would you like to use in playing the game (4-10)? "); 
         number = int.Parse(Console.ReadLine()); 
         Console.WriteLine(); 
        } 
        catch 
        { 
         Console.WriteLine("You must pick a number between 4 and 10. Choose again."); 
         Console.WriteLine(); 
        } 
       } while ((number < 4) || (number > 10)); 

       return number; 
      } 
      public static int GetGameDifficulty(int difficultyLevel) 
      { 
       int difficulty = 0; 

       do 
       { 
        try 
        { 
         Console.Write("Choose a difficulty level (1=hard, 2=medium, 3=easy): "); 
         difficulty = int.Parse(Console.ReadLine()); 
        } 
        catch 
        { 
         Console.WriteLine("   Incorrect entry: Please re-enter."); 
        } 
       } while ((difficulty < 1) || (difficulty > 3)); 

       return difficulty; 
      } 
      public static int[] GenerateRandomNumbers(int PCSize) 
      { 
       int eachNumber; 
       int[] randomNumber = new int[PCSize]; 
       Random rnd = new Random(); 

       for (int i = 0; i < randomNumber.Length; i++) 
       { 
        eachNumber = rnd.Next(1, 5); 
        randomNumber[i] = eachNumber; 
        Console.Write(eachNumber); 
       } 
       Console.WriteLine(); 
       return randomNumber; 
      } 
      public static int[] GetUserGuess(int userSize) 
      { 
       int number = 0; 
       int[] userGuess = new int[userSize]; 
       for (int i = 0; i < userGuess.Length; i++) 
       { 
        Console.Write("Digit {0}: ", (i + 1)); 
        number = int.Parse(Console.ReadLine()); 
        userGuess[i] = number; 
        //Console.Write(number); 
       } 
       Console.WriteLine(); 
       Console.Write("Your guess: "); 
       for (int i = 0; i < userGuess.Length; i++) 
       { 
        Console.Write(userGuess[i] + " "); 
       } 
       Console.WriteLine(); 
       return userGuess; 
      } 
      public static int CountHits(int[] PCArray, int[] userArray, int attempts) 
      { 
       int hit = 0; 
       int miss = 0; 
       int hits = 0; 

       for (int i = 0; i < PCArray.Length; i++) 
       { 
        if (PCArray[i] == userArray[i]) 
        { 
         hit = hit + 1; 
         hits = hit; 
        } 
        else 
        { 
         miss = miss + 1; 
        } 
       } 
       Console.WriteLine("Results: {0} Hit(s), {1} Miss(es)", hit, miss); 
       return hits; 
      } 
     } 

回答

3

首先,您應該重新考慮您的代碼,因爲流量控制分散在您的代碼中。例如,您不需要attemptsremaining,它們控制着同樣的事情。

使用兩個變量來控制相同的事情是什麼導致你的問題。前兩個if在你for應該是這樣的:

if (hits != PCArray.Length && remaining > 1) 

else if (remaining == 1) 

注意我刪除了不必要的括號。通過這些更改,您的應用程序可以工作,但代碼不太漂亮。

增強代碼的一種簡單方法是首先檢查正確的結果,如果不是,則減少attempts變量並最終檢查其值。這就是爲什麼用你的代碼,if應該比較1,而不是0

您的CountHits方法中的hits變量是完全不必要的;你應該只返回hit。其實miss變量也可以避免,你可以計算它。還請記住使用++運算符。

但正如我先說的,重要的不是讓它工作,而是要正確地組織你的代碼。稍後我會發布我的版本。


我的版本,它不是世界上最美麗的東西,但我不想改變你的代碼結構的

class Program 
{ 
    static void Main(string[] args) 
    { 
     Console.WriteLine("************** Let's play Master-Mind **************\n"); 

     string name = GetPlayerName(); 

     do 
     { 
      Play(name); 

      Console.Write("\nWould you like to play again (Y/N)? "); 
     } 
     while (Console.ReadLine().ToUpper() == "Y"); 
    } 

    private static void Play(string name) 
    { 
     int numberCount = GetRandomNumberCount(); 
     Console.Write(numberCount + " it is. Let's play."); 
     Console.WriteLine(); 

     int[] PCArray = GenerateRandomNumbers(numberCount); 
     Console.WriteLine("A {0}-digit number has been chosen. Each possible digit may be the number 1 to 4.\n", numberCount); 

     int difficulty = GetGameDifficulty(); 

     bool won = false; 
     for (int allowedAttempts = difficulty * numberCount; allowedAttempts > 0 && !won; allowedAttempts--) 
     { 
      Console.WriteLine("\nEnter your guess ({0} guesses remaining)", allowedAttempts); 

      int[] userArray = GetUserGuess(numberCount); 

      if (CountHits(PCArray, userArray) == numberCount) 
       won = true; 
     } 

     if (won) 
      Console.WriteLine("You win, {0}!", name); 
     else 
      Console.WriteLine("Oh no, {0}! You couldn't guess the right number.", name); 

     Console.Write("The correct number is: "); 
     for (int j = 0; j < numberCount; j++) 
      Console.Write(PCArray[j] + " "); 
     Console.WriteLine(); 
    } 

    private static string GetPlayerName() 
    { 
     Console.Write("Please enter your name: "); 
     string name = Console.ReadLine(); 
     Console.WriteLine("Welcome, {0}. Have fun!!\n", name); 
     return name; 
    } 

    public static int GetRandomNumberCount() 
    { 
     int number; 

     Console.Write("How many numbers would you like to use in playing the game (4-10)? "); 
     while (!int.TryParse(Console.ReadLine(), out number) || number < 4 || number > 10) 
      Console.WriteLine("You must pick a number between 4 and 10. Choose again."); 

     return number; 
    } 

    public static int GetGameDifficulty() 
    { 
     int difficulty = 0; 

     Console.Write("Choose a difficulty level (1=hard, 2=medium, 3=easy): "); 
     while (!int.TryParse(Console.ReadLine(), out difficulty) || difficulty < 1 || difficulty > 3) 
      Console.WriteLine("Incorrect entry: Please re-enter."); 

     return difficulty; 
    } 

    public static int[] GenerateRandomNumbers(int PCSize) 
    { 
     int eachNumber; 
     int[] randomNumber = new int[PCSize]; 
     Random rnd = new Random(); 

     Console.Write("PC number: "); 
     for (int i = 0; i < PCSize; i++) 
     { 
      eachNumber = rnd.Next(1, 5); 
      randomNumber[i] = eachNumber; 
      Console.Write(eachNumber); 
     } 
     Console.WriteLine(); 
     return randomNumber; 
    } 

    public static int[] GetUserGuess(int userSize) 
    { 
     int number = 0; 
     int[] userGuess = new int[userSize]; 
     for (int i = 0; i < userSize; i++) 
     { 
      Console.Write("Digit {0}: ", (i + 1)); 
      while (!int.TryParse(Console.ReadLine(), out number) || number < 1 || number > 4) 
       Console.WriteLine("Invalid number!"); 
      userGuess[i] = number; 
     } 
     Console.WriteLine(); 
     Console.Write("Your guess: "); 
     for (int i = 0; i < userSize; i++) 
     { 
      Console.Write(userGuess[i] + " "); 
     } 
     Console.WriteLine(); 
     return userGuess; 
    } 

    public static int CountHits(int[] PCArray, int[] userArray) 
    { 
     int hits = 0; 

     for (int i = 0; i < PCArray.Length; i++) 
     { 
      if (PCArray[i] == userArray[i]) 
       hits++; 
     } 

     Console.WriteLine("Results: {0} Hit(s), {1} Miss(es)", hits, PCArray.Length - hits); 
     return hits; 
    } 
} 

它確實多了一些驗證和它甚至實際上讓你再玩一次! ;)

+0

謝謝安德魯,我試了幾次,我認爲它現在正常工作。 我完全理解「剩餘」和「企圖」是一樣的。我沒有得到的唯一問題是,如果我只在我的循環中使用「企圖」而不是「剩餘」,比程序停止一半。因此,如果您從8次嘗試開始,則程序將以剩餘4次猜測結束。如果你從12開始,程序將在6次嘗試後開始。 –

+0

...在6次嘗試後停止*(不開始)。 –

+0

那麼,你應該調試它,看看它爲什麼停在那裏;應該不難看出循環結束的條件是什麼。 – Andrew