Chris Evans

ceva24.dev | Thoughts, code and everything in-between

The Tetris Randomiser in 20 Lines of Code

2015-07-17

The Tetris Randomiser that chooses the next tetromino, implemented in Groovy. It could have been shorter, but with efficiency trade-offs.

I chose to implement the TGM2 variant, as described on the Tetris Wiki.

(Update: the wiki has since been updated with a version of the algorithm that is slightly different from the one described when this post was originally made – I’ve written a new version of the algorithm that is 35 lines long!)

def rng = new Random()
def blocks = ['T''I''L''J''S''Z''O']
def history = ['Z''S''Z''S'] as ArrayDeque
def maxTilePosition = 4
 
def nextPiece = {
 
    def block
 
    for (i in 1..6) {
        block = blocks[rng.nextInt(maxTilePosition)]
        if (!(block in history)) break
    }
 
    maxTilePosition = 7
 
    history.remove()
    history.add(block)
 
    return block
}

It can be called by doing something like:

20.times { print nextPiece() }

With output:

TIJLZTSIJLOSIZTJSOLI
Chris Evans profile picture
CHRIS EVANS
Senior Engineering Manager at the LEGO Group, UK