From e7c7065a8d74f61aaf1d22bc4024d9be994ce3eb Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Wed, 10 May 2023 20:02:10 +0200 Subject: [PATCH 1/3] Adding MatrixRemoveRow/Column --- .../main/java/school_project/Utils/Array.java | 26 +++++++++++++++ .../java/school_project/Utils/ArrayTest.java | 33 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/app/src/main/java/school_project/Utils/Array.java b/app/src/main/java/school_project/Utils/Array.java index 9f722d6..8d2176a 100644 --- a/app/src/main/java/school_project/Utils/Array.java +++ b/app/src/main/java/school_project/Utils/Array.java @@ -10,4 +10,30 @@ public class Array{ } return ret; } + + public static boolean[][] MatrixRemoveRow(boolean[][] o, int row){ + boolean[][] newMatrix = new boolean[o.length - 1][o[0].length]; + int newRow = 0; + for (int i = 0; i < o.length; i++) { + if(i == row) + i++; + newMatrix[newRow] = o[i]; + newRow++; + } + return newMatrix; + } + + public static boolean[][] MatrixRemoveColumn(boolean[][] o, int col){ + boolean[][] newMatrix = new boolean[o.length][o[0].length - 1]; + for (int i = 0; i < o.length; i++) { + int newCol = 0; + for(int j = 0; j < o[0].length; j++){ + if(j == col) + j++; + newMatrix[i][newCol] = o[i][j]; + newCol++; + } + } + return newMatrix; + } } diff --git a/app/src/test/java/school_project/Utils/ArrayTest.java b/app/src/test/java/school_project/Utils/ArrayTest.java index 70b66b9..14e5285 100644 --- a/app/src/test/java/school_project/Utils/ArrayTest.java +++ b/app/src/test/java/school_project/Utils/ArrayTest.java @@ -23,4 +23,37 @@ class ArrayTest { a[1][1] = true; assertArrayEquals(b, c); } + + @Test + void matrixRemoveRow() { + boolean[][] a = new boolean[][] { + {true, false, true}, + {false, false, false}, + {true, false, true}, + }; + boolean[][] b = new boolean[][] { + {true, false, true}, + {true, false, true}, + }; + + boolean[][] result = Array.MatrixRemoveRow(a, 1); + assertArrayEquals(b, result); + } + + @Test + void matrixRemoveColumn() { + boolean[][] a = new boolean[][] { + {true, false, true}, + {false, false, false}, + {true, false, true}, + }; + boolean[][] b = new boolean[][] { + {true, true}, + {false, false}, + {true, true}, + }; + + boolean[][] result = Array.MatrixRemoveColumn(a, 1); + assertArrayEquals(b, result); + } } \ No newline at end of file From 8ec5a622d8c095dd9932dda93e6ec0ca5c6d5e3f Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Wed, 10 May 2023 20:22:20 +0200 Subject: [PATCH 2/3] isColumn/RowOnlyFalse --- .../main/java/school_project/Utils/Array.java | 18 +++++++++++++++ .../java/school_project/Utils/ArrayTest.java | 22 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/app/src/main/java/school_project/Utils/Array.java b/app/src/main/java/school_project/Utils/Array.java index 8d2176a..872cb45 100644 --- a/app/src/main/java/school_project/Utils/Array.java +++ b/app/src/main/java/school_project/Utils/Array.java @@ -36,4 +36,22 @@ public class Array{ } return newMatrix; } + + public static boolean isRowOnlyFalse(boolean[][] o, int row){ + boolean mark = true; + for (int i = 0; i < o[row].length; i++) { + if(o[row][i]) + mark = false; + } + return mark; + } + + public static boolean isColumnOnlyFalse(boolean[][] o, int column){ + boolean mark = true; + for (int i = 0; i < o.length; i++) { + if(o[i][column]) + mark = false; + } + return mark; + } } diff --git a/app/src/test/java/school_project/Utils/ArrayTest.java b/app/src/test/java/school_project/Utils/ArrayTest.java index 14e5285..70a75ea 100644 --- a/app/src/test/java/school_project/Utils/ArrayTest.java +++ b/app/src/test/java/school_project/Utils/ArrayTest.java @@ -56,4 +56,26 @@ class ArrayTest { boolean[][] result = Array.MatrixRemoveColumn(a, 1); assertArrayEquals(b, result); } + + @Test + void isRowOnlyFalse() { + boolean[][] a = new boolean[][] { + {true, false, true}, + {false, false, false}, + {true, false, true}, + }; + assertTrue(Array.isRowOnlyFalse(a, 1)); + assertFalse(Array.isRowOnlyFalse(a, 0)); + } + + @Test + void isColumnOnlyFalse() { + boolean[][] a = new boolean[][] { + {true, false, true}, + {false, false, false}, + {true, false, true}, + }; + assertTrue(Array.isColumnOnlyFalse(a, 1)); + assertFalse(Array.isColumnOnlyFalse(a, 0)); + } } \ No newline at end of file From 90d6d47cc811c9d36736aaa8a1a9021767e05490 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Wed, 10 May 2023 22:49:29 +0200 Subject: [PATCH 3/3] limit piece matrix to their minimum size --- app/src/main/java/school_project/Piece.java | 7 ++- app/src/main/java/school_project/Shape.java | 46 +++++++++++++++++-- .../main/java/school_project/Utils/Array.java | 4 ++ .../test/java/school_project/PieceTest.java | 2 - .../test/java/school_project/ShapeTest.java | 33 +++++++++++++ 5 files changed, 85 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/school_project/Piece.java b/app/src/main/java/school_project/Piece.java index 7ce450e..29ee035 100644 --- a/app/src/main/java/school_project/Piece.java +++ b/app/src/main/java/school_project/Piece.java @@ -55,6 +55,8 @@ public class Piece extends Shape{ */ public void RotateRight(int times){ while(times > 0) { + height = matrix.length; + width = matrix[0].length; boolean[][] temp_matrix = new boolean[width][height]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { @@ -70,9 +72,10 @@ public class Piece extends Shape{ public boolean equals(Object obj) { if(obj instanceof Piece){ Piece pieceObj = (Piece) obj; - if( pieceObj.getPosition().equals(this.getPosition()) && pieceObj.getShape().equals(getShape())) { - return true; + if (pieceObj.getPosition() != null && this.getPosition() != null){ + return pieceObj.getPosition().equals(this.getPosition()) && pieceObj.getShape().equals(getShape()); } + return pieceObj.getShape().equals(getShape()); } return false; } diff --git a/app/src/main/java/school_project/Shape.java b/app/src/main/java/school_project/Shape.java index 3a436d6..d6fbd6d 100644 --- a/app/src/main/java/school_project/Shape.java +++ b/app/src/main/java/school_project/Shape.java @@ -1,6 +1,8 @@ package school_project; +import school_project.Utils.Array; + import java.io.Serializable; /** @@ -21,14 +23,52 @@ public class Shape implements Serializable, Cloneable{ } public void setShape(boolean[][] matrix) throws IllegalArgumentException{ - height = matrix.length; - width = matrix[0].length; for (boolean[] row: matrix){ - if(row.length != width){ + if(row.length != matrix[0].length){ 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; } diff --git a/app/src/main/java/school_project/Utils/Array.java b/app/src/main/java/school_project/Utils/Array.java index 872cb45..5de668f 100644 --- a/app/src/main/java/school_project/Utils/Array.java +++ b/app/src/main/java/school_project/Utils/Array.java @@ -17,6 +17,8 @@ public class Array{ for (int i = 0; i < o.length; i++) { if(i == row) i++; + if(i >= o.length) + continue; newMatrix[newRow] = o[i]; newRow++; } @@ -30,6 +32,8 @@ public class Array{ for(int j = 0; j < o[0].length; j++){ if(j == col) j++; + if(j >= o[0].length) + continue; newMatrix[i][newCol] = o[i][j]; newCol++; } diff --git a/app/src/test/java/school_project/PieceTest.java b/app/src/test/java/school_project/PieceTest.java index 6e80749..b78d371 100644 --- a/app/src/test/java/school_project/PieceTest.java +++ b/app/src/test/java/school_project/PieceTest.java @@ -17,7 +17,6 @@ class PieceTest { boolean[][] piece1_matrix_result = { {true, false, true}, {true, true, false}, - {false, false, false}, }; boolean[][] piece2_matrix = { @@ -31,7 +30,6 @@ class PieceTest { }; boolean[][] piece3_matrix_result = { - {false, false, false}, {false, true, true}, {true, false, true}, }; diff --git a/app/src/test/java/school_project/ShapeTest.java b/app/src/test/java/school_project/ShapeTest.java index c5e6fd3..88377b3 100644 --- a/app/src/test/java/school_project/ShapeTest.java +++ b/app/src/test/java/school_project/ShapeTest.java @@ -1,6 +1,9 @@ package school_project; import org.junit.jupiter.api.Test; +import school_project.Utils.Array; + +import java.util.Arrays; import static org.junit.jupiter.api.Assertions.*; @@ -29,6 +32,30 @@ class ShapeTest { {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(); shape1.setShape(matrix_shape1); assertEquals(3, shape1.getHeight()); @@ -44,5 +71,11 @@ class ShapeTest { assertEquals(3, shape4.getHeight()); 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()); + } } \ No newline at end of file