繁体   English   中英

C#2d数组值替换

[英]C# 2d array value replace

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

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

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

    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到6之间的Random数?

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

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

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

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

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

                            }
                        }
                    }
                }

            }

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM