Merge branch 'master' of https://git.herisson.ovh/undefined_name/School_Project
This commit is contained in:
46
app/src/main/java/school_project/Map.java
Normal file
46
app/src/main/java/school_project/Map.java
Normal file
@ -0,0 +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 final ArrayList<Piece> 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;
|
||||
}
|
||||
}
|
59
app/src/main/java/school_project/Piece.java
Normal file
59
app/src/main/java/school_project/Piece.java
Normal file
@ -0,0 +1,59 @@
|
||||
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 Vec2 Position;
|
||||
private Map linked_map;
|
||||
|
||||
public Piece() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Piece(boolean[][] matrix) {
|
||||
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
|
||||
*
|
||||
* @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 < width; i++) {
|
||||
for (int j = 0; j < height; j++) {
|
||||
temp_matrix[i][j] = matrix[-j+height-1][i];
|
||||
}
|
||||
}
|
||||
times--;
|
||||
matrix = temp_matrix;
|
||||
}
|
||||
}
|
||||
}
|
44
app/src/main/java/school_project/Shape.java
Normal file
44
app/src/main/java/school_project/Shape.java
Normal file
@ -0,0 +1,44 @@
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
19
app/src/main/java/school_project/Vec2.java
Normal file
19
app/src/main/java/school_project/Vec2.java
Normal file
@ -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 = 0;
|
||||
y = 0;
|
||||
}
|
||||
|
||||
public Vec2(int x, int y ){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user