Various Tetris AI implemented in C++. The code has been fine-tuned to avoid unnecessary copying and memory allocations.
Tetris varies a lot between implementations. The following rules are used:
- 10x20 board
- 1 piece at a time, 0 piece lookahead, no holding piece
- Hard drops only
- Piece starts from row 0 (some implementations use hidden row 21-23)
- Game is over when piece overflows row 0
uniform
: Randomly pick any of the 7 piece using a uniform PRNG7bag
: The Random Generatornes
: NES randomizer, but LFSR is only scrambled on piece generation (in original NES Tetris, LFSR is scrambled many times in between spawns)nesApprox
: NES randomizer, but approximated as first-order Markov process
eltetris
: El-Tetrisyiyuan
: The (Near) Perfect Bot
Each AI implements the following function signature:
DropMove yourOwnAI(const Board &board, PieceType piece) {
// do stuff
}
Compile:
$ make
Usage: tetris [options]
Optional arguments:
-h --help shows help message and exits [default: false]
-v --version prints version information and exits [default: false]
-a --ai AI to use [default: "eltetris"]
-r --randomizer randomizer to use [default: "7bag"]
-s --seed RNG seed [default: 0]
-p --pieces Number of pieces to generate [default: 1000000]
Example:
$ bin/tetris -a eltetris -randomizer 7bag -s 123 -p 100000
ai=eltetris
randomizer=7bag
seed=123
pieces=100000
10000
20000
30000
40000
50000
60000
70000
80000
90000
pieces=100000, lines cleared=39999
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
.O..OO....
OOOOOO.O..