# Maze Progress Bar - RecursiveGen

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