Generating Pieces in the map
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Debucquoy 2023-04-27 10:35:50 +02:00
parent 7e434f9a6d
commit 16ab4b5387
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
2 changed files with 40 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import school_project.Utils.Array;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.Random; import java.util.Random;
public class MapGenerator { public class MapGenerator {
@ -43,7 +44,33 @@ public class MapGenerator {
} }
Map ret = new Map(map_shape); Map ret = new Map(map_shape);
boolean[][] piece_layout = Array.MatrixCopyOf(map_shape); boolean[][] piece_layout = Array.MatrixCopyOf(map_shape);
ArrayList<Vec2> 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 //generate pieces
return ret; return ret;

View File

@ -8,12 +8,19 @@ class MapGeneratorTest {
@Test @Test
void generate() { void generate() {
Map map_e = MapGenerator.generate(MapGenerator.Difficulty.Easy); Map[] maps = new Map[] {
Map map_m = MapGenerator.generate(MapGenerator.Difficulty.Medium); MapGenerator.generate(MapGenerator.Difficulty.Easy),
Map map_d = MapGenerator.generate(MapGenerator.Difficulty.Difficult); MapGenerator.generate(MapGenerator.Difficulty.Medium),
MapGenerator.generate(MapGenerator.Difficulty.Difficult),
};
System.out.println(map_e); for(Map m: maps){
System.out.println(map_m); System.out.println("==========");
System.out.println(map_d); System.out.println(m);
System.out.println("++++++++++++++++++++");
for (Piece p: m.getPieces()){
System.out.println(p);
}
}
} }
} }