56 lines
1.4 KiB
Java
Raw Normal View History

package school_project;
import java.util.ArrayList;
2023-03-21 14:14:39 +01:00
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{
2023-03-21 14:14:39 +01:00
private final ArrayList<Piece> pieces = new ArrayList<>();
public Map(boolean[][] matrix) {
super(matrix);
}
public Map() {
super();
}
public void addPiece(Piece piece){
2023-03-21 14:14:39 +01:00
piece.setLinked_map(this);
pieces.add(piece);
}
public void addPiece(Piece[] pieces) {
for (Piece p : pieces)
this.addPiece(p);
}
2023-03-21 14:14:39 +01:00
/**
* 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;
}
}