# Determine position of a rotated element in Tetris

Tetris-Tiles are stored as a 4x4 boolean matrix. Each rotation step has it's own matrix, the representation of the T-Block would look like this:

[
0, 0, 0, 0,
0, 1, 1, 1,
0, 0, 1, 0,
0, 0, 0, 0
],
[
0, 0, 1, 0,
0, 0, 1, 1,
0, 0, 1, 0,
0, 0, 0, 0
],
[
0, 0, 1, 0,
0, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0
],
[
0, 0, 1, 0,
0, 1, 1, 0,
0, 0, 1, 0,
0, 0, 0, 0
]


I'm trying to find a way to calculate the position of the block when it's being rotated and collides with the board (the board is also a matrix). The original Tetris would simply not allow rotation of a block when the rotation would result in a collision. Modern variants of the game will resolve the collision and move the block to a valid position.

Here are some situations that should be solved. The board is 6x6, red = active block, grey = placed/occupied blocks. Each time, a counter-clockwise rotation should be performed. The green overlay indicates the matrix for the block. The arrow indicates the resulting correction to resolve the rotation:

1. Block is at the left side of the board. Since the block cannot leave the board, it should be moved back inside after a rotation.
2. Block hits "ground", but isn't placed/committed yet. In this case, the tile must be moved up to resolve the collision (in case of a "I"-Block, the movement would be 2 cells up).
3. Tile would hit occupied blocks, must be moved left to resolve collision.
4. Tile cannot be rotated.

What would be the best approach to tackle this problem? Optimally, the solution should be generic, eg. work with arbitrary 4x4 matrix blocks on an arbitrarily sized and populated board.

• Interesting.. I did a tetris a year ago and i just rotated the matrix and kept the original. If something overlapped i used the orginal. Worked fine. I testplayed quite often and never ran into problems. Must have been just lucky. Thanks for the interisting topic! Commented Nov 14, 2014 at 10:55

The situation you describe is called a "wall kick".

A wall kick happens when a player rotates a piece when no space exists in the squares where that tetromino would normally occupy after the rotation.

...

The simplest wall kick algorithm ... is to try moving the tetromino one space to the right, and then one space to the left, and fail if neither can be done.

There are various Tetris rotation systems, all documented on Wikia: Rotation Systems

SRS is the "official" Tetris specification, and it has quite a complex algorithm for wall kicks involving tables. The final piece may not even overlap the original piece at all!

The DTET rotation system extends the simplest algorithm by checking five other wall kicks besides just right and left. All pieces follow the same rules.

• +1, Interesting wiki. Although moving the I block one space left or right won't work. The more advanced rotation systems also seem to apply different rules for different blocks.. I'd like to find a method that would work for all block possibilities (if not too costly). Commented May 13, 2011 at 15:24
• @bummzack: Well, then just use the same table for all the blocks like the DTET system. It simply tests more locations besides left and right. You can create your own system and check however many positions you want. However, using the same table for all pieces might allow "unnatural" wall kicks. Commented May 13, 2011 at 16:05
• Thanks for the edits. These are actually much better than the material on wikia. Much appreciated. Commented May 13, 2011 at 16:50