A basic Tetris game simulator where pieces never rotate and are just dropped onto a grid. The grid provides methods to add pieces and exposes its current height expressed as number of non-empty rows.
The game accepts sequences of Tetris pieces, one sequence per line, and will return the grid height once all the pieces have been added to the grid. Pieces and positions can be specified in the following format:
Q2,T3,S6
where each letter identifies a Tetris piece with a specific shape:
Qis the squareIis the straight piece, laid out horizontallyT,Z,S,J,Lhave the same shape as their letter
and the number represents the leftmost column occupied by this piece when dropped into the grid.
For example, the sequence above will result in a grid of height 3 and the following layout:
| |
| TTT |
| QQT SS |
| QQ SS |
------------
while the following sequence:
I0,I0,Q4,I6,I6
will result in an empty grid as two full lines will be eliminated.
The program requires python 3 (it was tested with version 3.8.5). After checking out the project, run it like this:
$ python src/tetris/cmd.pyand it accepts alternatively:
- a single file name argument:
$ echo "Q1,Q3" > in.txt
$ python src/tetris/cmd.py in.txt
2- sequences of pieces as arguments:
$ python src/tetris/cmd.py "Q0,Q2,Z0,I2,I6" "I3,Z2,S5"
4
3- piped input
$ echo "Q1,Q3,Z0,Z2,T1" | python src/tetris/cmd.py
6In order to run tests you will need poetry - it's also a good idea to make sure your pip is up to date:
python -m pip install --upgrade pip
pip install poetrywith poetry installed you can then run the tests locally:
poetry run pytest