Background
Tetris is a single-player game played on a rectangular grid with tetromino pieces.
When you fill one or more lines with tetrominoes, the filled lines are removed, and all blocks above them move down accordingly. In the following diagrams, .
is an empty space, #
is an existing block, and the tetromino marked with A
s is the one just placed:
One line cleared example
#...AA.. -> ........
####AA## #...##..
---
Two lines cleared example
(note that the 3rd line moved down once, while the top line moved twice)
...A.... ........
###A#### ........
##.A.##. -> ...#....
###A#### ##.#.##.
####.### ####.###
Challenge
Two board states will be given as input. One is right before a specific tetromino appears (the left-side state of the above diagrams, without A
s), and the other is right after the tetromino is placed and line clears are completed (the right-side state). Given this information, recover the type of the tetromino placed between the two states, which is one of the following seven types:
O J L S T Z I
## # # ## # ## ####
## ### ### ## ### ##
You can assume the following:
- The input is valid; the two boards have the same dimensions, and the game state can be changed from the first to the second by placing a single tetromino. Also, the placed tetromino is completely inside the grid before line clearing occurs (i.e. it won't be placed above the ceiling of the given grid, even partially).
- The answer is unique.
- The width of the board is at least 5.
For this challenge, ignore the rotation rules of actual Tetris games, and assume that any tetromino placement is valid, as long as the tetromino does not overlap with existing blocks or float in the air. This allows placing a tetromino inside a closed room (which actually happens in some exotic games).
You can take the input as a matrix (or equivalent) consisting of two distinct values for spaces and blocks. You can output the type of the tetromino as one of seven distinct values of your choice. Allowed output formats include numbers, strings, and possibly nested or multi-dimensional arrays of numbers and/or strings.
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
The notation is the same as the above example. Before
and After
are the two input grids, Answer
is the expected output (given as alphabet code), and Explanation
shows where the tetromino is placed.
Before:
......
......
After:
..##..
..##..
Answer: O
Explanation:
..AA..
..AA..
----------------
Before:
..####
...###
#.####
After:
......
......
..####
Answer: T
Explanation:
..####
AAA###
#A####
----------------
Before:
...###
...###
#.....
After:
......
..####
#.....
Answer: L (J doesn't work since the piece will be floating)
Explanation:
..A###
AAA###
#.....
----------------
Before:
##..##
##..##
##..##
After:
......
###.##
##.###
Answer: S
Explanation:
##A.##
##AA##
##.A##
----------------
Before:
##.##
##..#
##.##
After:
.....
.....
.....
Answer: T
Explanation: self-explanatory
----------------
Before:
.###.
#...#
.....
#...#
.###.
After:
.....
.###.
#...#
..#..
.###.
Answer: T
Explanation:
.###.
#...#
..A..
#AAA#
.###.