Adding Pieces positions
This commit is contained in:
parent
4854fef677
commit
24df4e376c
@ -1,19 +1,46 @@
|
|||||||
package school_project;
|
package school_project;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represent the map with its pieces.
|
* Represent the map with its pieces.
|
||||||
* Every piece has a position element that represent its position on the map
|
* Every piece has a position element that represent its position on the map
|
||||||
*/
|
*/
|
||||||
public class Map extends Shape{
|
public class Map extends Shape{
|
||||||
private ArrayList<Piece> pieces;
|
private final ArrayList<Piece> pieces = new ArrayList<>();
|
||||||
|
|
||||||
public Map(boolean[][] matrix) {
|
public Map(boolean[][] matrix) {
|
||||||
super(matrix);
|
super(matrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addPiece(Piece piece){
|
public void addPiece(Piece piece){
|
||||||
|
piece.setLinked_map(this);
|
||||||
pieces.add(piece);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ package school_project;
|
|||||||
public class Piece extends Shape{
|
public class Piece extends Shape{
|
||||||
|
|
||||||
private Vec2 Position;
|
private Vec2 Position;
|
||||||
|
private Map linked_map;
|
||||||
|
|
||||||
public Piece() {
|
public Piece() {
|
||||||
super();
|
super();
|
||||||
@ -22,7 +23,21 @@ public class Piece extends Shape{
|
|||||||
return Position;
|
return Position;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getPo
|
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
|
* Rotate the matrix of the piece. Used when the player right click
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package school_project;
|
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 {
|
public class Shape {
|
||||||
|
|
||||||
protected boolean[][] matrix;
|
protected boolean[][] matrix;
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user