diff --git a/app/src/main/java/school_project/Map.java b/app/src/main/java/school_project/Map.java index 28008b0..e67944a 100644 --- a/app/src/main/java/school_project/Map.java +++ b/app/src/main/java/school_project/Map.java @@ -1,9 +1,14 @@ 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); @@ -13,8 +18,8 @@ public class Map extends Shape{ super(); } - // TODO: 2/27/23 Tests for Map public void addPiece(Piece piece){ + piece.setLinked_map(this); pieces.add(piece); } @@ -22,4 +27,29 @@ public class Map extends Shape{ for (Piece p : pieces) this.addPiece(p); } + + /** + * 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 50d7c4e..a358dd2 100644 --- a/app/src/main/java/school_project/Piece.java +++ b/app/src/main/java/school_project/Piece.java @@ -1,8 +1,16 @@ 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; + private Map linked_map; + public Piece() { super(); } @@ -11,6 +19,26 @@ public class Piece extends Shape{ super(matrix); } + public Vec2 getPosition() { + return Position; + } + + 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 64f7bca..7061bc2 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/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; + } +} 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