MapGenerator #34

Merged
tonitch merged 5 commits from MapGenerator into master 2023-05-01 18:38:53 +02:00
2 changed files with 40 additions and 6 deletions
Showing only changes of commit 16ab4b5387 - Show all commits

View File

@ -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<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
return ret;

View File

@ -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);
}
}
}
}