views:

2931

answers:

6

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 that I get a rotated tetris block. I cannot simply rotate the texture because all my collision detection, etc has been designed to work with the 2D array. The game is written in C# using XNA.

How can i possibly rotate my 2D array of ints by 90 degrees clockwise/counter clockwise.

Here is how my 'L' block is stored as an example.

0 1 0 0
0 1 0 0
0 1 1 0 
0 0 0 0

Thanks for your help.

+6  A: 
Reed Copsey
Good solution, but overkill given the problem domain.
Ryan Emerle
testing this code now...
Brock Woolf
I am voting this up, it should be marked as the solution. Not sure what you mean rde6173, unless you too are vouching for storing 4 orientation arrays.
Ricket
That's what he's suggesting. There are advantages to that, but this answers the question directly - I wasn't trying to suggest alternate designs.
Reed Copsey
Thanks Reed it works :)
Brock Woolf
No problem :) Glad I can help.
Reed Copsey
+1  A: 

If you want to rotate a 4 x 4 block, you just move the positions:

A B C A
C D D B
B D D C
A C B A

Each A moves to the next A, and the same for B, C and D.

   /-----\
   |     |
   |     V
   A B C A
/->C D>D B--\
|  B D D C  |
|  A C B A  |
|    | ^    |
|    | |    |
\----/ \----/
Gamecat
that thing looks like a dick in ascii art
fmsf
I think your picture is a little off, not sure exactly what you're trying to clarify with it and the arrows seem to point b -> c and a -> c rather than supporting your instructions.fmsf, grow up.
Ricket
@Ricket thanks, corrected it.
Gamecat
+3  A: 

In classic tetris there are very few permutations of objects. I would simply have a constant array for each "tetromino," at each of the 4 positions, and simple logic to choose the appropriate one based on input.

Why waste CPU cycles trying to rotate it?

Ryan Emerle
The math is so simple in this case, it's probably just as fast to rotate it as it would be to do the permutation search based on the current block style + position.
Reed Copsey
That's ridiculous.
Ryan Emerle
I'd also add that from a gameplay and design point-of-view, it might be better to have pre-set rotations. What do you do when you get the long 4x1 piece? If you rotate it twice, should it really be one full space over to the side (which it would be if you use pure math)?
bigmattyh
CPU cycles are not an issue in this case. Do not try to overoptimize by thinking about "wasting CPU cycles". Whether you store 4 different permutations and select one, or loop through and rotate the array, either way will be so minimal a time span that it won't matter at all. Down vote.
Ricket
I have a 2.4GHz Core 2 Duo processor, not to mention that a double for loop that checks an integer is doing a comparison of 8 bytes times 16. That's almost nothing at all for the CPU. Plus what if I one day wanted a 5x5 matrix? I'd have to recode it all.
Brock Woolf
+5  A: 

Don't rotate the pieces with code. Just store an array of the different piece orientations and cycle through them when the piece is rotated. There's no need to dynamically rotate them in a Tetris game.

As the problem domain is Tetris, you will find that a rotation algorithm causes undesirable effects, such as the long thin Tetronimo not alternating between two positions (as it does in the real thing).

Jon
-1; There is no advantage to storing 4 orientations of each piece rather than just rotating. You should be focused on answering his question, not discouraging him, unless he's making a critical mistake; which he certainly is not.
Ricket
It would take me far less time to write a double for loop as was suggested above.
Brock Woolf
Ricket, easy on the tone there. I have updated my answer to indicate why rotation is a bad idea in this case.
Jon
It's not about the time it takes you to write as a developer, it's all about the time it takes to execute. Remember, compilers might be good at optimising loops, but they're even better when it comes to constants.
iAn
I agree with Jon, I've just been contemplating this problem myself. If you rotate the array, the blocks don't rotate the same way as they do in the original game. They appear to rotate around the centre.
devilcrack
+1  A: 

I'd store (x, y) coordinates of the "cells" and use rotation matrix to rotate them. See Drawing a Rotated Rectangle for example. You probably have to round the result to closest 0.5 increment.

eed3si9n
A: 

A detailed explanation of Array rotation problem with 7 possible solution with Code and Time & Space Complexities at

http://www.rawkam.com/?p=1008

sunil