0
\$\begingroup\$

I am making a Tetris clone. Is there any way that I can avoid using a 2D array for storing a Tetris block?

EDIT: Suppose I have an array to store an L-shaped piece:

1 0
1 0
1 1

But for the sake of rotation, I do that shape as this:

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

I can do this in either objects or arrays. If I use objects then create four objects for each "square", and I can store the position of each square, and rotate it about the piece's center. But I am finding a better way to optimally perform a rotation on the piece. I believe rotating this array 90 degrees would take O(n^2) time. I try to avoid making rotated copies, and instead rotated on the fly. What algorithm or approach should I use?

\$\endgroup\$
  • 6
    \$\begingroup\$ You should use whatever's easiest. This doesn't seem like it's worth optimizing, unless the profiler tells you you're spending a lot of time there. O(n^2) hardly matters when n is 4. \$\endgroup\$ – amitp Dec 14 '14 at 17:51
1
\$\begingroup\$

If you really really really wants to you can store the data as a 1D array with each 2 pairs means an block's x and y position. Like so [-2,0,-1,0,1,0,2,0] with (0,0) being the implicit center of rotation. You can improve on this further by not using array of ints but pack your data into the smallest bits possible.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.