1

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

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


            }

        }

这将产生一个 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=new randomNumber 不为 0

4

3 回答 3

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

于 2013-11-09T12:55:56.300 回答
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);

                            }
                        }
                    }
                }

            }

        }
于 2013-11-09T12:59:03.333 回答
1

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

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

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

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

于 2013-11-09T12:54:06.233 回答