Tetris Board

Asked at Jane StreetOnsite

Reported by the problem's creator, not verified

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.

Examples

  1. Example 1

    Input: 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],[".....","####.","##...","##..."]]

    Explanation: The first O lands in columns 0-1 on rows 0-1. The horizontal I rests on top of it at row 2 and hangs over columns 2 and 3, leaving empty cells beneath. A second O would need rows 3-4, and the board has only 4 rows, so place returns False and nothing changes.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.