- Home /
How can I accomplish Tetris rotations with 2D arrays?
Good god, who knew Tetris was so complicated? I'm a self-taught intermediate/advanced Unity developer (going by the classifications on Unity's tutorials) so I usually know my way around developing a lot of games, but this...!
Can anybody help me rotate a Tetramino as represented by a 2D array? Examples of the arrays follow:
int[][] tetraminoT =
{
new int[] {0,0,0,},
new int[] {1,1,1,},
new int[] {0,1,0,}
};
int[][] tetraminoJ =
{
new int[] {0,1,0,},
new int[] {0,1,0,},
new int[] {1,1,0,}
};
int[][] tetraminoI =
{
new int[] {0,1,0,0 },
new int[] {0,1,0,0 },
new int[] {0,1,0,0 },
new int[] {0,1,0,0 }
};
Using a gameObject as a pivot and calling Transform.Rotate or Rigidbody.MoveRotation are insufficient to answer this question. I'd rather solve this in game space and get some more practice with array nonsense.
Setting up the board, the tetraminos, moving them, checking for completed rows - none of this is a problem, and I expect to be able to implement SRS & wall kick stuff when I get over this roadblock.
A lot of people seem to think that this page holds the answer, but I'm having serious trouble parsing it.
Can anybody help a fellow traveller out? Thanks in advance. =) --Rev
Answer by toddisarockstar · Sep 24, 2018 at 06:01 AM
just loop through your x and y as if you where making a duplicate. but instead switch the x and y when assigning to the new output:)
public static int[][] Rotated(int[][] t){
int[][] ret = new int[t[0].Length][];
int x = ret.Length;
while(x>0){x--;ret[x] = new int[t.Length];}
x = t.Length;
while (x>0) {x--;
int y = t[x].Length;
while(y>0){y--;
//simply switch the x and y when assinging to the new array;
ret[y][x]=t[x][y];
}}
return ret;
}