Merge pull request 'Shape_Piece_Map' (#15) from Shape_Piece_Map into master
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #15 Reviewed-by: BrokenBrad <eddyjiofak@gmail.com> Reviewed-by: Mat_02 <diletomatteo@gmail.com>
This commit is contained in:
commit
eeae13b339
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;
|
||||
}
|
||||
}
|
35
app/src/test/java/school_project/MapTest.java
Normal file
35
app/src/test/java/school_project/MapTest.java
Normal file
@ -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());
|
||||
}
|
||||
}
|
60
app/src/test/java/school_project/PieceTest.java
Normal file
60
app/src/test/java/school_project/PieceTest.java
Normal file
@ -0,0 +1,60 @@
|
||||
package school_project;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PieceTest {
|
||||
|
||||
@Test
|
||||
void rotateRight() {
|
||||
boolean[][] piece1_matrix = {
|
||||
{true, false, false},
|
||||
{false, true, false},
|
||||
{true, true, false},
|
||||
};
|
||||
|
||||
boolean[][] piece1_matrix_result = {
|
||||
{true, false, true},
|
||||
{true, true, false},
|
||||
{false, false, false},
|
||||
};
|
||||
|
||||
boolean[][] piece2_matrix = {
|
||||
{true},
|
||||
{false},
|
||||
{true},
|
||||
};
|
||||
|
||||
boolean[][] piece2_matrix_result = {
|
||||
{true, false, true},
|
||||
};
|
||||
|
||||
boolean[][] piece3_matrix_result = {
|
||||
{false, false, false},
|
||||
{false, true, true},
|
||||
{true, false, true},
|
||||
};
|
||||
|
||||
Piece piece1 = new Piece();
|
||||
piece1.setShape(piece1_matrix);
|
||||
|
||||
Piece piece2 = new Piece(piece2_matrix);
|
||||
|
||||
Piece piece3 = new Piece(piece1_matrix);
|
||||
|
||||
Piece piece4 = new Piece(piece1_matrix);
|
||||
|
||||
piece1.RotateRight(1);
|
||||
assertArrayEquals(piece1_matrix_result, piece1.getShape());
|
||||
|
||||
piece2.RotateRight(1);
|
||||
assertArrayEquals(piece2_matrix_result, piece2.getShape());
|
||||
|
||||
piece3.RotateRight(3);
|
||||
assertArrayEquals(piece3_matrix_result, piece3.getShape());
|
||||
|
||||
piece4.RotateRight(3);
|
||||
assertNotEquals(piece1_matrix_result, piece4.getShape());
|
||||
}
|
||||
}
|
48
app/src/test/java/school_project/ShapeTest.java
Normal file
48
app/src/test/java/school_project/ShapeTest.java
Normal file
@ -0,0 +1,48 @@
|
||||
package school_project;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ShapeTest {
|
||||
|
||||
@Test
|
||||
void setShape() {
|
||||
boolean[][] matrix_shape1 = {
|
||||
{true, false},
|
||||
{true, true},
|
||||
{true, false}
|
||||
};
|
||||
boolean[][] matrix_shape2 = {
|
||||
{true, false, true},
|
||||
{true, true, true},
|
||||
{true, false, true},
|
||||
};
|
||||
boolean[][] matrix_shape3 = {
|
||||
{true, false, true},
|
||||
{true, true}
|
||||
};
|
||||
|
||||
boolean[][] matrix_shape4 = {
|
||||
{true},
|
||||
{false},
|
||||
{true}
|
||||
};
|
||||
|
||||
Shape shape1 = new Shape();
|
||||
shape1.setShape(matrix_shape1);
|
||||
assertEquals(3, shape1.getHeight());
|
||||
assertEquals(2, shape1.getWidth());
|
||||
|
||||
Shape shape2 = new Shape(matrix_shape2);
|
||||
assertEquals(3, shape2.getHeight());
|
||||
assertEquals(3, shape2.getWidth());
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> new Shape(matrix_shape3));
|
||||
|
||||
Shape shape4 = new Shape(matrix_shape4);
|
||||
assertEquals(3, shape4.getHeight());
|
||||
assertEquals(1, shape4.getWidth());
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user