Skip to content

Instantly share code, notes, and snippets.

@ceva24
Last active September 6, 2015 13:55
Show Gist options
  • Select an option

  • Save ceva24/065d64d2b7cbbe7cfe29 to your computer and use it in GitHub Desktop.

Select an option

Save ceva24/065d64d2b7cbbe7cfe29 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', 'Z', 'S', 'S'] as ArrayDeque
def first = true
def nextPiece = {
def block
if (first) {
block = blocks[rng.nextInt(4)]
history.removeFirst()
history.addFirst(block)
first = false
}
else {
for (i in 1..6) {
block = blocks[rng.nextInt(7)]
if (!(block in history)) break
}
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