Una forma sencilla y rápida de aprender JAVA, observando y deduciendo cómo se comporta el lenguaje a través de ejemplos prácticos.

Archivo del blog

sábado, 20 de agosto de 2022

Generación de laberintos II. Cambio de formato.

Ejemplo de tranformación


Código 1 (Blogspot_Laberinto.java):

package blogspot_laberinto;

public class Blogspot_Laberinto {

    public static void main(String[] args) {

        // Tamaño laberinto
        int x = 15;
        int y = 27;

        String strMaze = new Maze(y - 2, x - 2).toString();
        boolean[][] map0 = new StructMaze(strMaze, x, y).mMaze;
        StyleMaze sm = new StyleMaze(map0, x, y);

        // mostrar resultados
        System.out.println("\nLaberinto formato 1:\n");
        System.out.println(strMaze);
        System.out.println("\nLaberinto formato 2:\n");
        System.out.println(sm.map1);

    }

    private static class StructMaze {

        char[] vMaze;
        boolean[][] mMaze;

        public StructMaze(String strMaze, int x, int y) {
            vMaze = strMaze.replace("\n", "").toCharArray();
            mMaze = new boolean[x + 2][y + 2]; //se suma 2 para los bordes (ahorrar codigo)
            int cont = 0;
            for (int i = 1; i < x + 1; i++) {
                for (int j = 1; j < y + 1; j++) {
                    mMaze[i][j] = vMaze[cont] == '█';
                    cont++;
                }
            }
        }
    }

}


Código 2 (Maze.java):

package blogspot_laberinto;

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

}


Código 3 (StyleMaze.java):

package blogspot_laberinto;

public class StyleMaze {

    public String map1;

    public StyleMaze(boolean[][] p0, int x, int y) {
        this.map1 = "";
        String strMuro = " ├┬┌┤─┐┬┴└│├┘┴┤┼";
        boolean[][] mCod = combina();
        for (int i = 1; i < x + 1; i++) {
            for (int j = 1; j < y + 1; j++) {
                if (p0[i][j]) { // si hay muro
                    for (int k = 0; k < 16; k++) {
                        if (p0[i - 1][j] == mCod[k][0] && p0[i][j - 1] == mCod[k][1] && p0[i + 1][j] == mCod[k][2] && p0[i][j + 1] == mCod[k][3]) {
                            map1 += strMuro.charAt(k);
                        }
                    }
                } else {
                    map1 += " ";
                }
            }
            map1 += "\n";
        }
    }

    private boolean[][] combina() {
        String cod;
        boolean[][] mCod = new boolean[16][4];
        for (int i = 0; i < 16; i++) {
            cod = Integer.toBinaryString(i);
            while (cod.length() < 4) {
                cod = "0" + cod;
            }
            for (int j = 0; j < cod.length(); j++) {
                mCod[i][j] = cod.charAt(j) == '1';
            }
        }
        return mCod;
    }

}


Resultado:

run:

Laberinto formato 1:

███████████████████████████
█ █       █ █ █         █ █
█ █████ ███ █ █ █ █ █████ █
█       █   █   █ █       █
███████ ███ █ ███ █ █ █████
█       █   █ █ █ █ █ █ █ █
███ ███ ███ ███ █ █████ █ █
█   █     █ █ █   █ █ █   █
█████████ █ █ ███ █ █ █ ███
█               █     █ █ █
███████ ███ ███ █ █████ █ █
█   █   █   █             █
███ █████ █ █ █ █████ █ ███
█         █ █ █     █ █   █
███████████████████████████


Laberinto formato 2:

┌─┬───────┬─┬─┬─────────┬─┐
│ │       │ │ │         │ │
│ └───┤ ┌─┘ │ ┴ ┬ ┬ ├───┘ │
│       │   │   │ │       │
├─────┤ ├─┤ │ ┌─┤ │ ┬ ┌─┬─┤
│       │   │ │ │ │ │ │ │ │
├─┤ ┌─┤ └─┐ ├─┤ ┴ ├─┼─┤ ┴ │
│   │     │ │ │   │ │ │   │
├───┴───┤ ┴ ┴ └─┐ ┴ ┴ │ ┌─┤
│               │     │ │ │
├───┬─┤ ┌─┤ ┌─┤ ┴ ├───┘ ┴ │
│   │   │   │             │
├─┤ └───┘ ┬ │ ┬ ├───┐ ┬ ├─┤
│         │ │ │     │ │   │
└─────────┴─┴─┴─────┴─┴───┘

BUILD SUCCESSFUL (total time: 0 seconds)


No hay comentarios:

Publicar un comentario

Con la tecnología de Blogger.