Merge branch 'Shape_Piece_Map' into MapParser
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
2023-03-21 23:28:01 +01:00
5 changed files with 119 additions and 3 deletions

View File

@@ -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<Piece> pieces;
private final ArrayList<Piece> 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;
}
}