Tetris Board
Jane StreetOnsiteMedium
You are building the board of a falling-block game. The board has width columns and height rows, numbered from 0 at the bottom, and starts empty.
Pieces are named "I", "O", "T", "L" and "S". Their cells at rotation 0, written (x, y) with y pointing up, are:
I:(0,0) (1,0) (2,0) (3,0)O:(0,0) (1,0) (0,1) (1,1)T:(0,0) (1,0) (2,0) (1,1)L:(0,0) (1,0) (2,0) (2,1)S:(0,0) (1,0) (1,1) (2,1)
Rotation r turns the piece clockwise r times — each turn maps (x, y) to (y, -x) — then shifts it so its smallest x and smallest y are both 0.
place(piece, rotation, col) drops the piece with its leftmost cells in column col, straight down from above the board. It falls until one of its cells would overlap a filled cell or go below row 0, and rests there; it never slides. It returns True if the piece was placed. It returns False, leaving the board unchanged, if some column of the piece lies outside the board or any cell would rest in a row >= height.
heights() returns, for each column, one more than the row of its highest filled cell (0 for an empty column). board() returns the rows from top to bottom as strings of # (filled) and . (empty).
Each example drives one Tetris instance: init holds the constructor arguments and operations lists the calls; the output lists every call's return value.
Part 1: Placing pieces
Implement the constructor, place, heights and board as described above.
- Example 1
- init
[5,4]- operations
[["place",["O",0,0]],["place",["I",0,0]],["place",["O",0,0]],["heights",[]],["board",[]]]- Output
[true,true,false,[3,3,3,3,0],[".....","####.","##...","##..."]]
Why: The first
Olands in columns 0-1 on rows 0-1. The horizontalIrests on top of it at row 2 and hangs over columns 2 and 3, leaving empty cells beneath. A secondOwould need rows 3-4, and the board has only 4 rows, soplacereturnsFalseand nothing changes.
Constraints
4 <= width <= 204 <= height <= 300 <= rotation <= 3-1 <= col <= widthpieceis one of"I","O","T","L","S"1 <= len(operations) <= 10^3