Merge branch 'master' into menu
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

This commit is contained in:
Debucquoy Anthony 2023-05-05 12:11:19 +02:00
commit 752c722b0c
10 changed files with 233 additions and 10 deletions

View File

@ -5,14 +5,14 @@ name: Check_Requirement
steps: steps:
- name: base_check - name: base_check
image: gradle:jdk19-alpine image: gradle:jdk11-alpine
commands: commands:
- ./gradlew clean - ./gradlew clean
- ./gradlew build - ./gradlew build
- ./gradlew test - ./gradlew test
- name: syntax_check - name: syntax_check
image: gradle:jdk19-alpine image: gradle:jdk11-alpine
commands: commands:
- ./gradlew check - ./gradlew check
@ -44,6 +44,6 @@ depends_on:
- Check_Requirement - Check_Requirement
--- ---
kind: signature kind: signature
hmac: f7588a8f835401820f6f596cad344ab01794dc0abcf9f81c989c625844ab4cc3 hmac: 6b154c74ec624ce2d5867386bb7a6ee51cae9153457a8ce15f53e54546ccbc0e
... ...

View File

@ -0,0 +1,84 @@
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(2) + 3, rand.nextInt(2) + 3);
break;
case Medium:
map_size = new Vec2(rand.nextInt(3)+5, rand.nextInt(3)+5);
break;
case Difficult:
map_size = new Vec2(rand.nextInt(2)+8, rand.nextInt(2)+8);
depth = 2;
break;
default:
map_size = new Vec2();
break;
}
// 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(3)+1;
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

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

View File

@ -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
@ -55,7 +68,8 @@ public class Piece extends Shape{
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(obj instanceof Piece pieceObj){ if(obj instanceof Piece){
Piece pieceObj = (Piece) obj;
if( pieceObj.getPosition().equals(this.getPosition()) && pieceObj.getShape().equals(getShape())) { if( pieceObj.getPosition().equals(this.getPosition()) && pieceObj.getShape().equals(getShape())) {
return true; return true;
} }

View 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;
}
}

View File

@ -21,9 +21,19 @@ public class Vec2 implements Serializable {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj instanceof Vec2 vec) { if (obj instanceof Vec2) {
Vec2 vec = (Vec2) obj;
return this.x == vec.x && this.y == vec.y; return this.x == vec.x && this.y == vec.y;
} }
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+")";
}
} }

View File

@ -0,0 +1 @@
SMSη<><CEB7>3<>€"π"°"p"°€SME

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

View File

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

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