Last active
August 29, 2015 14:57
-
-
Save ceva24/2707f91c129eb48bb98e to your computer and use it in GitHub Desktop.
The Tetris Randomizer algorithm implemented in Groovy. Specifically the TGM2 variant.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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