Okay, so I have two arrays:
public int[] start = new int[21] { 0, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14 };
public int[] end = new int[21] { 0, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14 }
I'm supposed to pick a random value from both arrays, and stick it in a 2D array. So, here is my code for that:
for (int i = 0; i < 21; i++)
{
Random ranNum = new Random();
int genX = 0;
int genY = 5;
int indx = 1;
while (Board[genX, genY].rank > -1)
{
genX = ranNum.Next(0, 9);
genY = ranNum.Next(5, 8);
}
while (startBoard[indx] == -1)
{
indx = ranNum.Next(0, 21);
}
if (gameBoard[genX, genY].rank == -1)
{
Board[genX, genY].rank = start [indx];
startBoard[indx] = -1;
}
while (gameBoard[genX, genY].rank > -1)
{
genX = ranNum.Next(0, 9);
genY = ranNum.Next(0, 3);
}
while (enemyBoard[indx] == -1)
{
indx = ranNum.Next(0, 21);
}
if (Board[genX, genY].rank == -1)
{
Board[genX, genY].rank = end[indx];
enemyBoard[indx] = -1;
}
}
Actually, this works fine. ONCE. Board is a custom object. Basically, that code randomly picks an item inside one of the arrays, randomly generates an X and Y index, and places it in the 2D array. It also replaces the item taken from the (previous 1D) array by a -1, to signify that there is nothing in that index, and thus, won't get picked.
As well, the 2D array is initially filled with -1's, to signify that there is nothing in them. If there is anything in it, the code will randomize another set of X and Y. Take note that there are different min / max values for the random for the "start" and "end" array items. This is so that they end up on the upper and lower end of the 2D array (the middle values stays at "-1").
Here's my problem. I have a code to reset that. Basically, it just restores back the arrays to before it had -1's, and erases all values in the 2D array and replaces it with -1's.
But, say, I run that code above. I am not satisfied with the placement. I reset the arrays, and run that code again.
At which point, the entire program stops running.
Could I have some help on this? Any ideas why it happens?
Thanks in advanced.