Tetris

Normal 3 Exclusive 10s 256 MB
Data StructuresOOP GSA

Implement a Tetris engine without rotation.

Pieces from a fixed queue fall one at a time into a board W columns wide and H rows tall. Commands move the falling piece, drop it, or print the board. A piece locks when it is pushed down and cannot fall, full rows clear, and the next piece in the queue appears at the top.

Nothing rotates: each piece keeps the orientation shown here for its whole life. # marks a cell of the piece, and each picture is the piece's bounding box.

Text
I      O     T      S      Z      J      L
####   ##    .#.    .##    ##.    #..    ..#
       ##    ###    ##.    .##    ###    ###
  • Row 0 is the top of the board and column 0 is its left edge.

  • The first piece in the queue spawns before the first command, and the next one spawns each time a piece locks.

  • A piece spawns with the top row of its bounding box on row 0 and the left column of its box on column floor((W - w) / 2), where w is the width of the box: 4 for I, 2 for O, 3 for every other piece.

  • If any cell of the new piece would land on a locked block, the game is over: print GAME OVER and do not place the piece. No piece ever spawns again.

  • If the queue is empty when a piece locks, nothing spawns.

  • LEFT and RIGHT move the falling piece one column, but only if every cell of the moved piece is inside the board and on an empty cell. Otherwise nothing happens.

  • DOWN moves the falling piece one row down under the same rule. If it cannot move down, it locks where it is instead.

  • DROP moves the falling piece down as many rows as it can, then locks it.

  • PRINT prints the board.

  • While no piece is falling (after a game over, or once the queue runs out), LEFT, RIGHT, DOWN and DROP do nothing. PRINT still prints.

  • Locking turns every cell of the piece into a block labelled with the piece's letter.

  • Then every row whose cells all hold blocks is removed. Each row above moves down one row for every removed row below it, and empty rows enter at the top. Blocks move only with their row: they never fall into gaps.

  • A lock that removes one row scores 100 points and one that removes two rows scores 300. No piece is more than two rows tall, so a lock never removes more than two.

  • The next piece spawns after the rows are removed.

The first line is W H. The second line is the queue: every piece in spawn order, as one string of letters from IOTSZJL. Each remaining line is one command: LEFT, RIGHT, DOWN, DROP or PRINT.

Each PRINT writes SCORE <score> LINES <lines>, where lines counts every row removed so far, followed by the board's H rows from row 0 down. A row is W characters: . for an empty cell, the piece's letter for a locked block, and @ for a cell of the falling piece.

A game over writes GAME OVER on its own line at the moment it happens. Nothing else is printed.

  • A piece that comes to rest on something is not locked yet. It can still slide with LEFT and RIGHT, and if it slides over a gap, the next DOWN carries it further down.

  • Only the cells of a spawning piece decide a game over. A block inside the empty corner of its bounding box does not end the game.

  • Rows clear before the next piece spawns, so a lock that fills the top rows can still clear them in time.

  • 4≤W≤404 \le W \le 40, 2≤H≤402 \le H \le 40
  • The queue holds between 11 and 10410^4 pieces
  • At most 10510^5 commands, at most 100100 of them PRINT
  • Every line is well formed
Example 1
Input
4 4
IO
PRINT
DROP
PRINT
Output
SCORE 0 LINES 0
@@@@
....
....
....
SCORE 100 LINES 1
.@@.
.@@.
....
....
Explanation

The I spawns on columns 0 to 3, since floor((4 - 4) / 2) = 0, and PRINT shows it as @. DROP takes it to row 3, where it locks and fills the row, so the row clears for 100 points. The O then spawns at column floor((4 - 2) / 2) = 1.

Example 2
Input
6 5
OIT
RIGHT
RIGHT
DROP
DOWN
DOWN
PRINT
LEFT
DOWN
DOWN
DOWN
PRINT
Output
SCORE 0 LINES 0
......
......
.@@@@.
....OO
....OO
SCORE 100 LINES 1
..@...
.@@@..
......
......
....OO
Explanation

The O slides to the right wall and drops. The I falls two rows and comes to rest on the O, which does not lock it: LEFT slides it off the ledge, two DOWNs carry it to the floor, and the third DOWN, which cannot move it, locks it. Row 4 is now full and clears, the O's top row moves down into row 4, and the T spawns.

Example 3
Input
4 3
TOI
DROP
LEFT
DROP
PRINT
Output
GAME OVER
SCORE 0 LINES 0
....
.T..
TTT.
Explanation

The T drops and locks on rows 1 and 2. The O would spawn on columns 1 and 2 of rows 0 and 1, and its lower left cell is the T's top block, so the game ends during the first DROP. The LEFT and DROP after it do nothing, the I never spawns, and PRINT still prints the board.

Accepted 5/10
Acceptance 50%
Loading editor...
Input
4 4
IO
PRINT
DROP
PRINT
Expected output
SCORE 0 LINES 0
@@@@
....
....
....
SCORE 100 LINES 1
.@@.
.@@.
....
....