Generating Pieces in the map
This commit is contained in:
		@ -4,6 +4,7 @@ import school_project.Utils.Array;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.Random;
 | 
			
		||||
 | 
			
		||||
public class MapGenerator {
 | 
			
		||||
@ -43,7 +44,33 @@ public class MapGenerator {
 | 
			
		||||
        }
 | 
			
		||||
        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;
 | 
			
		||||
 | 
			
		||||
@ -8,12 +8,19 @@ 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);
 | 
			
		||||
        Map[] maps = new Map[] {
 | 
			
		||||
            MapGenerator.generate(MapGenerator.Difficulty.Easy),
 | 
			
		||||
            MapGenerator.generate(MapGenerator.Difficulty.Medium),
 | 
			
		||||
            MapGenerator.generate(MapGenerator.Difficulty.Difficult),
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        System.out.println(map_e);
 | 
			
		||||
        System.out.println(map_m);
 | 
			
		||||
        System.out.println(map_d);
 | 
			
		||||
        for(Map m: maps){
 | 
			
		||||
            System.out.println("==========");
 | 
			
		||||
            System.out.println(m);
 | 
			
		||||
            System.out.println("++++++++++++++++++++");
 | 
			
		||||
            for (Piece p: m.getPieces()){
 | 
			
		||||
                System.out.println(p);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user