From e06abe60de96ccc534e8e3b3b0401cea56e3dcfe Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Mon, 27 Feb 2023 00:52:19 +0100 Subject: [PATCH 1/8] Starting Piece Shape and Maps, WIP: wrong algorithm --- app/src/main/java/school_project/Piece.java | 31 ++++++++++ app/src/main/java/school_project/Shape.java | 40 +++++++++++++ .../test/java/school_project/PieceTest.java | 60 +++++++++++++++++++ .../test/java/school_project/ShapeTest.java | 48 +++++++++++++++ 4 files changed, 179 insertions(+) create mode 100644 app/src/main/java/school_project/Piece.java create mode 100644 app/src/main/java/school_project/Shape.java create mode 100644 app/src/test/java/school_project/PieceTest.java create mode 100644 app/src/test/java/school_project/ShapeTest.java diff --git a/app/src/main/java/school_project/Piece.java b/app/src/main/java/school_project/Piece.java new file mode 100644 index 0000000..f5dde6b --- /dev/null +++ b/app/src/main/java/school_project/Piece.java @@ -0,0 +1,31 @@ +package school_project; + +public class Piece extends Shape{ + + public Piece() { + super(); + } + + public Piece(boolean[][] matrix) { + super(matrix); + } + + /** + * Rotate the matrix of the piece. Used when the player right click + * TODO: ALGORITHME INCORECTE, A REFAIRE + * + * @param times Set the amount of time the rotation should be executed. Should be set between 1 and 3. + */ + public void RotateRight(int times){ + while(times > 0) { + boolean[][] temp_matrix = new boolean[width][height]; + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + temp_matrix[j][i] = matrix[i][j]; + } + } + times--; + matrix = temp_matrix; + } + } +} \ No newline at end of file diff --git a/app/src/main/java/school_project/Shape.java b/app/src/main/java/school_project/Shape.java new file mode 100644 index 0000000..7f52580 --- /dev/null +++ b/app/src/main/java/school_project/Shape.java @@ -0,0 +1,40 @@ +package school_project; + + +public class Shape { + + protected boolean[][] matrix; + protected int height, width; + + public Shape() { + matrix = new boolean[0][0]; + } + + public Shape(boolean[][] matrix){ + this.setShape(matrix); + } + + public void setShape(boolean[][] matrix) throws IllegalArgumentException{ + height = matrix.length; + width = matrix[0].length; + + for (boolean[] row: matrix){ + if(row.length != width){ + throw new IllegalArgumentException("The argument should be a square matrix"); + } + } + this.matrix = matrix; + } + + public int getHeight() { + return height; + } + + public int getWidth() { + return width; + } + + public boolean[][] getShape() { + return matrix; + } +} \ No newline at end of file diff --git a/app/src/test/java/school_project/PieceTest.java b/app/src/test/java/school_project/PieceTest.java new file mode 100644 index 0000000..ad9e23c --- /dev/null +++ b/app/src/test/java/school_project/PieceTest.java @@ -0,0 +1,60 @@ +package school_project; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class PieceTest { + + @Test + void rotateRight() { + boolean[][] piece1_matrix = { + {true, false, false}, + {false, true, false}, + {true, true, false}, + }; + + boolean[][] piece1_matrix_result = { + {true, false, true}, + {false, true, true}, + {false, false, false}, + }; + + boolean[][] piece2_matrix = { + {true}, + {false}, + {true}, + }; + + boolean[][] piece2_matrix_result = { + {true, false, true}, + }; + + boolean[][] piece3_matrix_result = { + {false, false, false}, + {true, true, false}, + {true, false, true}, + }; + + Piece piece1 = new Piece(); + piece1.setShape(piece1_matrix); + + Piece piece2 = new Piece(piece2_matrix); + + Piece piece3 = new Piece(piece1_matrix); + + Piece piece4 = new Piece(piece1_matrix); + + piece1.RotateRight(1); + assertArrayEquals(piece1_matrix_result, piece1.getShape()); + + piece2.RotateRight(1); + assertArrayEquals(piece2_matrix_result, piece2.getShape()); + + piece3.RotateRight(3); + assertArrayEquals(piece3_matrix_result, piece3.getShape()); + + piece4.RotateRight(3); + assertNotEquals(piece1_matrix_result, piece4.getShape()); + } +} \ No newline at end of file diff --git a/app/src/test/java/school_project/ShapeTest.java b/app/src/test/java/school_project/ShapeTest.java new file mode 100644 index 0000000..c5e6fd3 --- /dev/null +++ b/app/src/test/java/school_project/ShapeTest.java @@ -0,0 +1,48 @@ +package school_project; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ShapeTest { + + @Test + void setShape() { + boolean[][] matrix_shape1 = { + {true, false}, + {true, true}, + {true, false} + }; + boolean[][] matrix_shape2 = { + {true, false, true}, + {true, true, true}, + {true, false, true}, + }; + boolean[][] matrix_shape3 = { + {true, false, true}, + {true, true} + }; + + boolean[][] matrix_shape4 = { + {true}, + {false}, + {true} + }; + + Shape shape1 = new Shape(); + shape1.setShape(matrix_shape1); + assertEquals(3, shape1.getHeight()); + assertEquals(2, shape1.getWidth()); + + Shape shape2 = new Shape(matrix_shape2); + assertEquals(3, shape2.getHeight()); + assertEquals(3, shape2.getWidth()); + + assertThrows(IllegalArgumentException.class, () -> new Shape(matrix_shape3)); + + Shape shape4 = new Shape(matrix_shape4); + assertEquals(3, shape4.getHeight()); + assertEquals(1, shape4.getWidth()); + + } +} \ No newline at end of file From 498529f29a48471916e8581d9ef098815bc5eece Mon Sep 17 00:00:00 2001 From: "Debucquoy Anthony (tonitch)" Date: Mon, 27 Feb 2023 11:05:32 +0100 Subject: [PATCH 2/8] Finishing RotateRight Signed-off-by: Anthony Debucquoy --- app/src/main/java/school_project/Piece.java | 7 +++---- app/src/test/java/school_project/PieceTest.java | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/school_project/Piece.java b/app/src/main/java/school_project/Piece.java index f5dde6b..3a92feb 100644 --- a/app/src/main/java/school_project/Piece.java +++ b/app/src/main/java/school_project/Piece.java @@ -12,16 +12,15 @@ public class Piece extends Shape{ /** * Rotate the matrix of the piece. Used when the player right click - * TODO: ALGORITHME INCORECTE, A REFAIRE * * @param times Set the amount of time the rotation should be executed. Should be set between 1 and 3. */ public void RotateRight(int times){ while(times > 0) { boolean[][] temp_matrix = new boolean[width][height]; - for (int i = 0; i < height; i++) { - for (int j = 0; j < width; j++) { - temp_matrix[j][i] = matrix[i][j]; + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + temp_matrix[i][j] = matrix[-j+height-1][i]; } } times--; diff --git a/app/src/test/java/school_project/PieceTest.java b/app/src/test/java/school_project/PieceTest.java index ad9e23c..e3adc2e 100644 --- a/app/src/test/java/school_project/PieceTest.java +++ b/app/src/test/java/school_project/PieceTest.java @@ -16,7 +16,7 @@ class PieceTest { boolean[][] piece1_matrix_result = { {true, false, true}, - {false, true, true}, + {true, true, false}, {false, false, false}, }; @@ -32,7 +32,7 @@ class PieceTest { boolean[][] piece3_matrix_result = { {false, false, false}, - {true, true, false}, + {false, true, true}, {true, false, true}, }; From e983c5aa13da3d1f20f27ab84bcb32d208aa797c Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Mon, 27 Feb 2023 11:22:07 +0100 Subject: [PATCH 3/8] Adding Map class Signed-off-by: Anthony Debucquoy --- app/src/main/java/school_project/Map.java | 20 +++++++++++++++++++ app/src/main/java/school_project/Piece.java | 1 + .../main/java/school_project/Position.java | 2 ++ 3 files changed, 23 insertions(+) create mode 100644 app/src/main/java/school_project/Map.java create mode 100644 app/src/main/java/school_project/Position.java diff --git a/app/src/main/java/school_project/Map.java b/app/src/main/java/school_project/Map.java new file mode 100644 index 0000000..e9834c0 --- /dev/null +++ b/app/src/main/java/school_project/Map.java @@ -0,0 +1,20 @@ +package school_project; + +import java.util.ArrayList; + +public class Map extends Shape{ + private ArrayList pieces; + + public Map(boolean[][] matrix) { + super(matrix); + } + + public Map() { + super(); + } + + // TODO: 2/27/23 Tests for Map + public void AddShape(Piece piece){ + pieces.add(piece); + } +} diff --git a/app/src/main/java/school_project/Piece.java b/app/src/main/java/school_project/Piece.java index 3a92feb..50d7c4e 100644 --- a/app/src/main/java/school_project/Piece.java +++ b/app/src/main/java/school_project/Piece.java @@ -2,6 +2,7 @@ package school_project; public class Piece extends Shape{ + private int x,y; // Position in the Map Object public Piece() { super(); } diff --git a/app/src/main/java/school_project/Position.java b/app/src/main/java/school_project/Position.java new file mode 100644 index 0000000..a981709 --- /dev/null +++ b/app/src/main/java/school_project/Position.java @@ -0,0 +1,2 @@ +package school_project; + From 8d88f01c9d420fc9b18e08a2a2bc31773fefccc8 Mon Sep 17 00:00:00 2001 From: "Debucquoy Anthony (tonitch)" Date: Wed, 1 Mar 2023 23:44:26 +0100 Subject: [PATCH 4/8] Removing Position because my brain bugged Signed-off-by: Anthony Debucquoy --- app/src/main/java/school_project/Position.java | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 app/src/main/java/school_project/Position.java diff --git a/app/src/main/java/school_project/Position.java b/app/src/main/java/school_project/Position.java deleted file mode 100644 index a981709..0000000 --- a/app/src/main/java/school_project/Position.java +++ /dev/null @@ -1,2 +0,0 @@ -package school_project; - From 71beb69c840c077098fb0617882afc7274e068c5 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Mon, 20 Mar 2023 23:57:10 +0100 Subject: [PATCH 5/8] Changing addShape to AddPiece Signed-off-by: Anthony Debucquoy --- app/src/main/java/school_project/Map.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/school_project/Map.java b/app/src/main/java/school_project/Map.java index e9834c0..6053df7 100644 --- a/app/src/main/java/school_project/Map.java +++ b/app/src/main/java/school_project/Map.java @@ -14,7 +14,7 @@ public class Map extends Shape{ } // TODO: 2/27/23 Tests for Map - public void AddShape(Piece piece){ + public void addPiece(Piece piece){ pieces.add(piece); } } From 4854fef6775a91559eabf2d64738de79307f3d34 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Tue, 21 Mar 2023 09:56:13 +0100 Subject: [PATCH 6/8] wip Signed-off-by: Anthony Debucquoy --- app/src/main/java/school_project/Map.java | 9 ++++----- app/src/main/java/school_project/Piece.java | 15 ++++++++++++++- app/src/main/java/school_project/Vec2.java | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/school_project/Vec2.java diff --git a/app/src/main/java/school_project/Map.java b/app/src/main/java/school_project/Map.java index 6053df7..eb288d8 100644 --- a/app/src/main/java/school_project/Map.java +++ b/app/src/main/java/school_project/Map.java @@ -2,6 +2,10 @@ package school_project; import java.util.ArrayList; +/** + * Represent the map with its pieces. + * Every piece has a position element that represent its position on the map + */ public class Map extends Shape{ private ArrayList pieces; @@ -9,11 +13,6 @@ public class Map extends Shape{ super(matrix); } - public Map() { - super(); - } - - // TODO: 2/27/23 Tests for Map public void addPiece(Piece piece){ pieces.add(piece); } diff --git a/app/src/main/java/school_project/Piece.java b/app/src/main/java/school_project/Piece.java index 50d7c4e..b1f1d85 100644 --- a/app/src/main/java/school_project/Piece.java +++ b/app/src/main/java/school_project/Piece.java @@ -1,8 +1,15 @@ package school_project; +/** + * Represent a Piece in the game. + * Every Piece should be contained in a Map Object. + * A piece has a position witch is the position of its top-leftest position in its matrix. + * If the piece is not placed in the Map (in a floating state) the position should be null; + */ public class Piece extends Shape{ - private int x,y; // Position in the Map Object + private Vec2 Position; + public Piece() { super(); } @@ -11,6 +18,12 @@ public class Piece extends Shape{ super(matrix); } + public Vec2 getPosition() { + return Position; + } + + public void getPo + /** * Rotate the matrix of the piece. Used when the player right click * diff --git a/app/src/main/java/school_project/Vec2.java b/app/src/main/java/school_project/Vec2.java new file mode 100644 index 0000000..79d4fd9 --- /dev/null +++ b/app/src/main/java/school_project/Vec2.java @@ -0,0 +1,19 @@ +package school_project; + +/** + * This is used to represent a position/vector/... any ensemble of 2 elements that have to work together in + * a plan. This way we can use some basic operations over them. + */ +public class Vec2 { + public int x, y; + + public Vec2() { + x = -1; + y = -1; + } + + public Vec2(int x, int y ){ + this.x = x; + this.y = y; + } +} From 24df4e376c04d085046aeabd2794b5ccd6c2aebd Mon Sep 17 00:00:00 2001 From: Debucquoy Date: Tue, 21 Mar 2023 14:14:39 +0100 Subject: [PATCH 7/8] Adding Pieces positions --- app/src/main/java/school_project/Map.java | 29 ++++++++++++++- app/src/main/java/school_project/Piece.java | 17 ++++++++- app/src/main/java/school_project/Shape.java | 4 +++ app/src/test/java/school_project/MapTest.java | 35 +++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 app/src/test/java/school_project/MapTest.java diff --git a/app/src/main/java/school_project/Map.java b/app/src/main/java/school_project/Map.java index eb288d8..69a99b3 100644 --- a/app/src/main/java/school_project/Map.java +++ b/app/src/main/java/school_project/Map.java @@ -1,19 +1,46 @@ package school_project; import java.util.ArrayList; +import java.util.Arrays; /** * Represent the map with its pieces. * Every piece has a position element that represent its position on the map */ public class Map extends Shape{ - private ArrayList pieces; + private final ArrayList pieces = new ArrayList<>(); public Map(boolean[][] matrix) { super(matrix); } public void addPiece(Piece piece){ + piece.setLinked_map(this); pieces.add(piece); } + + /** + * Return a matrix with all used space on the map to see if a piece can fit in a space + * + * @return matrix of boolean with false being the not used space + */ + public boolean[][] getUsedSpace(){ + + // Copy of the map to avoid side effect + boolean[][] used = new boolean[height][width]; + for (int i = 0; i < height; i++) { + used[i] = Arrays.copyOf(matrix[i], width); + } + + for (Piece p : pieces) { + for(int x = 0; x < p.height; x++){ + for(int y = 0; y < p.width; y++){ + if (p.getShape()[x][y]){ + used[p.getPosition().x + x][p.getPosition().y + y] = false; + } + } + } + } + return used; + } } diff --git a/app/src/main/java/school_project/Piece.java b/app/src/main/java/school_project/Piece.java index b1f1d85..a358dd2 100644 --- a/app/src/main/java/school_project/Piece.java +++ b/app/src/main/java/school_project/Piece.java @@ -9,6 +9,7 @@ package school_project; public class Piece extends Shape{ private Vec2 Position; + private Map linked_map; public Piece() { super(); @@ -22,7 +23,21 @@ public class Piece extends Shape{ return Position; } - public void getPo + public void setPosition(Vec2 position){ + if (linked_map == null) { + return; + } + + this.Position = position; + } + + /** + * set the map the piece is into the the map argument + * @param map map where to place the piece + */ + public void setLinked_map(Map map){ + this.linked_map = map; + } /** * Rotate the matrix of the piece. Used when the player right click diff --git a/app/src/main/java/school_project/Shape.java b/app/src/main/java/school_project/Shape.java index 7f52580..eda3a9c 100644 --- a/app/src/main/java/school_project/Shape.java +++ b/app/src/main/java/school_project/Shape.java @@ -1,6 +1,10 @@ package school_project; +/** + * Base class for everything that is a shape kind, like map and pieces + * it contain a matrix of boolean where the shape is defined by the true's + */ public class Shape { protected boolean[][] matrix; diff --git a/app/src/test/java/school_project/MapTest.java b/app/src/test/java/school_project/MapTest.java new file mode 100644 index 0000000..289997b --- /dev/null +++ b/app/src/test/java/school_project/MapTest.java @@ -0,0 +1,35 @@ +package school_project; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class MapTest { + + @Test + void getUsedSpace() { + boolean[][] map_matrix = { + {false, true, true, false}, + {false, true, true, true}, + {true, true, true, false}, + {false, true, true, true} + }; + Map testMap = new Map(map_matrix); + + boolean[][] piece1_matrix = { + {true, true}, + {true, true}, + }; + Piece piece1 = new Piece(piece1_matrix); + testMap.addPiece(piece1); + piece1.setPosition(new Vec2(0,1)); + + boolean[][] result_matrix = { + {false, false, false, false}, + {false, false, false, true}, + {true, true, true, false}, + {false, true, true, true} + }; + assertArrayEquals(result_matrix, testMap.getUsedSpace()); + } +} \ No newline at end of file From e7d82b3076856d10d977439621bbb2e2883d8bf6 Mon Sep 17 00:00:00 2001 From: Debucquoy Date: Thu, 23 Mar 2023 11:24:34 +0100 Subject: [PATCH 8/8] Setting default position of vector to 0. --- app/src/main/java/school_project/Vec2.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/school_project/Vec2.java b/app/src/main/java/school_project/Vec2.java index 79d4fd9..5de0978 100644 --- a/app/src/main/java/school_project/Vec2.java +++ b/app/src/main/java/school_project/Vec2.java @@ -8,8 +8,8 @@ public class Vec2 { public int x, y; public Vec2() { - x = -1; - y = -1; + x = 0; + y = 0; } public Vec2(int x, int y ){