2012-02-22 166 views
0

它說,在我的數組中,我已經超過了索引。我的程序是由5個玩家(5個索引)玩的猜數遊戲。我已經使用數組來創建對象和玩家類。 我已經到達了一個殘局,我的程序在第二輪或第三輪比賽中崩潰。我注意到,在我的第二輪中,索引沒有循環屬性:循環在第一個循環中計數索引1到5,然後在第二個循環中計數2到5,然後如果我甚至到達第三輪循環,所有的指標都被打亂,這意味着我不能從1到5.IndexOutofRangeException是什麼意思?

由於每個玩家獲得3次猜測,使用這3次猜測和你離開遊戲。我已經拿到了我爲該播放器創建的對象的數組,創建了一個比前一個小的臨時數組,並引用它來實現當前數組。

我查看了代碼中的引用,發現儘可能多的代碼,我無法找到導致System.IndexOutOfRangeException的錯誤。這是由我的猜謎遊戲課引起的。

這裏是我的GuessingGame類:

using System; // only this using statement is needed here. 

namespace GuessingGame 
{ 

class GuessingGame 
{ 
    #region instance attributes 
    private const int GUESSES_ALLOWED = 3; 
    private const int NUMBER_OF_PLAYERS_TO_START = 5; 
    private const int MIN_VALUE = 1; 
    private const int MAX_VALUE = 15; 
    private Player[] players; 
    private Random randomSource; 
    #endregion 

    public GuessingGame() 
    { 
    Console.WriteLine("Starting Constructor of GuessingGame"); 
    players = new Player[NUMBER_OF_PLAYERS_TO_START]; 
    randomSource = new Random(); 

    string playerName = ""; 
    for (int index = 0; index < players.Length; index++) 
    { 
     Console.Write("What is the name for player #" 
       + (index +1) + "?\t"); 
     playerName = Console.ReadLine(); 
     players[index] = new Player(playerName, randomSource); 
     Console.Write("\n"); 
    } 
    Console.WriteLine("Ending GuessingGame Constructor"); 
    } 

    public GuessingGame(string [] playerNames) 
    { 

    Console.WriteLine("Starting Constructor of GuessingGame"); 
    players = new Player[playerNames.Length]; 
    randomSource = new Random(); 
    for (int index = 0; index < playerNames.Length; index++) 
    { 
     players[index] = new Player(playerNames[index], randomSource); 
    } 
    } 

    public void playGame() 
    { 
    int numberOfPlayersWhoHavePlayedThisRound = 0; 
    int index = 0; 

    bool[] playedThisRound = null; 
    string playerGuessEntry = ""; 
    int playerGuessValue = -1; 
    Player[] tempArray = new Player[players.Length - 1]; 
    bool roundOver = false; 

    Console.WriteLine(
      "Starting playGame - press any key to continue"); 
    //Console.Read() 

    while (roundOver == false) // Is this the right condition? 
    { 

     playedThisRound = new bool[players.Length]; 


     while (playedThisRound[index] == false) 
     { 
      do 
      { 
       Console.Write(players[index].getName() 
         + ", Enter a number between " 
         + MIN_VALUE.ToString() 
         + " and " + MAX_VALUE.ToString() 
         + " inclusive\t"); 
       playerGuessEntry = Console.ReadLine(); 
       Console.Write("\n"); 
      } 
      while (!int.TryParse(playerGuessEntry, 
         out playerGuessValue) 
        || playerGuessValue < MIN_VALUE 
        || playerGuessValue > MAX_VALUE); 
      if(playerGuessValue < MIN_VALUE || playerGuessValue > MAX_VALUE) 
      { 
       Console.Write("Invalid guess- try again"); 
      } 
      else 
      { 

       Console.WriteLine("You entered " 
         + playerGuessValue.ToString()); 

       players[index].makeAGuess(playerGuessValue); 
       playedThisRound[index] = true; 
       if (index == players.Length) 
       { 
        Console.WriteLine("End of Round"); 
        index = 0; //edit? 
        numberOfPlayersWhoHavePlayedThisRound = 0; 
       } 

      } 
      if (players[index].getGuessesUsed() == 3) 
      {//creating a temp array 
       Console.WriteLine("Guesses MAXED"); 
       tempArray = players[index].deletePlayerFromArray(players, index); 
       players = tempArray; // referencing 
       bool[] tempBooleanArray = new bool[playedThisRound.Length - 1];//reducing size of played this round array 
       Console.WriteLine("Playedthisround length: " + playedThisRound.Length + " \nThe Index: " + index.ToString()); 
       tempBooleanArray = players[index].deletePlayerBool(playedThisRound, index); 
       playedThisRound = tempBooleanArray; 
       Console.WriteLine("New Player Array Size: " + players.Length); 
       Console.WriteLine("New Boolean Array Size: " + playedThisRound.Length); 
      } 
      if (index == players.Length - 1) 
      { 
       index = 0; 
       numberOfPlayersWhoHavePlayedThisRound = 0; 
      } 
      if (players.Length == 1) 
      { 
       roundOver = true; 
      } 
      index++; 
      numberOfPlayersWhoHavePlayedThisRound++; 
     } 
      Console.WriteLine("WINNER:" + players[index].getName() + 
       "\nWins: " + players[index].getWins() + "\nArray Size: " + players.Length.ToString()); 

    }//end of while 

    Console.WriteLine("Ending playGame - " 
      + "press any key to continue"); 
    Console.Read(); 
    } 
     public bool playersAlreadyPlayed(bool[] thePlayer) 
     { 
      bool havePlayed = false; 
      for (int plays = 0; plays < thePlayer.Length; plays++) 
      { 
       if (thePlayer[plays] == false) 
       { 
        havePlayed = false; 
       } 
       else 
       { 
        havePlayed = true; 
       } 
      } 
      return havePlayed; 
     } 

    static void Main(string[] args) 
    { 
    GuessingGame newGame = new GuessingGame(); 
    newGame.playGame(); 
    } 
} 

}

,這裏是玩家級

using System; 

namespace GuessingGame 
{ 

    class Player 
    { 

     private String name; 
     private int winningNumber; 
     private int guessesUsed; 
     private int wins; 
     private Random myWinningNumberSource; 

     public Player(string newName, Random random) 
     { 
     name = newName; 
     guessesUsed = 0; 
     wins = 0; 
     myWinningNumberSource = random; 
     winningNumber = myWinningNumberSource.Next(1, 16); 
     } 


     public bool makeAGuess(int guessValue) 
     { 
      bool isWinner = false;//edit 
     if (guessValue == winningNumber) 
     { 
      wins++; 

      Console.WriteLine("Congradulations, You have guessed correct number!\n"); 
      Console.WriteLine("You have a total of " + wins + " wins!"); 
      Console.WriteLine("You have " + (3 - guessesUsed) + " guesses left!\n"); 
      winningNumber = myWinningNumberSource.Next(1, 16); 
      isWinner = true; //edit 

     } 
     else 
     { 
      guessesUsed++; 

      Console.WriteLine("Oh no! You have guessed incorretly!"); 
      Console.WriteLine("You have used " + guessesUsed + " and have " + (3 - guessesUsed) + " guesses left!"); 
      Console.WriteLine("HINT: You should have guessed " + winningNumber); 
      isWinner = false; 


      if (guessesUsed > 3) 
      { 
       Console.WriteLine("Sorry you have Lost, Game Over"); 

      } 

     } 
     return isWinner; 
     } 

     public int getGuessesUsed() 
     { 
     return guessesUsed; 
     } 

     public string getName() 
     { 
     return name; 
     } 
     public int getWins() 
     { 
      return wins; 
     } 
     public Player[] getWinner(Player[] nPlayers) 
     { 
      int maxScore = 0; //edit 
      Player[] winningPlayers; 
      winningPlayers = new Player[5]; 
      for (int i = 0; i < nPlayers.Length; i++) 
      { 
       if (nPlayers[i].wins >= maxScore) 
       { 
        winningPlayers[i].wins = nPlayers[i].getWins(); 
        winningPlayers[i].name = nPlayers[i].getName(); 
       } 
      } 
      return winningPlayers; 
     } 
     public bool[] deletePlayerBool(bool[] playedThisRound, int removeIndex)//edit 
     { 
      bool[] newArray = new bool[playedThisRound.Length - 1]; 
      int tempIndex = 0; 
      for (int i = 0; i < playedThisRound.Length; i++) 
      { 
       if (i != removeIndex) 
       { 
        newArray[tempIndex++] = playedThisRound[i]; 
       } 
      } 
      return newArray; 
     } 
     public Player[] deletePlayerFromArray(Player[] nPlayers, int removeIndex) 
     { 
      Player[] newArray = new Player[nPlayers.Length - 1]; 
      int tempIndex = 0; 
      for (int i = 0; i < nPlayers.Length; i++) 
      { 
       if (i != removeIndex) 
       { 
        newArray[tempIndex++] = nPlayers[i]; 
       } 
      } 
      return newArray; 
     } 

    } 
} 
+1

這是你應該使用調試器的地方。什麼代碼是拋出的異常? – 2012-02-22 21:05:50

+1

要回答標題:http://msdn.microsoft.com/en-us/library/system.indexoutofrangeexception.aspx – 2012-02-22 21:07:00

+0

一般來說,'IndexOutofRangeException'意味着你試圖訪問一個數組的元素在一個更大的索引而不是等於陣列的長度。請記住:**數組索引從零開始,而不是一個。 – 2012-02-22 21:07:22

回答

1

我處於nPlayer長度的範圍內而不是0-4。

public Player[] getWinner(Player[] nPlayers) 
    { 
     int maxScore = 0; //edit 
     Player[] winningPlayers; 
     winningPlayers = new Player[5]; 
     for (int i = 0; i < nPlayers.Length; i++) 
     { 
      if (nPlayers[i].wins >= maxScore) 
      { 
       winningPlayers[i].wins = nPlayers[i].getWins(); 
       winningPlayers[i].name = nPlayers[i].getName(); 
      } 
     } 
     return winningPlayers; 
    } 
0

這意味着你試圖訪問比數組大的索引。在行:

while(playedThisRound[index] == false) 

你不使用索引之前檢查的界限,你的崩潰可能是在那裏。

+0

好的,檢查playingThisRound.Length的邊界? – GivenPie 2012-02-22 23:56:16

0

這意味着您正試圖訪問數組中索引高於數組限制的項目。

+0

...或小於零。 – phoog 2012-02-22 21:13:27