C#2d数组值替换

时间:2013-11-09 12:46:48

标签: c# arrays random

我创建了一个这样的数组:

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);


            }

        }

这将产生一个2d数组9x9,其中填充了随机数。 例如:

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

现在出现了一个真正的问题:如何使零“消失”(零将被上面的值替换,或者零会向上移动)?显然,顶行的零没有任何高于它们的值,因此必须创建一个新的随机数。此次randomNumber.Next(6)+1也不是一个选项。 我试过这个,但没用:

 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;
        }



        }
        }   

编辑: 3x3中的示例: 初始网格:

1 3 5

0 5 3

3 4 0

后:

R 3 R

1 5 5

3 4 3

R =新的randomNumber,它不是0

3 个答案:

答案 0 :(得分:2)

我知道这就是你想要的:(零会被上面的值替换,或者零会向上移动)

    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];
                }
            }                 
        }

    }

编辑:对不起,我在向您显示输出后检查了代码并意识到我忘了检查条目是否等于0:)

答案 1 :(得分:1)

基本上,您希望获得1到6之间的Random数字?

为此,您可以使用Random.Next(int minValue, int maxValue)方法。

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

此外,使用随机时,您不应该在循环内部重新播种。在循环之前实例化Random对象,然后使用它。你的第一个例子很好,但不是第二个例子。

答案 2 :(得分:1)

我对此进行了简要的测试,看起来像你想做的那样:

        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);

                            }
                        }
                    }
                }

            }

        }