From 2fa2ab1bb16ef13d3a72ce0da6f1323d37282775 Mon Sep 17 00:00:00 2001 From: Debucquoy Date: Wed, 26 Apr 2023 17:37:15 +0200 Subject: [PATCH 1/4] Generate map shape Todo: check if there is isolated cell and delete them too --- .../java/school_project/MapGenerator.java | 41 +++++++++++++++++++ .../java/school_project/MapGeneratorTest.java | 19 +++++++++ 2 files changed, 60 insertions(+) create mode 100644 app/src/main/java/school_project/MapGenerator.java create mode 100644 app/src/test/java/school_project/MapGeneratorTest.java 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..b18f279 --- /dev/null +++ b/app/src/main/java/school_project/MapGenerator.java @@ -0,0 +1,41 @@ +package school_project; + +import java.util.Random; + +public class MapGenerator { + private static 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(3, 5), rand.nextInt(3, 5)); + case Medium -> map_size = new Vec2(rand.nextInt(5, 8), rand.nextInt(5, 8)); + case Difficult -> { + map_size = new Vec2(rand.nextInt(8, 10), rand.nextInt(8, 10)); + depth = 2; + } + default -> map_size = new Vec2(); + } + + 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(); + } + } + return new Map(map_shape); + } +} 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..e9695d8 --- /dev/null +++ b/app/src/test/java/school_project/MapGeneratorTest.java @@ -0,0 +1,19 @@ +package school_project; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class MapGeneratorTest { + + @Test + void generate() { + Map map_e = MapGenerator.generate(MapGenerator.Difficulty.Easy); + Map map_m = MapGenerator.generate(MapGenerator.Difficulty.Medium); + Map map_d = MapGenerator.generate(MapGenerator.Difficulty.Difficult); + + System.out.println(map_e); + System.out.println(map_m); + System.out.println(map_d); + } +} \ No newline at end of file -- 2.46.0 From 5dceec0f79c1c8a041dd05c5514687f765fcfccb Mon Sep 17 00:00:00 2001 From: Debucquoy Date: Thu, 27 Apr 2023 10:04:31 +0200 Subject: [PATCH 2/4] Adding Array Copy for matrix Currently only boolean but we can add more if we need --- .../java/school_project/MapGenerator.java | 14 ++++++++-- .../main/java/school_project/Utils/Array.java | 13 ++++++++++ .../java/school_project/Utils/ArrayTest.java | 26 +++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/school_project/Utils/Array.java create mode 100644 app/src/test/java/school_project/Utils/ArrayTest.java diff --git a/app/src/main/java/school_project/MapGenerator.java b/app/src/main/java/school_project/MapGenerator.java index b18f279..d1844e1 100644 --- a/app/src/main/java/school_project/MapGenerator.java +++ b/app/src/main/java/school_project/MapGenerator.java @@ -1,9 +1,13 @@ package school_project; +import school_project.Utils.Array; + +import java.util.ArrayList; +import java.util.Arrays; import java.util.Random; public class MapGenerator { - private static Random rand = new Random(); + private static final Random rand = new Random(); public enum Difficulty { Easy, Medium, @@ -24,6 +28,7 @@ public class MapGenerator { default -> map_size = new Vec2(); } + // Cut edges boolean[][] map_shape = new boolean[map_size.x][map_size.y]; for (boolean[] b : map_shape) { Arrays.fill(b, true); @@ -36,6 +41,11 @@ public class MapGenerator { map_shape[i][j] = rand.nextBoolean(); } } - return new Map(map_shape); + Map ret = new Map(map_shape); + boolean[][] piece_layout = Array.MatrixCopyOf(map_shape); + + + //generate pieces + return ret; } } 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/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 -- 2.46.0 From 7e434f9a6d509269737534ba5961b8424f9e9af0 Mon Sep 17 00:00:00 2001 From: Debucquoy Date: Thu, 27 Apr 2023 10:35:36 +0200 Subject: [PATCH 3/4] addition of vectors Vector to string --- app/src/main/java/school_project/Vec2.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/src/main/java/school_project/Vec2.java b/app/src/main/java/school_project/Vec2.java index 7bf6ed9..6529e15 100644 --- a/app/src/main/java/school_project/Vec2.java +++ b/app/src/main/java/school_project/Vec2.java @@ -26,4 +26,13 @@ public class Vec2 implements Serializable { } return false; } + + public Vec2 add(Vec2 o){ + return new Vec2(x + o.x, y + o.y); + } + + @Override + public String toString() { + return "("+x+","+y+")"; + } } -- 2.46.0 From 16ab4b538727694d8d1c33b3a2d6419c6ad6f8c9 Mon Sep 17 00:00:00 2001 From: Debucquoy Date: Thu, 27 Apr 2023 10:35:50 +0200 Subject: [PATCH 4/4] Generating Pieces in the map --- .../java/school_project/MapGenerator.java | 27 +++++++++++++++++++ .../java/school_project/MapGeneratorTest.java | 19 ++++++++----- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/school_project/MapGenerator.java b/app/src/main/java/school_project/MapGenerator.java index d1844e1..f6a44b8 100644 --- a/app/src/main/java/school_project/MapGenerator.java +++ b/app/src/main/java/school_project/MapGenerator.java @@ -4,6 +4,7 @@ import school_project.Utils.Array; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Random; public class MapGenerator { @@ -43,7 +44,33 @@ public class MapGenerator { } 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(1, 4); + 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/test/java/school_project/MapGeneratorTest.java b/app/src/test/java/school_project/MapGeneratorTest.java index e9695d8..d052592 100644 --- a/app/src/test/java/school_project/MapGeneratorTest.java +++ b/app/src/test/java/school_project/MapGeneratorTest.java @@ -8,12 +8,19 @@ class MapGeneratorTest { @Test void generate() { - Map map_e = MapGenerator.generate(MapGenerator.Difficulty.Easy); - Map map_m = MapGenerator.generate(MapGenerator.Difficulty.Medium); - Map map_d = MapGenerator.generate(MapGenerator.Difficulty.Difficult); + Map[] maps = new Map[] { + MapGenerator.generate(MapGenerator.Difficulty.Easy), + MapGenerator.generate(MapGenerator.Difficulty.Medium), + MapGenerator.generate(MapGenerator.Difficulty.Difficult), + }; - System.out.println(map_e); - System.out.println(map_m); - System.out.println(map_d); + 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 -- 2.46.0