它说在我的数组中我已经浏览了索引。我的节目是5个玩家(5个索引)玩的数字猜测游戏。我使用数组来创建对象和播放器类。 我已经到了一个残端,我的程序在第二轮或第三轮比赛中崩溃了。我注意到在第二轮中,索引没有循环属性:循环在第一个循环中计算索引1到5,然后在第二个循环中计数2到5,然后如果我甚至到达循环的第3轮,所有的索引都被洗牌,意思是我不能从1到5。
当每个玩家得到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;
}
}
}
答案 0 :(得分: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;
}
答案 1 :(得分:0)
这意味着您正在尝试访问大于数组的索引。在该行:
while(playedThisRound[index] == false)
在使用索引之前,你没有检查边界,你的崩溃可能就在那里。
答案 2 :(得分:0)
这意味着您正在尝试访问索引高于数组限制的数组中的项目。