How to Rotate a 2D Array of Integers

前端 未结 7 1944
栀梦
栀梦 2020-12-15 06:36

I am programming a Tetris clone and in my game I store my tetromino blocks as 4x4 arrays of blocks. I now need to be able to rotate the integer positions in the arrays so th

7条回答
  •  一整个雨季
    2020-12-15 07:05

    If they're a 2D array, you can implement rotation by copying with different array access orders.

    i.e., for a clockwise rotation, try:

    int [,] newArray = new int[4,4];
    
    for (int i=3;i>=0;--i)
    {
        for (int j=0;j<4;++j)
        {
             newArray[j,3-i] = array[i,j];
        }
    }
    

    Counter-clockwise is similar.

提交回复
热议问题