limit piece matrix to their minimum size
This commit is contained in:
		@ -55,6 +55,8 @@ public class Piece extends Shape{
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    public void RotateRight(int times){
 | 
					    public void RotateRight(int times){
 | 
				
			||||||
        while(times > 0) {
 | 
					        while(times > 0) {
 | 
				
			||||||
 | 
					            height = matrix.length;
 | 
				
			||||||
 | 
					            width = matrix[0].length;
 | 
				
			||||||
            boolean[][] temp_matrix = new boolean[width][height];
 | 
					            boolean[][] temp_matrix = new boolean[width][height];
 | 
				
			||||||
            for (int i = 0; i < width; i++) {
 | 
					            for (int i = 0; i < width; i++) {
 | 
				
			||||||
                for (int j = 0; j < height; j++) {
 | 
					                for (int j = 0; j < height; j++) {
 | 
				
			||||||
@ -70,9 +72,10 @@ public class Piece extends Shape{
 | 
				
			|||||||
    public boolean equals(Object obj) {
 | 
					    public boolean equals(Object obj) {
 | 
				
			||||||
        if(obj instanceof Piece){
 | 
					        if(obj instanceof Piece){
 | 
				
			||||||
            Piece pieceObj = (Piece) obj;
 | 
					            Piece pieceObj = (Piece) obj;
 | 
				
			||||||
            if( pieceObj.getPosition().equals(this.getPosition()) && pieceObj.getShape().equals(getShape())) {
 | 
					            if (pieceObj.getPosition() != null && this.getPosition() != null){
 | 
				
			||||||
                return true;
 | 
					                return pieceObj.getPosition().equals(this.getPosition()) && pieceObj.getShape().equals(getShape());
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            return pieceObj.getShape().equals(getShape());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return false;
 | 
					        return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,8 @@
 | 
				
			|||||||
package school_project;
 | 
					package school_project;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import school_project.Utils.Array;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.Serializable;
 | 
					import java.io.Serializable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
@ -21,14 +23,52 @@ public class Shape implements Serializable, Cloneable{
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void setShape(boolean[][] matrix) throws IllegalArgumentException{
 | 
					    public void setShape(boolean[][] matrix) throws IllegalArgumentException{
 | 
				
			||||||
        height = matrix.length;
 | 
					 | 
				
			||||||
        width = matrix[0].length;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for (boolean[] row: matrix){
 | 
					        for (boolean[] row: matrix){
 | 
				
			||||||
            if(row.length != width){
 | 
					            if(row.length != matrix[0].length){
 | 
				
			||||||
                throw new IllegalArgumentException("The argument should be a square matrix");
 | 
					                throw new IllegalArgumentException("The argument should be a square matrix");
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (int i = 0; i < matrix.length; i++) {
 | 
				
			||||||
 | 
					            if(!Array.isRowOnlyFalse(matrix, i)) {
 | 
				
			||||||
 | 
					                for (int j = 0; j < i; j++) {
 | 
				
			||||||
 | 
					                    matrix = Array.MatrixRemoveRow(matrix, 0);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (int i = matrix.length-1; i >= 0; i--) {
 | 
				
			||||||
 | 
					            if(!Array.isRowOnlyFalse(matrix, i)) {
 | 
				
			||||||
 | 
					                for (int j = matrix.length-1; j > i; j--) {
 | 
				
			||||||
 | 
					                    matrix = Array.MatrixRemoveRow(matrix, j);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (int i = 0; i < matrix[0].length; i++) {
 | 
				
			||||||
 | 
					            if(!Array.isColumnOnlyFalse(matrix, i)) {
 | 
				
			||||||
 | 
					                for (int j = 0; j < i; j++) {
 | 
				
			||||||
 | 
					                    matrix = Array.MatrixRemoveColumn(matrix, 0);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (int i = matrix[0].length-1; i >= 0; i--){
 | 
				
			||||||
 | 
					            if(!Array.isColumnOnlyFalse(matrix, i)) {
 | 
				
			||||||
 | 
					                for (int j = matrix[0].length-1; j > i; j--) {
 | 
				
			||||||
 | 
					                    matrix = Array.MatrixRemoveColumn(matrix, j);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        height = matrix.length;
 | 
				
			||||||
 | 
					        width = matrix[0].length;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        this.matrix = matrix;
 | 
					        this.matrix = matrix;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -17,6 +17,8 @@ public class Array{
 | 
				
			|||||||
        for (int i = 0; i < o.length; i++) {
 | 
					        for (int i = 0; i < o.length; i++) {
 | 
				
			||||||
            if(i == row)
 | 
					            if(i == row)
 | 
				
			||||||
                i++;
 | 
					                i++;
 | 
				
			||||||
 | 
					            if(i >= o.length)
 | 
				
			||||||
 | 
					                continue;
 | 
				
			||||||
            newMatrix[newRow] = o[i];
 | 
					            newMatrix[newRow] = o[i];
 | 
				
			||||||
            newRow++;
 | 
					            newRow++;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -30,6 +32,8 @@ public class Array{
 | 
				
			|||||||
            for(int j = 0; j < o[0].length; j++){
 | 
					            for(int j = 0; j < o[0].length; j++){
 | 
				
			||||||
                if(j == col)
 | 
					                if(j == col)
 | 
				
			||||||
                    j++;
 | 
					                    j++;
 | 
				
			||||||
 | 
					                if(j >= o[0].length)
 | 
				
			||||||
 | 
					                    continue;
 | 
				
			||||||
                newMatrix[i][newCol] = o[i][j];
 | 
					                newMatrix[i][newCol] = o[i][j];
 | 
				
			||||||
                newCol++;
 | 
					                newCol++;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
				
			|||||||
@ -17,7 +17,6 @@ class PieceTest {
 | 
				
			|||||||
        boolean[][] piece1_matrix_result = {
 | 
					        boolean[][] piece1_matrix_result = {
 | 
				
			||||||
                {true, false, true},
 | 
					                {true, false, true},
 | 
				
			||||||
                {true, true, false},
 | 
					                {true, true, false},
 | 
				
			||||||
                {false, false, false},
 | 
					 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        boolean[][] piece2_matrix = {
 | 
					        boolean[][] piece2_matrix = {
 | 
				
			||||||
@ -31,7 +30,6 @@ class PieceTest {
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        boolean[][] piece3_matrix_result = {
 | 
					        boolean[][] piece3_matrix_result = {
 | 
				
			||||||
                {false, false, false},
 | 
					 | 
				
			||||||
                {false, true, true},
 | 
					                {false, true, true},
 | 
				
			||||||
                {true, false, true},
 | 
					                {true, false, true},
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,9 @@
 | 
				
			|||||||
package school_project;
 | 
					package school_project;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.junit.jupiter.api.Test;
 | 
					import org.junit.jupiter.api.Test;
 | 
				
			||||||
 | 
					import school_project.Utils.Array;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Arrays;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import static org.junit.jupiter.api.Assertions.*;
 | 
					import static org.junit.jupiter.api.Assertions.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -29,6 +32,30 @@ class ShapeTest {
 | 
				
			|||||||
                {true}
 | 
					                {true}
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        boolean[][] matrix_shape5 = {
 | 
				
			||||||
 | 
					                {false, false, false, false, false},
 | 
				
			||||||
 | 
					                {false, false, false, false, false},
 | 
				
			||||||
 | 
					                {false, true, true, true, false},
 | 
				
			||||||
 | 
					                {false, true, false, true, false},
 | 
				
			||||||
 | 
					                {false, false, false, false, false},
 | 
				
			||||||
 | 
					                {false, false, false, false, false},
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        boolean[][] matrix_shape5_result = {
 | 
				
			||||||
 | 
					                {true, true, true},
 | 
				
			||||||
 | 
					                {true, false, true},
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        boolean[][] matrix_shape6 = {
 | 
				
			||||||
 | 
					                {true, false},
 | 
				
			||||||
 | 
					                {false, false}
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        boolean[][] matrix_shape6_result = {
 | 
				
			||||||
 | 
					                {true},
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        System.out.println(Array.isRowOnlyFalse(matrix_shape1, 0));
 | 
				
			||||||
        Shape shape1 = new Shape();
 | 
					        Shape shape1 = new Shape();
 | 
				
			||||||
        shape1.setShape(matrix_shape1);
 | 
					        shape1.setShape(matrix_shape1);
 | 
				
			||||||
        assertEquals(3, shape1.getHeight());
 | 
					        assertEquals(3, shape1.getHeight());
 | 
				
			||||||
@ -44,5 +71,11 @@ class ShapeTest {
 | 
				
			|||||||
        assertEquals(3, shape4.getHeight());
 | 
					        assertEquals(3, shape4.getHeight());
 | 
				
			||||||
        assertEquals(1, shape4.getWidth());
 | 
					        assertEquals(1, shape4.getWidth());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Shape shape5 = new Shape(matrix_shape5);
 | 
				
			||||||
 | 
					        assertArrayEquals(matrix_shape5_result, shape5.getShape());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Shape shape6 = new Shape(matrix_shape6);
 | 
				
			||||||
 | 
					        assertArrayEquals(matrix_shape6_result, shape6.getShape());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user