domingo, 21 de agosto de 2022

Generación de laberintos III. Guardar en formato .png (mini)

Generación de laberintos aleatorios con formato .png. Comprimido al máximo posible. Un pixel equivale a un bloque de muro (█).


Código 1 (Blogspot_Laberinto_png):

package blogspot_laberinto_png;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Blogspot_Laberinto_png {

    public static void main(String[] args) {

        // tamaño laberinto
        int x = 97;
        int y = 97;

        String strMaze = new Maze(y - 2, x - 2).toString();

        // resolucion de imagen .png
        int res_x = 99;
        int res_y = 99;

        BufferedImage imagen = new BufferedImage(res_x, res_y, BufferedImage.TYPE_INT_RGB);
        Lienzo.Dibujar((Graphics2D) imagen.getGraphics(), res_x, res_y, strMaze);
        try {
            ImageIO.write(imagen, "png", new File("Laberinto.png"));
        } catch (IOException e) {
        }

    }
}


Código 2 (Lienzo.java):

package blogspot_laberinto;

import java.awt.Color;
import java.awt.Graphics2D;

class Lienzo {

    static void Dibujar(Graphics2D g, int x, int y, String strMaze) {
        
        //tamaño bloque (1x1 pixels)
        int tCuadroX = 1;

        String[] arrMaze = strMaze.split("\n");

        //fondo blanco
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, x, y);

        //dibujar bloques muro
        g.setColor(Color.BLUE);
        for (int j = 0; j < arrMaze.length; j++) {
            for (int i = 0; i < arrMaze.length; i++) {
                if (arrMaze[j].charAt(i) != ' ') {
                    g.fillRect(tCuadroX * (i + 1), tCuadroX * (j + 1), tCuadroX, tCuadroX);
                }
            }
        }
    }

}


Código 3 (Maze.java):

package blogspot_laberinto_png;

import java.util.LinkedList;
import java.util.Random;

public class Maze {

    public static final char PASSAGE_CHAR = ' ';
    public static final char WALL_CHAR = '█';
    public static final boolean WALL = false;
    public static final boolean PASSAGE = !WALL;

    private final boolean map[][];
    private final int width;
    private final int height;

    public Maze(final int width, final int height) {
        this.width = width;
        this.height = height;
        this.map = new boolean[width][height];

        final LinkedList<int[]> frontiers = new LinkedList<>();
        final Random random = new Random();
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        frontiers.add(new int[]{x, y, x, y});

        while (!frontiers.isEmpty()) {
            final int[] f = frontiers.remove(random.nextInt(frontiers.size()));
            x = f[2];
            y = f[3];
            if (map[x][y] == WALL) {
                map[f[0]][f[1]] = map[x][y] = PASSAGE;
                if (x >= 2 && map[x - 2][y] == WALL) {
                    frontiers.add(new int[]{x - 1, y, x - 2, y});
                }
                if (y >= 2 && map[x][y - 2] == WALL) {
                    frontiers.add(new int[]{x, y - 1, x, y - 2});
                }
                if (x < width - 2 && map[x + 2][y] == WALL) {
                    frontiers.add(new int[]{x + 1, y, x + 2, y});
                }
                if (y < height - 2 && map[x][y + 2] == WALL) {
                    frontiers.add(new int[]{x, y + 1, x, y + 2});
                }
            }
        }

    }

    @Override
    public String toString() {
        final StringBuffer b = new StringBuffer();
        for (int x = 0; x < width + 2; x++) {
            b.append(WALL_CHAR);
        }
        b.append('\n');
        for (int y = 0; y < height; y++) {
            b.append(WALL_CHAR);
            for (int x = 0; x < width; x++) {
                b.append(map[x][y] == WALL ? WALL_CHAR : PASSAGE_CHAR);
            }
            b.append(WALL_CHAR);
            b.append('\n');
        }
        for (int x = 0; x < width + 2; x++) {
            b.append(WALL_CHAR);
        }
        b.append('\n');
        return b.toString();
    }

}


Resultado:




No hay comentarios:

Publicar un comentario