Skip to content

Instantly share code, notes, and snippets.

@ceva24
Last active August 29, 2015 14:57
Show Gist options
  • Select an option

  • Save ceva24/2707f91c129eb48bb98e to your computer and use it in GitHub Desktop.

Select an option

Save ceva24/2707f91c129eb48bb98e to your computer and use it in GitHub Desktop.
The Tetris Randomizer algorithm implemented in Groovy. Specifically the TGM2 variant.
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment