Tetris Simulation
Time limit5sMemory limit512 MB
Simulate falling tetrominoes on a small board, clear full rows after each drop, and print the final field or game over.
- Level
Medium4 of 10
- Topics
- Simulation, Matrix
- Solved
- No attempts yet
Problem
Tetris is a video game that many people have played. Here you simulate a version of it with simpler rules.
The field is columns wide and rows tall, and it starts empty. Columns are numbered to from left to right, and rows are numbered to from top to bottom. Tetrominoes fall one at a time, each starting entirely above the top of the field. A falling piece moves down one row at a time and stops as soon as one more row of movement would push part of it below the bottom row or onto a square that an earlier piece occupies. A piece never moves sideways and never rotates while it falls.
Right after a piece stops, every row that is completely filled is cleared. Several rows can be cleared at once. The cleared rows disappear, every row above them moves down by the number of cleared rows, and the same number of empty rows appears at the top. Here is an example, where o marks the piece that just stopped.
|..............| |..............| |..............|
|.............o| |..............| |..............|
|.............o| |..............| |..............|
|.............o| |..............| |..............|
|.............o| |..............| |..............|
|..xx..........| --> |..xx..........| --> |..............|
|xxxxxxxxxxxxx.| |xxxxxxxxxxxxxo| |..............|
|xxxxxxxxxxxxx.| |xxxxxxxxxxxxxo| |..xx..........|
|xx..xxxxxxxxx.| |xx..xxxxxxxxxo| |xx..xxxxxxxxxo|
|xxxxxxxxxxx...| |xxxxxxxxxxx..o| |xxxxxxxxxxx..o|
---------------- ---------------- ----------------
Falling Stopped Cleared 2 rows
In this simpler version, squares left hanging in the air after a clear do not fall any further. The two squares near the top keep their columns and only move down by the two cleared rows. Chain clears therefore never happen, even though the original game has them.
The game ends once all pieces are placed. It also ends early if a piece stops at a position where at least one of its squares is still above row : the field is not tall enough for that piece, so the piece cannot be placed.
The type, the rotation and the horizontal position of every piece are given in the input. Simulate the play and report the final field.
Input
There are 7 types of tetromino.
1 2 3 4 5 6 7
x x x x xx x x
xx xx x x xx x xxx
x x xx xx x
x
The rotation of a piece is an integer with . The shapes above are the shapes for , and each increase of by one turns the shape 90 degrees counterclockwise. The bounding box is taken again after every turn, so the shape for a given always sits tight against its own bounding box. The four rotations of type 7 and of type 1 look like this.
r=0 r=1 r=2 r=3
x x xxx x
xxx xx x xx
x x
x xx x xx
xx xx xx xx
x x
The horizontal position is an integer . It is the column of the left edge of the piece's bounding box, so a piece whose bounding box is columns wide covers columns to .
The first line has the number of test cases . Each test case starts with a line holding three integers , , : the width of the field, its height, and the number of falling pieces. Then follow lines. The -th of them holds three integers , , : the type, the rotation and the horizontal position of the -th piece. Every keeps its piece inside the field horizontally. All lines are given even when the game ends before the last piece.
Output
For each test case, print one line holding "Case #i:", where is the number of the test case counting from 1. Then, if the game ended before all pieces were placed, print "Game Over!" without the quotes. Otherwise print the final field as lines of characters each, using x for an occupied square and . for an empty one.