2023-02-27 00:52:19 +01:00
|
|
|
package school_project;
|
|
|
|
|
|
|
|
|
2023-04-21 20:00:15 +02:00
|
|
|
import java.io.Serializable;
|
|
|
|
|
2023-03-21 14:14:39 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2023-04-21 20:00:15 +02:00
|
|
|
public class Shape implements Serializable, Cloneable{
|
|
|
|
|
2023-02-27 00:52:19 +01:00
|
|
|
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;
|
|
|
|
}
|
2023-04-21 20:00:15 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
String ret = "";
|
|
|
|
for (boolean[] row : matrix) {
|
|
|
|
for (boolean el : row) {
|
|
|
|
if(el) ret = ret.concat("⬛");
|
|
|
|
else ret = ret.concat("⬜");
|
|
|
|
}
|
|
|
|
ret = ret.concat("\n");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|