MapGenerator #34
41
app/src/main/java/school_project/MapGenerator.java
Normal file
41
app/src/main/java/school_project/MapGenerator.java
Normal file
@ -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);
|
||||
tonitch marked this conversation as resolved
|
||||
}
|
||||
}
|
19
app/src/test/java/school_project/MapGeneratorTest.java
Normal file
19
app/src/test/java/school_project/MapGeneratorTest.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user
boucle qui fait le tour des extrémités de la matrice