I've created an array like this:
int[,] grid = new int[9, 9];
Random randomNumber = new Random();
var rowLength = grid.GetLength(0);
var colLength = grid.GetLength(1);
for (int row = 0; row < rowLength; row++)
{
for (int col = 0; col < colLength; col++)
{
grid[row, col] = randomNumber.Next(6);
}
}
This will result in an 2d array 9x9 which is filled with random numbers. example:
5 5 0 0 4 3 3 4 5
2 0 5 5 2 1 2 0 4
4 0 2 0 2 4 3 5 4
0 3 4 3 1 2 4 1 1
5 4 1 3 3 0 4 3 4
0 2 3 3 1 2 0 1 5
2 4 3 1 2 5 4 3 1
0 4 5 3 1 1 0 3 1
2 1 2 2 2 4 0 3 2
Now comes the real question: How can I make the zeros "disappear"(the zeros would be replaced with the values above them, or the zeros would move up against the top)? Obviously the zeros at the top row do not have any values above them so there would have to be created a new random number. Also randomNumber.Next(6)+1 is not an option this time around. I have tried this,but to no use:
foreach(int z in grid)
{
if(z==0)
{
if(col>0)
{
int a=grid[col,row];
int b=grid[col-1,row];
a=b;
b=a;
}
else
{
int a = grid[col, row];
Random randomNumber= new Random();
a = randomNumber.Next(6) + 1;
}
}
}
EDIT: Example in a 3x3: initial grid:
1 3 5
0 5 3
3 4 0
after:
R 3 R
1 5 5
3 4 3
R=new randomNumber which is not 0