diff --git a/app/src/main/java/school_project/GameUI.java b/app/src/main/java/school_project/GameUI.java index 487469a..5edfabf 100644 --- a/app/src/main/java/school_project/GameUI.java +++ b/app/src/main/java/school_project/GameUI.java @@ -2,6 +2,7 @@ package school_project; import javafx.scene.Group; import javafx.scene.input.MouseButton; +import school_project.Menu.MenuAcceuil; import school_project.Utils.MatrixShape; import java.io.FileNotFoundException; @@ -70,6 +71,9 @@ public class GameUI extends Group{ _piece.setLayoutX(grid.getLayoutX() + p.getPosition().y * (SEGMENT_SIZE+SPACE_SIZE)); _piece.setLayoutY(grid.getLayoutY() + p.getPosition().x * (SEGMENT_SIZE+SPACE_SIZE)); } + if(level.gameDone()){ + Controller.switchRoot(new MenuAcceuil()); + } } }); diff --git a/app/src/main/java/school_project/Map.java b/app/src/main/java/school_project/Map.java index 26dacd9..be7037e 100644 --- a/app/src/main/java/school_project/Map.java +++ b/app/src/main/java/school_project/Map.java @@ -68,6 +68,18 @@ public class Map extends Shape{ return true; } + /** + * Check if every pieces has a space on the board to know if the game is finished + * @return true if the game is finished, false if not + */ + public boolean gameDone(){ + ArrayList posList = getPosList(); + for(Piece p: pieces){ + posList.removeAll(p.getOccupation()); + } + return posList.isEmpty(); + } + /** * Return a matrix with all used space on the map to see if a piece can fit in a space * diff --git a/app/src/main/java/school_project/Piece.java b/app/src/main/java/school_project/Piece.java index 21143d1..e9bdc7c 100644 --- a/app/src/main/java/school_project/Piece.java +++ b/app/src/main/java/school_project/Piece.java @@ -42,6 +42,8 @@ public class Piece extends Shape{ public ArrayList getOccupation(){ ArrayList ret = new ArrayList<>(); + if(Position == null) + return ret; for (int x = 0; x < height; x++) { for (int y = 0; y < width; y++) { if(getShape()[x][y]){ diff --git a/app/src/main/java/school_project/Shape.java b/app/src/main/java/school_project/Shape.java index d6fbd6d..e599677 100644 --- a/app/src/main/java/school_project/Shape.java +++ b/app/src/main/java/school_project/Shape.java @@ -4,6 +4,7 @@ package school_project; import school_project.Utils.Array; import java.io.Serializable; +import java.util.ArrayList; /** * Base class for everything that is a shape kind, like map and pieces @@ -84,6 +85,22 @@ public class Shape implements Serializable, Cloneable{ return matrix; } + /** + * Get the list of all the open possition of the map + * @return ArrayList of vec2 of all the positions + */ + public ArrayList getPosList(){ + ArrayList ret = new ArrayList<>(); + for (int x = 0; x < height; x++) { + for (int y = 0; y < width; y++) { + if(matrix[x][y]){ + ret.add(new Vec2(x, y)); + } + } + } + return ret; + } + @Override public String toString() { String ret = "";