-3

I am trying to make a Tetris game. I finished everything so far, except the rotation function. So, to sum it up. The plan is to rotate the following array elements by 90°: (In this case its a bool array)

. . . . .                 . . . . .
. . x . .                 . . . . .
. . x x .     ====== >    . . x x .
. . . x .                 . x x . .
. . . . .                 . . . . .

To do this I wrote the following code:

private bool[,] rotateGrid(bool[,] _grid)
    {
        bool[,] g = new bool[5, 5];

        for(int i=0; i<5; i++)
        {
            bool[] row = new bool[5];

            for(int j=4; j>=0; j--)
            {
                int jInvert = 4 - j;

                row[jInvert] = grid[j, i];
            }

            for(int j=0; j<5; j++)
            {
                grid[i, j] = row[j];
            }
        }

        return g;
    }

somehow, if I call this function with a full array, it retunes a empty one.

Why?

1 Answers1

0

My Code in java, just convert this to c#. If you want to do this even better, use a lambda expression. With a lambda expression you could do this in one line of code :)

/*rotates a block 90* right*/
    private static boolean[][] rotateBlock(boolean[][] block){
        boolean [][] temp;
        int longestRow = longestRow(block);
        temp = new boolean[longestRow][block.length];
        int counter = 0;
        for(int i = 0; i < longestRow;i++){
            for(int j = 0; j < block.length;j++){
                if(i < block[j].length){
                    if(block[j][i]){
                        temp[i][counter] = true;
                        counter++;
                    }
                }
                else{
                    //null = false
                    temp[i][counter] = false;
                    counter++;
                }
            }
            counter = 0;
        }
        return temp;
    }
Melvin
  • 77
  • 1
  • 10