Generating Pieces in the map
This commit is contained in:
parent
7e434f9a6d
commit
16ab4b5387
@ -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;
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user