Last active
September 6, 2015 13:55
-
-
Save ceva24/065d64d2b7cbbe7cfe29 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', '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