Problem Statement
In a well known computer game you drop tetramino pieces into a playfield. The goal of the game is to completely fill lines in the playfield. Given a partially filled playfield and a piece, you are to determine the maximum number of complete lines you can create by dropping this piece.
The playfield is between 4 and 10 tiles in width (inclusive) and between 4 and 20 tiles in height (inclusive). There are 7 different tetramino pieces, each occupying 4 tiles:
1) ### 2) ### 3) ## 4) ## 5) ## 6) ### 7) #### # # ## ## ## #
Before dropping a piece, you may rotate it 90, 180 or 270 degrees, or not at all. Then you select a horizontal position where you want to drop the piece. This rotation, and the selecting of the horizontal position, can be considered done above the playfield. Thus, there will never be a hindrance in the actual rotation or horizontal movement (see example 1). However, once the horizontal position and rotation have been decided, they can't be changed while the piece is being dropped (see example 0).
The final location of the piece after it has been dropped must be fully within the playfield. If it's not possible to drop a piece due to this rule, it is game over and you should return -1 (see example 2).
Create a class Tetramino which contains the method maxLines which takes a
Definition
- Class:
- Tetramino
- Method:
- maxLines
- Parameters:
- String[], int
- Returns:
- int
- Method signature:
- int maxLines(String[] playfield, int piece)
- (be sure your method is public)
Notes
- It's not allowed to slide a piece. After the initial (optional) rotation and horizontal movement, it must be possible for the piece to fall straight down to its final location without additional horizontal movement or rotations.
- A piece may not stay in midair. It must be dropped as far as possible, until it either hits the bottom of the playfield or an obstacle which blocks the dropping.
- The piece will begin falling above the playfield.
Constraints
- playfield will contain between 4 and 20 elements, inclusive.
- The length of each element in playfield will be between 4 and 10, inclusive.
- All elements in playfield will be of the same length.
- Each element in playfield will only contain the characters '.' and '#'.
- Each element in playfield will contain at least one '.'.
- piece will be between 1 and 7, inclusive.
Examples
{".......", "##..###", "#.....#", "#.....#", "##....#", "####..#", "####..#"}
3
Returns: 0
Since it's not possible to move the piece to the right once it has passed the second line, it's not possible to complete any lines. Thus the method should return 0.
{".####", ".#..#", ".##.#", "..###", ".####", ".####", ".####", ".####", ".####", ".##.#", ".####", ".####", "#.###", "####."}
7
Returns: 3
It's possible to rotate and put the piece into the leftmost column. This will complete 3 lines, so the method should return 3.
{"#.########", "##.#######", "######.###", "#....#####", "#...##.###"}
2
Returns: -1
The playfield is almost filled and the piece can't be placed anywhere since it has to fall from above the playfield. The method should return -1.
{".....", ".....", "###..", "###..", "####.", "####.", "####."}
4
Returns: 2
By rotating and dropping the piece to the far right, it's possible to clear 2 lines.
{".....", ".....", "###..", "###..", "####.", "####.", "####."}
5
Returns: 1
{".#########","...#######","######....","..........","..........","..........",".........."}
1
Returns: -1
{"##.######.","##.##.####","##.#######","##.#####.#","######.###","#########.","########.#","#######.##","######.###","#####.####","####.#####","###.######","##.#######","#.########",".#########","#.########","##.#######","###.######","####.#####","#####.####"}
7
Returns: 1
{".......",".......","####...","######.","......."}
1
Returns: 2
{".......",".......",".......",".......","####..."}
1
Returns: 1
{".......",".......",".......",".......","#####.."}
1
Returns: 1
{".......",".......","#####..","#......","#......"}
1
Returns: 1
{".......",".......","####...","####.##","......."}
2
Returns: 2
{".......",".......",".......",".......","####..."}
2
Returns: 1
{".......",".......",".......",".......","#####.."}
2
Returns: 1
{".......",".......","#####..","#......","#......"}
2
Returns: 1
{".......",".......","###..##","###..##","...#..."}
3
Returns: 2
{".......",".......",".......",".......","###..##"}
4
Returns: 1
{".......",".......",".......","#####..","######."}
4
Returns: 2
{".......",".......",".......","..#####",".######"}
5
Returns: 2
{".......",".......",".......",".......","#####.."}
5
Returns: 1
{".......",".......",".......",".......","###..##"}
5
Returns: 1
{".......",".......",".......","#####..","######."}
5
Returns: 1
{".......",".......",".......","..#####",".######"}
4
Returns: 1
{".......",".......",".......",".......","#####.."}
4
Returns: 0
{".......",".......",".......","###...#","......."}
6
Returns: 1
{".......",".......","###...#",".......","......."}
6
Returns: 0
{"........",".......#",".......#","#####..#","######.#"}
6
Returns: 2
{".......",".......","##.....","##..###","##.####"}
6
Returns: 2
{".......",".......",".......","##...##"}
6
Returns: 1
{"........","........","........","........","##....##"}
7
Returns: 1
{"........","........","........","##....##","........"}
7
Returns: 0
{".###",".###",".###",".###"}
7
Returns: 4
{"###.","#.#.",".##.","##.."}
7
Returns: 1
{".###",".###",".###","###."}
7
Returns: -1
{"###.","##.#","#.##",".###"}
7
Returns: -1
{"....","#.##","#.##","#.##"}
1
Returns: 2
{"....","#.##","#.##","#.##"}
2
Returns: 2
{"....","#.##","#.##","#.##"}
6
Returns: 1
{"#.##","#.##","#.##","#.##"}
7
Returns: 4
{"......","......",".#..##","......","##..##"}
4
Returns: 0
{"......","......",".#..##","......","##..##"}
5
Returns: 0
{".....",".....",".....","....."}
7
Returns: 0
{"....","....","....","...."}
7
Returns: 1
{"#........#",".#......#.","..#....#..","...#..#...","....##....","....##....","...#..#...","..#....#..",".#......#.","#........#","#........#",".#......#.","..#....#..","...#..#...","....##....","....##....","...#..#...","..#....#..",".#......#.","#........#"}
2
Returns: 0
{"#.........","#.########","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........",".........."}
6
Returns: -1
{"..........","..........","..........","..........","..........","..........","..........",".######...","########..","########..","#########.","#.......#.",".#.....##.","..#...#.#.","...#.#..#.","....#...#.","...#.#..#.","..#...#.#.",".#.....##.","#.......#."}
4
Returns: 2
{"..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........","..........",".........."}
6
Returns: 0
{ "#.########", "##.#######", "######.###", "#....#####", "#...##.###" }
2
Returns: -1