簡體   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