---
# System prepended metadata

title: Clearing Lines

---

# Clearing Lines
In order to implement Clearing Lines we must first create a new method called __clearLines()__. I think this should be part of the __mergeMatrix()__ method where it will be called. __clearLines()__ will implement another method called __checkForLines()__ which will check to see if any of the lines (that encompass the x axis length of the brick just placed) are now apart of a full line in the Board Matrix. __clearLines()__ will also implement the method __lowerBricks()__. __lowerBricks()__ will go the length of the y axis and check every position of the x axis (from the position of the cleared line) and lower all bricks above the position a set amount of length.

# A Detailed Look

Clearing Lines is fairly simple but it has a lot of moving parts. The bricks move around the board matrix until they reach the bottom of the board or until they encounter another brick.[^1] If this creates a full row of bricks then the row is cleared.

To implement this behavior we need a couple of methods:
1. __mergeMatrix()__

```java
   public static int[][] mergeMatrix(int[][] filledFields, int[][] brick, int x, int y) {
        int[][] copy = copyMatrix(filledFields);
        for (int i = 0; i < brick.length; i++) {
            for (int j = 0; j < brick[i].length; j++) {
                int targetX = x + i;
                int targetY = y + j;
                if (brick[j][i] != 0) {
                    copy[targetY][targetX] = brick[j][i];
                }
            }
        }
        return copy;
    }
```

__mergeMatrix()__ is the method used to fuse a brick with the game board. This is already in the overall Java project but it needs to be updated. This is where we will add in the __clearLines()__ method. This is TBD because the clear lines method can appear in other places especially if we want to create pacing for presenting visually all the different effects (the lines clearing visual, scoring visual, bricks falling visual, etc.)

2. __clearLines()__
```java
	public void clearLines(){
	int linesToBeCleared[] = mainBoard.checkForLines(); //will check to see if there are any lines that need to be cleared and return those lines x axis value into an array
        //we then will clear the lines from the board looping through the array
        //to achieve this we simply need to go to those rows in the board matrix and then change the whole row to 0s
	int numLinesCleared = linesToBeCleared.length;
	lowerBricks(linesToBeCleared);
	}
```

__clearLines()__ is the method that will be ran to check if any of the lines in which the brick that was just placed in will be cleared and thus scored. It will check all the rows that the brick just placed spans and then check to see if every x unit on the y axis is occupied. If it is then the line is cleared and added to an int variable (int __numLinesCleared)__. This int will be used for scoring.

3. __checkForlines()__ 

```java
 /*  This method will need to be ran on the background board that is used to maintain the status of the bricks that have already been placed.  
 We can also use this method to then get the length of the array we return for use in calculating the score multiplier. (length of the array would = lines cleared) The Array we return will never be over 4 since no brick is over 4 spaces in length */ 

 @Override  
 public int[] checkForLines() {  
 	ArrayList<Integer> rowsToClear = new ArrayList<Integer>(); //I do not believe you can return an ArrayList so this is used to have a dynamic array we can add to  
 	for(int i = 0; i < currentGameMatrix.length; i++){  
		boolean full = false; //will be made True if the row is full  
 		int numUsed = 0; //this variable will track to see how much of the row is full  
		
 		for(int y = 0; y < currentGameMatrix[i].length; y++){  
			if (currentGameMatrix[i][y] > 0) {  
				numUsed++;  
 			}  
		}  
            
		if(numUsed == currentGameMatrix[i].length){ //check to see if the whole array is full  
 		full = true;  
 		}  
        if(full == true){ //if the row is full then add it to the list  
 		rowsToClear.add(i);  
 		}  
	}  
        
	if(rowsToClear.size() == 0){ //if there are no rows to be cleared then we return null  
 		return null;  
 	}  
	
	int rowsToClearArray[] = new int[rowsToClear.size()]; //create an array that we can return to be used in another method  
 	
	for(int i = 0; i < rowsToClearArray.length; i++){ //add the elements of our ArrayList to the array we will return  
 		rowsToClearArray[i] = rowsToClear.get(i);  
 	}  
        return rowsToClearArray;  
	}  
}
```

__checkForLines()__ is a method that will go through all of the rows in the board matrix and put into an array all of the x values of the lines on the board that are full. This will tell the method __clearLines()__ which rows in the board matrix need to be cleared.

#### Update #1
checkForLines() will return null if there are no lines to be cleared in the board.
This is also not the most efficient way to check for lines because this method goes through all of the rows in the board. To make this more efficient we would need to only check the rows in which the most recent brick was placed.

4. __lowerBricks()__
```java
	public void lowerBricks(int[] linesToBeCleared){
//this will lower all the bricks above the last element of the lines cleared
//we will do something similiar to the other methods and loop through the board matrix
//this will be a loop that will go through all the rows above the first line cleared and
//lower all bricks until they come in contact with the bottom of the board or another row with bricks
	}
```

__lowerBricks()__ will take the array that has the rows that will be cleared (at this point already cleared) and lower all the bricks that are above that point down until it reaches another brick or the floor. It accomplishes this row by row.[^2]

[^1]: An interesting note about this behavior is how this works in an actual Tetris game. In an actual Tetris game the brick does not merge or become part of the game board for a second after it commits a movement. One can move the brick left and right and even change it's rotation before merging for a couple seconds. This specific behavior is a bit complex to recreate.

[^2]: Notice that all of the methods including mergeMatrix() loop through the board matrix. This can mean that all methods could be implemented at once and not as seperate methods to improve efficency. This would require some more advanced techniques (Recursion possibly?). Currently this implementation is inefficient as we loop through the board matrix ==four times(!)== every time a brick is placed.