Merge branch 'master' of git.herisson.ovh:undefined_name/School_project
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
commit
53972cd1ef
78
app/src/main/java/school_project/MapGenerator.java
Normal file
78
app/src/main/java/school_project/MapGenerator.java
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
package school_project;
|
||||||
|
|
||||||
|
import school_project.Utils.Array;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class MapGenerator {
|
||||||
|
private static final 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cut edges
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -2,11 +2,13 @@ package school_project.Parsers;
|
|||||||
|
|
||||||
import javafx.util.Pair;
|
import javafx.util.Pair;
|
||||||
import school_project.Map;
|
import school_project.Map;
|
||||||
|
import school_project.Piece;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.NotSerializableException;
|
import java.io.NotSerializableException;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is used to find the right parser to parser a save/level file.
|
* This is used to find the right parser to parser a save/level file.
|
||||||
@ -80,4 +82,51 @@ public class FileParserFactory {
|
|||||||
}
|
}
|
||||||
return new Pair<FileParser, Boolean>(fileParser, save_data);
|
return new Pair<FileParser, Boolean>(fileParser, save_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
Scanner in = new Scanner(System.in);
|
||||||
|
Map level = new Map();
|
||||||
|
|
||||||
|
System.out.print("Entrez le nom du fichier:");
|
||||||
|
File file = new File(in.nextLine());
|
||||||
|
|
||||||
|
System.out.print("Entrez la largeur de la map:");
|
||||||
|
int map_width = in.nextInt();
|
||||||
|
|
||||||
|
System.out.print("Entrez la hauteur de la map:");
|
||||||
|
int map_height = in.nextInt();
|
||||||
|
|
||||||
|
boolean[][] map_shape = new boolean[map_height][map_width];
|
||||||
|
|
||||||
|
for (int i = 0; i < map_height; i++) {
|
||||||
|
for (int j = 0; j < map_width; j++) {
|
||||||
|
System.out.print("mur (" + i + ", " + j + ")? (y/n):");
|
||||||
|
map_shape[i][j] = in.next(".").charAt(0) != 'y';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
level.setShape(map_shape);
|
||||||
|
System.out.println(level);
|
||||||
|
System.out.print("Entrez le nombre de pieces:");
|
||||||
|
int piece_amount = in.nextInt();
|
||||||
|
|
||||||
|
for (int i = 0; i < piece_amount; i++) {
|
||||||
|
System.out.print("Entrez la largeur de la piece" + (i+1) +": ");
|
||||||
|
int _piece_width = in.nextInt();
|
||||||
|
|
||||||
|
System.out.print("Entrez la hauteur de la piece" + (i+1) +": ");
|
||||||
|
int _piece_height = in.nextInt();
|
||||||
|
boolean[][] _piece_shape = new boolean[_piece_height][_piece_width];
|
||||||
|
|
||||||
|
for (int k = 0; k < _piece_height; k++) {
|
||||||
|
for (int j = 0; j < _piece_width; j++) {
|
||||||
|
System.out.print("mur (" + k + ", " + j + ")? (y/n):");
|
||||||
|
_piece_shape[k][j] = in.next(".").charAt(0) != 'y';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
level.addPiece(new Piece(_piece_shape));
|
||||||
|
}
|
||||||
|
saveFileFromMap(file, level);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,5 +1,10 @@
|
|||||||
package school_project;
|
package school_project;
|
||||||
|
|
||||||
|
import javafx.scene.paint.Color;
|
||||||
|
import javafx.scene.paint.Paint;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represent a Piece in the game.
|
* Represent a Piece in the game.
|
||||||
* Every Piece should be contained in a Map Object.
|
* Every Piece should be contained in a Map Object.
|
||||||
@ -10,13 +15,20 @@ public class Piece extends Shape{
|
|||||||
|
|
||||||
private Vec2 Position;
|
private Vec2 Position;
|
||||||
private Map linked_map;
|
private Map linked_map;
|
||||||
|
private transient Paint color; // https://www.baeldung.com/java-transient-keyword
|
||||||
public Piece() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Piece(boolean[][] matrix) {
|
public Piece(boolean[][] matrix) {
|
||||||
super(matrix);
|
super(matrix);
|
||||||
|
Random rand = new Random();
|
||||||
|
color = new Color(rand.nextDouble(), rand.nextDouble(), rand.nextDouble(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColor(Paint p){
|
||||||
|
color = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Paint getColor(){
|
||||||
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vec2 getPosition() {
|
public Vec2 getPosition() {
|
||||||
@ -27,6 +39,7 @@ public class Piece extends Shape{
|
|||||||
this.Position = position;
|
this.Position = position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set the map the piece is into the the map argument
|
* set the map the piece is into the the map argument
|
||||||
* @param map map where to place the piece
|
* @param map map where to place the piece
|
||||||
|
13
app/src/main/java/school_project/Utils/Array.java
Normal file
13
app/src/main/java/school_project/Utils/Array.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -26,4 +26,13 @@ public class Vec2 implements Serializable {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Vec2 add(Vec2 o){
|
||||||
|
return new Vec2(x + o.x, y + o.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "("+x+","+y+")";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
1
app/src/main/resources/school_project/level11.level
Normal file
1
app/src/main/resources/school_project/level11.level
Normal file
@ -0,0 +1 @@
|
|||||||
|
SMSη<><CEB7>€3<>€"π"°"p"°€SME
|
26
app/src/test/java/school_project/MapGeneratorTest.java
Normal file
26
app/src/test/java/school_project/MapGeneratorTest.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package school_project;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class MapGeneratorTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void generate() {
|
||||||
|
Map[] maps = new Map[] {
|
||||||
|
MapGenerator.generate(MapGenerator.Difficulty.Easy),
|
||||||
|
MapGenerator.generate(MapGenerator.Difficulty.Medium),
|
||||||
|
MapGenerator.generate(MapGenerator.Difficulty.Difficult),
|
||||||
|
};
|
||||||
|
|
||||||
|
for(Map m: maps){
|
||||||
|
System.out.println("==========");
|
||||||
|
System.out.println(m);
|
||||||
|
System.out.println("++++++++++++++++++++");
|
||||||
|
for (Piece p: m.getPieces()){
|
||||||
|
System.out.println(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -36,7 +36,7 @@ class PieceTest {
|
|||||||
{true, false, true},
|
{true, false, true},
|
||||||
};
|
};
|
||||||
|
|
||||||
Piece piece1 = new Piece();
|
Piece piece1 = new Piece(piece2_matrix);
|
||||||
piece1.setShape(piece1_matrix);
|
piece1.setShape(piece1_matrix);
|
||||||
|
|
||||||
Piece piece2 = new Piece(piece2_matrix);
|
Piece piece2 = new Piece(piece2_matrix);
|
||||||
|
26
app/src/test/java/school_project/Utils/ArrayTest.java
Normal file
26
app/src/test/java/school_project/Utils/ArrayTest.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user