diff --git a/.drone.yml b/.drone.yml index 993edeb..b4d44da 100644 --- a/.drone.yml +++ b/.drone.yml @@ -5,14 +5,14 @@ name: Check_Requirement steps: - name: base_check - image: gradle:jdk19-alpine + image: gradle:jdk11-alpine commands: - ./gradlew clean - ./gradlew build - ./gradlew test - name: syntax_check - image: gradle:jdk19-alpine + image: gradle:jdk11-alpine commands: - ./gradlew check @@ -44,6 +44,6 @@ depends_on: - Check_Requirement --- kind: signature -hmac: f7588a8f835401820f6f596cad344ab01794dc0abcf9f81c989c625844ab4cc3 +hmac: 6b154c74ec624ce2d5867386bb7a6ee51cae9153457a8ce15f53e54546ccbc0e ... diff --git a/app/src/main/java/school_project/MapGenerator.java b/app/src/main/java/school_project/MapGenerator.java new file mode 100644 index 0000000..e782f97 --- /dev/null +++ b/app/src/main/java/school_project/MapGenerator.java @@ -0,0 +1,84 @@ +package school_project; + +import school_project.Utils.Array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Random; + +public class MapGenerator { + private static final Random rand = new Random(); + public enum Difficulty { + Easy, + Medium, + Difficult, + } + public static Map generate(Difficulty difficulty){ + Vec2 map_size; + int depth = 1; // how much the map shape generator could grind + + // define map size depending on the difficulty + switch (difficulty){ + case Easy: + map_size = new Vec2(rand.nextInt(2) + 3, rand.nextInt(2) + 3); + break; + case Medium: + map_size = new Vec2(rand.nextInt(3)+5, rand.nextInt(3)+5); + break; + case Difficult: + map_size = new Vec2(rand.nextInt(2)+8, rand.nextInt(2)+8); + depth = 2; + break; + default: + map_size = new Vec2(); + break; + } + + // Cut edges + boolean[][] map_shape = new boolean[map_size.x][map_size.y]; + for (boolean[] b : map_shape) { + Arrays.fill(b, true); + } + for (int i = 0; i < map_shape.length; i++) { + for (int j = 0; j < map_shape[0].length; j++) { + if(i > depth - 1 && i < map_shape.length - depth && j > depth - 1 && j < map_shape[0].length - depth){ + j = map_shape[0].length - depth; + } + map_shape[i][j] = rand.nextBoolean(); + } + } + Map ret = new Map(map_shape); + boolean[][] piece_layout = Array.MatrixCopyOf(map_shape); + ArrayList EmptySlots = new ArrayList<>(); + + for (int i = 0; i < piece_layout.length; i++) { + for (int j = 0; j < piece_layout[i].length; j++) { + if(piece_layout[i][j]){ + EmptySlots.add(new Vec2(i, j)); + } + } + } + + while (EmptySlots.size() > 0){ + Collections.shuffle(EmptySlots); + Vec2 selected = EmptySlots.get(0); + int size = rand.nextInt(3)+1; + boolean[][] shape = new boolean[size][size]; + for(int i = 0; i < size; i++){ + for (int j = 0; j < size; j++) { + Vec2 checked = new Vec2(i, j).add(selected); + if(EmptySlots.contains(checked)){ + EmptySlots.remove(checked); + piece_layout[checked.x][checked.y] = false; + shape[i][j] = true; + } + } + } + ret.addPiece(new Piece(shape)); + } + + //generate pieces + return ret; + } +} diff --git a/app/src/main/java/school_project/Parsers/FileParserFactory.java b/app/src/main/java/school_project/Parsers/FileParserFactory.java index a7f4f68..83749bf 100644 --- a/app/src/main/java/school_project/Parsers/FileParserFactory.java +++ b/app/src/main/java/school_project/Parsers/FileParserFactory.java @@ -2,11 +2,13 @@ package school_project.Parsers; import javafx.util.Pair; import school_project.Map; +import school_project.Piece; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.NotSerializableException; +import java.util.Scanner; /** * This is used to find the right parser to parser a save/level file. @@ -80,4 +82,51 @@ public class FileParserFactory { } return new Pair(fileParser, save_data); } + + public static void main(String[] args) throws IOException { + Scanner in = new Scanner(System.in); + Map level = new Map(); + + System.out.print("Entrez le nom du fichier:"); + File file = new File(in.nextLine()); + + System.out.print("Entrez la largeur de la map:"); + int map_width = in.nextInt(); + + System.out.print("Entrez la hauteur de la map:"); + int map_height = in.nextInt(); + + boolean[][] map_shape = new boolean[map_height][map_width]; + + for (int i = 0; i < map_height; i++) { + for (int j = 0; j < map_width; j++) { + System.out.print("mur (" + i + ", " + j + ")? (y/n):"); + map_shape[i][j] = in.next(".").charAt(0) != 'y'; + } + } + + level.setShape(map_shape); + System.out.println(level); + System.out.print("Entrez le nombre de pieces:"); + int piece_amount = in.nextInt(); + + for (int i = 0; i < piece_amount; i++) { + System.out.print("Entrez la largeur de la piece" + (i+1) +": "); + int _piece_width = in.nextInt(); + + System.out.print("Entrez la hauteur de la piece" + (i+1) +": "); + int _piece_height = in.nextInt(); + boolean[][] _piece_shape = new boolean[_piece_height][_piece_width]; + + for (int k = 0; k < _piece_height; k++) { + for (int j = 0; j < _piece_width; j++) { + System.out.print("mur (" + k + ", " + j + ")? (y/n):"); + _piece_shape[k][j] = in.next(".").charAt(0) != 'y'; + } + } + + level.addPiece(new Piece(_piece_shape)); + } + saveFileFromMap(file, level); + } } \ No newline at end of file diff --git a/app/src/main/java/school_project/Piece.java b/app/src/main/java/school_project/Piece.java index 51df463..7ce450e 100644 --- a/app/src/main/java/school_project/Piece.java +++ b/app/src/main/java/school_project/Piece.java @@ -1,5 +1,10 @@ package school_project; +import javafx.scene.paint.Color; +import javafx.scene.paint.Paint; + +import java.util.Random; + /** * Represent a Piece in the game. * Every Piece should be contained in a Map Object. @@ -10,13 +15,20 @@ public class Piece extends Shape{ private Vec2 Position; private Map linked_map; - - public Piece() { - super(); - } + private transient Paint color; // https://www.baeldung.com/java-transient-keyword public Piece(boolean[][] matrix) { super(matrix); + Random rand = new Random(); + color = new Color(rand.nextDouble(), rand.nextDouble(), rand.nextDouble(), 1); + } + + public void setColor(Paint p){ + color = p; + } + + public Paint getColor(){ + return color; } public Vec2 getPosition() { @@ -27,6 +39,7 @@ public class Piece extends Shape{ this.Position = position; } + /** * set the map the piece is into the the map argument * @param map map where to place the piece @@ -55,7 +68,8 @@ public class Piece extends Shape{ @Override public boolean equals(Object obj) { - if(obj instanceof Piece pieceObj){ + if(obj instanceof Piece){ + Piece pieceObj = (Piece) obj; if( pieceObj.getPosition().equals(this.getPosition()) && pieceObj.getShape().equals(getShape())) { return true; } diff --git a/app/src/main/java/school_project/Utils/Array.java b/app/src/main/java/school_project/Utils/Array.java new file mode 100644 index 0000000..9f722d6 --- /dev/null +++ b/app/src/main/java/school_project/Utils/Array.java @@ -0,0 +1,13 @@ +package school_project.Utils; + +import java.util.Arrays; + +public class Array{ + public static boolean[][] MatrixCopyOf(boolean[][] o){ + boolean[][] ret = new boolean[o.length][]; + for (int i = 0; i < o.length; i++){ + ret[i] = Arrays.copyOf(o[i], o[i].length); + } + return ret; + } +} diff --git a/app/src/main/java/school_project/Vec2.java b/app/src/main/java/school_project/Vec2.java index 7bf6ed9..05d2576 100644 --- a/app/src/main/java/school_project/Vec2.java +++ b/app/src/main/java/school_project/Vec2.java @@ -21,9 +21,19 @@ public class Vec2 implements Serializable { @Override public boolean equals(Object obj) { - if (obj instanceof Vec2 vec) { + if (obj instanceof Vec2) { + Vec2 vec = (Vec2) obj; return this.x == vec.x && this.y == vec.y; } return false; } + + public Vec2 add(Vec2 o){ + return new Vec2(x + o.x, y + o.y); + } + + @Override + public String toString() { + return "("+x+","+y+")"; + } } diff --git a/app/src/main/resources/school_project/level11.level b/app/src/main/resources/school_project/level11.level new file mode 100644 index 0000000..b66355c --- /dev/null +++ b/app/src/main/resources/school_project/level11.level @@ -0,0 +1 @@ +SMSçÿÿ€3ÿ€"ð"°"p"°€SME \ No newline at end of file diff --git a/app/src/test/java/school_project/MapGeneratorTest.java b/app/src/test/java/school_project/MapGeneratorTest.java new file mode 100644 index 0000000..d052592 --- /dev/null +++ b/app/src/test/java/school_project/MapGeneratorTest.java @@ -0,0 +1,26 @@ +package school_project; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class MapGeneratorTest { + + @Test + void generate() { + Map[] maps = new Map[] { + MapGenerator.generate(MapGenerator.Difficulty.Easy), + MapGenerator.generate(MapGenerator.Difficulty.Medium), + MapGenerator.generate(MapGenerator.Difficulty.Difficult), + }; + + for(Map m: maps){ + System.out.println("=========="); + System.out.println(m); + System.out.println("++++++++++++++++++++"); + for (Piece p: m.getPieces()){ + System.out.println(p); + } + } + } +} \ No newline at end of file diff --git a/app/src/test/java/school_project/PieceTest.java b/app/src/test/java/school_project/PieceTest.java index e3adc2e..6e80749 100644 --- a/app/src/test/java/school_project/PieceTest.java +++ b/app/src/test/java/school_project/PieceTest.java @@ -36,7 +36,7 @@ class PieceTest { {true, false, true}, }; - Piece piece1 = new Piece(); + Piece piece1 = new Piece(piece2_matrix); piece1.setShape(piece1_matrix); Piece piece2 = new Piece(piece2_matrix); diff --git a/app/src/test/java/school_project/Utils/ArrayTest.java b/app/src/test/java/school_project/Utils/ArrayTest.java new file mode 100644 index 0000000..70b66b9 --- /dev/null +++ b/app/src/test/java/school_project/Utils/ArrayTest.java @@ -0,0 +1,26 @@ +package school_project.Utils; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ArrayTest { + + @Test + void matrixCopyOf() { + boolean[][] a = new boolean[][] { + {true, false, true}, + {false, false, false}, + {true, false, true}, + }; + boolean[][] b = new boolean[][] { + {true, false, true}, + {false, false, false}, + {true, false, true}, + }; + boolean[][] c = Array.MatrixCopyOf(a); + assertArrayEquals(a, c); + a[1][1] = true; + assertArrayEquals(b, c); + } +} \ No newline at end of file