For testing, I temporarily created a 'main' method in the 'Field' class.
public void removeFullLines() {
// Looking for partially filled rows in the
// original matrix (from bottom to top)
int[][] tempMatrix = new int[height][width];
// Current row index for tempMatrix
int rowIndex = height - 1;
for (int i = height - 1; i >= 0; i--) {
int count = 0;
for (int j = 0; j < width; j++)
count += matrix[i][j];
// Find partially filled lines and fill tempMatrix
// with them (from bottom to top)
if (count > 0 && count < width) {
System.arraycopy(matrix[i], 0, tempMatrix[rowIndex], 0, width);
rowIndex--;
}
}
// tempMatrix is copied into the source matrix.
for (int i = 0; i < height; i++)
System.arraycopy(tempMatrix[i], 0, matrix[i], 0, width);
}
public static void main(String[] args) {
Field field = new Field(10, 5);
// Randomly fill in the lines. The top two lines are only zeros.
for (int i = 2; i < field.height; i++) {
for (int j = 0; j < field.width; j++) {
int random = (int) (Math.random() * 7);
if (random != 4)
field.matrix[i][j] = 1;
}
}
System.out.println("Before calling the method:");
for (int i = 0; i < field.height; i++)
System.out.println(Arrays.toString(field.matrix[i]));
System.out.println();
field.removeFullLines();
System.out.println("After calling the method:");
for (int i = 0; i < field.height; i++)
System.out.println(Arrays.toString(field.matrix[i]));
System.out.println();
}
/*
CONSOLE OUTPUT(testing result):
Before calling the method: After calling the method:
[0, 0, 0, 0, 0] [0, 0, 0, 0, 0]
[0, 0, 0, 0, 0] [0, 0, 0, 0, 0]
[0, 1, 1, 0, 1] [0, 0, 0, 0, 0]
[1, 0, 1, 1, 1] [0, 0, 0, 0, 0]
[0, 1, 1, 1, 1] [0, 0, 0, 0, 0]
[0, 0, 1, 1, 1] [0, 1, 1, 0, 1]
[1, 1, 1, 1, 1] [1, 0, 1, 1, 1]
[1, 1, 1, 0, 0] [0, 1, 1, 1, 1]
[1, 1, 1, 1, 1] [0, 0, 1, 1, 1]
[1, 1, 1, 1, 1] [1, 1, 1, 0, 0]
Before calling the method: After calling the method:
[0, 0, 0, 0, 0] [0, 0, 0, 0, 0]
[0, 0, 0, 0, 0] [0, 0, 0, 0, 0]
[1, 1, 1, 1, 1] [0, 0, 0, 0, 0]
[1, 1, 0, 0, 1] [0, 0, 0, 0, 0]
[1, 0, 1, 0, 1] [0, 0, 0, 0, 0]
[0, 0, 1, 1, 1] [0, 0, 0, 0, 0]
[1, 1, 1, 1, 1] [1, 1, 0, 0, 1]
[1, 0, 1, 1, 1] [1, 0, 1, 0, 1]
[1, 1, 1, 1, 1] [0, 0, 1, 1, 1]
[1, 1, 1, 1, 1] [1, 0, 1, 1, 1]
Before calling the method: After calling the method:
[0, 0, 0, 0, 0] [0, 0, 0, 0, 0]
[0, 0, 0, 0, 0] [0, 0, 0, 0, 0]
[1, 1, 1, 1, 0] [0, 0, 0, 0, 0]
[1, 1, 1, 1, 1] [0, 0, 0, 0, 0]
[1, 1, 1, 1, 1] [0, 0, 0, 0, 0]
[1, 1, 1, 0, 1] [0, 0, 0, 0, 0]
[1, 1, 1, 1, 1] [0, 0, 0, 0, 0]
[1, 1, 1, 1, 1] [1, 1, 1, 1, 0]
[1, 1, 1, 1, 1] [1, 1, 1, 0, 1]
[1, 1, 0, 1, 1] [1, 1, 0, 1, 1]
*/
package com.codegym.task.task22.task2213;
/**
* The Field class describes the Tetris game field
*/
public class Field {
// Width and height
private int width;
private int height;
// Matrix representing the field: 1 means that part of the field is occupied, 0 means it is available
private int[][] matrix;
public Field(int height, int width) {
this.width = width;
this.height = height;
matrix = new int[height][width];
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int[][] getMatrix() {
return matrix;
}
/**
* The method returns the value of the matrix at coordinates (x, y)
* If the coordinates are outside the matrix, the method returns null.
*/
public Integer getValue(int x, int y) {
if (x >= 0 && x < width && y >= 0 && y < height)
return matrix[y][x];
return null;
}
/**
* The method sets the matrix cell with coordinates (x, y) to the passed value
*/
public void setValue(int x, int y, int value) {
if (x >= 0 && x < width && y >= 0 && y < height)
matrix[y][x] = value;
}
/**
* The method displays the current contents of the matrix on the screen
*/
public void print() {
// Create an array where we will "draw" the current game state
int[][] canvas = new int[height][width];
// Copy the game field matrix into the array
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
canvas[i][j] = matrix[i][j];
}
}
// Copy the game piece into the array, only the non-empty cells
int left = Tetris.game.getGamePiece().getX();
int top = Tetris.game.getGamePiece().getY();
int[][] brickMatrix = Tetris.game.getGamePiece().getMatrix();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (top + i >= height || left + j >= width) continue;
if (brickMatrix[i][j] == 1)
canvas[top + i][left + j] = 2;
}
}
// Display what we've "drawn", but start from the edge of the frame.
System.out.println("---------------------------------------------------------------------------\n");
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int index = canvas[i][j];
if (index == 0)
System.out.print(" . ");
else if (index == 1)
System.out.print(" X ");
else if (index == 2)
System.out.print(" X ");
else
System.out.print("???");
}
System.out.println();
}
System.out.println();
System.out.println();
}
/**
* Remove the completed lines
*/
public void removeFullLines() {
// For example, like this:
// Create a list to store the lines
// Copy all the non-empty lines into a list.
// Add incomplete lines to the beginning of the list.
// Convert the list back into a matrix
int[][] tempMatrix = new int[height][width];
int rowIndex = height - 1;
for (int i = height - 1; i >= 0; i--) {
int count = 0;
for (int j = 0; j < width; j++)
count += matrix[i][j];
if (count > 0 && count < width) {
System.arraycopy(matrix[i], 0, tempMatrix[rowIndex], 0, width);
rowIndex--;
}
}
for (int i = 0; i < height; i++)
System.arraycopy(tempMatrix[i], 0, matrix[i], 0, width);
}
}