Merge branch 'master' into menu
This commit is contained in:
		
							
								
								
									
										84
									
								
								app/src/main/java/school_project/MapGenerator.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								app/src/main/java/school_project/MapGenerator.java
									
									
									
									
									
										Normal 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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -2,11 +2,13 @@ package school_project.Parsers;
 | 
			
		||||
 | 
			
		||||
import javafx.util.Pair;
 | 
			
		||||
import school_project.Map;
 | 
			
		||||
import school_project.Piece;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.FileNotFoundException;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.NotSerializableException;
 | 
			
		||||
import java.util.Scanner;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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;
 | 
			
		||||
 | 
			
		||||
import javafx.scene.paint.Color;
 | 
			
		||||
import javafx.scene.paint.Paint;
 | 
			
		||||
 | 
			
		||||
import java.util.Random;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Represent a Piece in the game.
 | 
			
		||||
 * Every Piece should be contained in a Map Object.
 | 
			
		||||
@ -10,13 +15,20 @@ public class Piece extends Shape{
 | 
			
		||||
 | 
			
		||||
    private Vec2 Position;
 | 
			
		||||
    private Map linked_map;
 | 
			
		||||
 | 
			
		||||
    public Piece() {
 | 
			
		||||
        super();
 | 
			
		||||
    }
 | 
			
		||||
    private transient Paint color; // https://www.baeldung.com/java-transient-keyword
 | 
			
		||||
 | 
			
		||||
    public Piece(boolean[][] 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() {
 | 
			
		||||
@ -27,6 +39,7 @@ public class Piece extends Shape{
 | 
			
		||||
        this.Position = position;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * set the map the piece is into the the map argument
 | 
			
		||||
     * @param map map where to place the piece
 | 
			
		||||
@ -55,7 +68,8 @@ public class Piece extends Shape{
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    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())) {
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -21,9 +21,19 @@ public class Vec2 implements Serializable {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    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 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<05><><EFBFBD><EFBFBD>3<><33>"<22>"<22>"p"<22><11>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},
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        Piece piece1 = new Piece();
 | 
			
		||||
        Piece piece1 = new Piece(piece2_matrix);
 | 
			
		||||
        piece1.setShape(piece1_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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user