Advertisement
CaptainSpaceCat

3DPMaze - RecursiveGen

Jul 18th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.lang.Math;
  3.  
  4. public class RecursiveGen {
  5.  
  6.   public RecursiveGen() {
  7.   }
  8.  
  9.   private void initMaze(boolean[][][] m) {
  10.     for (int i = 0; i < m.length; i++) {
  11.       for (int v = 0; v < m[0].length; v++) {
  12.         for (int n = 0; n < 4; n++) {
  13.           m[i][v][n] = true;
  14.         }
  15.       }
  16.     }
  17.   }
  18.  
  19.   private static int[][] dirTab = {
  20.     {0, -1},
  21.     {1, 0},
  22.     {0, 1},
  23.     {-1, 0}
  24.   };
  25.  
  26.   public boolean validateMaze(boolean[][][] m) {
  27.     int dir = 2;
  28.     int w = m.length;
  29.     int h = m[0].length;
  30.     int[] currentPos = new int[] {0, 0};
  31.     boolean[][][] board = new boolean[w][h][4];
  32.     ArrayList<int[]> stack = new ArrayList<int[]>();
  33.     stack.add(currentPos);
  34.     while (true) {
  35.       if (m[currentPos[0]][currentPos[1]][dir]) {
  36.        
  37.       } else {
  38.         currentPos = addCoords(currentPos, dirTab[dir]);
  39.       }
  40.       if (currentPos[0] == w-1 && currentPos[1] == h-1) {
  41.         return true;
  42.       } else if (stack.size() == 0) {
  43.         return false;
  44.       }
  45.     }
  46.   }
  47.  
  48.   public boolean[][][] generate(int x, int y) {
  49.     boolean[][][] maze = new boolean[x][y][4];
  50.     initMaze(maze);
  51.     ArrayList<int[]> stack = new ArrayList<int[]>();
  52.     int[] currentPos = new int[] {0, 0};
  53.     stack.add(currentPos);
  54.     currentPos = addCoords(currentPos, dirTab[random(1, 2)]);
  55.     //System.out.println(currentPos[0] + ":" + currentPos[1]);
  56.     while (stack.size() > 0) {
  57.       int[] availableCells = getAvailableCells(currentPos, maze);
  58.       if (availableCells.length > 0) {
  59.         stack.add(currentPos);
  60.         int choice = availableCells[random(0, availableCells.length - 1)];
  61.         removeWalls(currentPos, choice, maze);
  62.         currentPos = addCoords(currentPos, dirTab[choice]);
  63.         //System.out.println(currentPos[0] + ":" + currentPos[1]);
  64.       } else {
  65.         currentPos = stack.remove(stack.size()-1);
  66.         //System.out.println(" <=" + currentPos[0] + ":" + currentPos[1]);
  67.       }
  68.     }
  69.     return maze;
  70.   }
  71.  
  72.   private int[] getAvailableCells(int[] pos, boolean[][][] m) {
  73.     ArrayList<Integer> options = new ArrayList<Integer>();
  74.     if (!(pos[0] == m.length-1 && pos[1] == m[0].length-1)) {
  75.       for (int i = 0; i < 4; i++) {
  76.         if (!cellUsed(addCoords(pos, dirTab[i]), m)) {
  77.           options.add(i);
  78.         }
  79.       }
  80.     } else {
  81.       m[m.length-1][m[0].length-1][2] = false;
  82.       //System.out.println("----====End Located====----");
  83.     }
  84.     int[] result = new int[options.size()];
  85.     for (int i = 0; i < result.length; i++) {
  86.       result[i] = options.get(i);
  87.     }
  88.     return result;
  89.   }
  90.  
  91.   private void removeWalls(int[] pos, int dir, boolean[][][] m) {
  92.     m[pos[0]][pos[1]][dir] = false;
  93.     int[] nPos = addCoords(pos, dirTab[dir]);
  94.     //m[pos[0] + dirTab[dir][0]][pos[1] + dirTab[dir][1]][(dir+2)%4] = false;
  95.     m[nPos[0]][nPos[1]][(dir+2)%4] = false;
  96.   }
  97.  
  98.   private boolean cellUsed(int[] pos, boolean[][][] m) {
  99.     if (pos[0] == -1 || pos[1] == -1 || pos[0] == m.length || pos[1] == m[0].length) {
  100.       return true;
  101.     }
  102.     for (int i = 0; i < 4; i++) {
  103.       if (!m[pos[0]][pos[1]][i]) {
  104.         return true;
  105.       }
  106.     }
  107.     return false;
  108.   }
  109.  
  110.   private int random(int low, int high) {
  111.     return (int)(Math.random()*(high+1-low)) + low;
  112.   }
  113.  
  114.   private int[] addCoords(int[] a, int[] b) {
  115.     return new int[] {a[0]+b[0], a[1]+b[1]};
  116.   }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement