I like Tetris a lot, but I'm not very good at it. Just once I'd like to see that spaceship take off in front of my own eyes! And since computers are oh-so great at everything, the only possible solution is to make a program to play it for me... except you're going to do that bit for me!
Given a tetromino (shape made of four squares) and a map of the playing field, you are to place the tetromino such that it scores the greatest number of lines (makes the greatest number of rows completely full of blocks) and creates the least number of new holes (an empty space that cannot "see" the top of the playing field1).
Input
The input will contain a character on a single line that represents the dropping tetromino, followed by a 10*18 grid2 of spaces () and plus signs (
+
).
The character represents any of the seven base tetrominoes found in Tetris. All of the pieces can be rotated by 90 degrees, but not flipped. All the tetrominoes and their rotations are as follows:
#
S = ## ##
## #
#
Z = ## ##
## #
# ### ##
L = # # # #
## # ###
# ### ##
J = # # # #
## # ###
# # #
T = ### ## ### ##
# # #
O = ##
##
#
I = # ####
#
#
The grid represents the playing field of Tetris, with +
being previously-placed blocks. So, an example input might be the following:
I
+ ++
+ +++++
++ +++++++
++ +++++++
++ +++++++
++ +++++++
++++++ +++
Output
Your output will be identical to the input, but with the tetromino in the ideal position. The tetromino should be represented with #
to differentiate them from the pre-placed blocks. In addition to this, you are also to output how many lines/holes your placement creates in the form xL yH
on a new line.
The output for the example given above would be the following3:
I
+ ++
+ +++++
++#+++++++
++#+++++++
++#+++++++
++#+++++++
++++++ +++
4L 0H
You are to output only the best result(s); in the case of two or more cases giving the same score, you are to output all of them (separated by a blank line). The best results are to be determined by sorting by the number of lines scored (descending) first, then the number of new holes created (ascending). So, 1L 1H
is a better score than 0L 0H
.
I will work on creating a list of various inputs and expected output(s) that you can test your program against. Watch this space.
Rules and Disambiguation
- This is code-golf, so shortest correct implementation wins.
- Input/output may be in any medium that suits your target language (e.g. file, stdin/stdout, text area).
- If your target language does not support multiple line input (or it is inconvenient to do so), you may instead delimit each line of the input with commas (
,
). - You may omit the output of any blank lines in the grid.
- Remember that the tetromino falls from above - you may not place the piece "underground". You may therefore assume that all possible placements of the piece will be at "surface-level" (i.e. there are no blocks between the piece and the top of the board).
- Assume that there will never be a situation in which you are forced into a game over (the placed tetromino touches the top-center of the field).
- Solutions that are identical in output must be omitted (e.g. there are 3 solutions outputs if you naively rotate the
O
piece).
1 I'm aware that this will create some false positives, but it's a simplification.
2 This is the grid size used in the Game Boy version.
3 Yes, 0H
is correct. Check again, I said new holes ;^)