1

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

3
  • Do you want to create matrix without zeores? Or you want to write a method that checks matrix and removes zeroes? Commented Nov 9, 2013 at 13:06
  • Alternative b, a method that checks matrix and removes zeroes @Romansz Commented Nov 9, 2013 at 13:22
  • No, he's basically writing Tetris. He needs to get the value from the row above when he clears a 0. Commented Nov 9, 2013 at 13:26

3 Answers 3

2

I understand that this is what you want : (the zeros would be replaced with the values above them, or the zeros would move up against the top)

    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);
            if(grid[row,col) == 0)
            {
                if(row == 0)
                {
                    while(true)
                    {
                       grid[row,col] = randomNumber.Next(6);
                       if(grid[row,col] == 0)
                          continue;
                       else
                          break;
                    }
                }
                else
                {
                    grid[row,col] = grid[row-1,col];
                }
            }                 
        }

    }

Edit : Sorry, I checked to code after you show me output and realised that I forgot to check if the entry is equal to 0 or not :)

Sign up to request clarification or add additional context in comments.

3 Comments

this clears the grid of the zeros, but instead makes each row the same, like this: 111314532 111314532 111314532 111314532 111314532 111314532 111314532 111314532 111314532
Looks to me like the idea in the code above is on the right track, but that you need to call it as a method and call it recursively to get all the other rows above to also update with the values of the rows above those. Set it up and recurse it.
UPDATE: this does 50% of what I asked for, this generates new randomnumber for each 0 in the top row, but then it copies the top row to all the other rows...
1

I briefly tested this, and it appears to do what you want:

        int[,] grid = new int[3, 3];

        grid[0, 0] = 1;
        grid[0, 1] = 2;
        grid[0, 2] = 3;
        grid[1, 0] = 0;
        grid[1, 1] = 5; 
        grid[1, 2] = 6; 
        grid[2, 0] = 0;
        grid[2, 1] = 8; 
        grid[2, 2] = 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);
        //        if (row == 1 && col == 0)
        //            grid[row, col] = 0;
        //        if (row == 2 && col == 0)
        //            grid[row, col] = 0;
        //    }
        //}

        //  Now, we have the grid with 0's, time to play Tetris with the 0's.
        for (int row = rowLength - 1; row >= 0; row--)
        {
            for (int col = 0; col < colLength; col++)
            {
                if (grid[row, col] == 0)
                {
                    for (int currentRow = row; currentRow >= 0; currentRow--)
                    {
                        if (currentRow == 0)
                            grid[currentRow, col] = randomNumber.Next(1, 6);
                        else
                        {
                            grid[currentRow, col] = grid[currentRow - 1, col];
                            if (grid[currentRow, col] == 0)  //  There was a 0 above our 0.
                            {
                                bool replaced = false;
                                for (int numberOfRowsAbove = 1; numberOfRowsAbove <= currentRow; numberOfRowsAbove++)
                                {
                                    if (grid[currentRow - numberOfRowsAbove, col] != 0)
                                    {
                                        grid[currentRow, col] = grid[currentRow - numberOfRowsAbove, col];
                                        replaced = true;
                                    }
                                }
                                if (!replaced)
                                    grid[currentRow, col] = randomNumber.Next(1, 6);

                            }
                        }
                    }
                }

            }

        }

17 Comments

Random.Next(6)+1 would work as well, but because each time something gets removed on the grid the new value for the removed object is going to be zero, then the numbers above the removed object would drop one or more rows down.
In this code there is one problem... one value seems to be different from the expected... Otherwise it works perfectly.
First grid: 553231504 500321133 223302143 113413405 003504014 440335433 224004334 015351202 521435330
your method: 134441355 134441355 134342153 523343133 213424445 143515414 424334333 215351234 521435332
I edited again. If there is a 0 in the row above a 0, you have to deal with that fact as well.
|
1

Basically you are looking to get a Random number between 1 and 6?

For that you can use the Random.Next(int minValue, int maxValue) method.

Random randomNumber = new Random();
randomNumer.Next(1, 6);

Also, when working with random you should never re-seed inside of the loop. Instantiate the Random object before the loop, then use it. Your first example was good, but not the second one.

1 Comment

Random.Next(6)+1 would work as well, but because each time something gets removed on the grid the new value for the removed object is going to be zero, then the numbers above the removed object would drop one or more rows down.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.