|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.io.PrintWriter; |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +public class PrimsGenerator{ |
| 6 | + private int row, col; |
| 7 | + public Square[][] maze; |
| 8 | + private ArrayList<Coordinate[]> frontiers; |
| 9 | + |
| 10 | + public PrimsGenerator(int r, int c) throws IOException{ |
| 11 | + row = r; |
| 12 | + col = c; |
| 13 | + maze = new Square[row][col]; |
| 14 | + frontiers = new ArrayList<Coordinate[]>(); |
| 15 | + fillWithWall(); |
| 16 | + generate(); |
| 17 | + addSE(); |
| 18 | + //writeToFile(); |
| 19 | + } |
| 20 | + |
| 21 | + private void fillWithWall(){ |
| 22 | + for (int r = 0; r < row; r++){ |
| 23 | + for (int c = 0; c < col; c++){ |
| 24 | + maze[r][c] = new Square("#"); |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + private void generate(){ |
| 30 | + Coordinate start = randomSquare(); |
| 31 | + frontiers.add(new Coordinate[]{start, start}); |
| 32 | + |
| 33 | + |
| 34 | + while (!frontiers.isEmpty()){ |
| 35 | + int randF = (int)(Math.random() * frontiers.size()); |
| 36 | + Coordinate[] fSq = frontiers.remove(randF); |
| 37 | + // for (Coordinate c : fSq){ |
| 38 | + // System.out.println(c); |
| 39 | + // } |
| 40 | + |
| 41 | + int r1 = fSq[0].getX(); |
| 42 | + int c1 = fSq[0].getY(); |
| 43 | + int r2 = fSq[1].getX(); |
| 44 | + int c2 = fSq[1].getY(); |
| 45 | + |
| 46 | + if (maze[r2][c2].getType().equals("#")){ |
| 47 | + maze[r1][c1] = new Square(" "); |
| 48 | + maze[r2][c2] = new Square(" "); |
| 49 | + |
| 50 | + |
| 51 | + if (c2 > 1 && maze[r2][c2 - 2].getType().equals("#")){ |
| 52 | + Coordinate cor1 = new Coordinate(r2, c2 - 1); |
| 53 | + Coordinate cor2 = new Coordinate(r2, c2 - 2); |
| 54 | + frontiers.add(new Coordinate[]{cor1, cor2}); |
| 55 | + } |
| 56 | + if (r2 > 1 && maze[r2 - 2][c2].getType().equals("#")){ |
| 57 | + Coordinate cor1 = new Coordinate(r2 - 1, c2); |
| 58 | + Coordinate cor2 = new Coordinate(r2 - 2, c2); |
| 59 | + frontiers.add(new Coordinate[]{cor1, cor2}); |
| 60 | + } |
| 61 | + if (c2 < (col - 2) && maze[r2][c2 + 2].getType().equals("#")){ |
| 62 | + Coordinate cor1 = new Coordinate(r2, c2 + 1); |
| 63 | + Coordinate cor2 = new Coordinate(r2, c2 + 2); |
| 64 | + frontiers.add(new Coordinate[]{cor1, cor2}); |
| 65 | + } |
| 66 | + if (r2 < (row - 2) && maze[r2 + 2][c2].getType().equals("#")){ |
| 67 | + Coordinate cor1 = new Coordinate(r2 + 1, c2); |
| 68 | + Coordinate cor2 = new Coordinate(r2 + 2, c2); |
| 69 | + frontiers.add(new Coordinate[]{cor1, cor2}); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // returns coordinates of random square within borders |
| 76 | + private Coordinate randomSquare(){ |
| 77 | + int x = (int)(Math.random() * (row - 2)) + 1; // excludes border squares |
| 78 | + int y = (int)(Math.random() * (col - 2)) + 1; |
| 79 | + return new Coordinate(x, y); |
| 80 | + } |
| 81 | + |
| 82 | + private void addSE(){ |
| 83 | + boolean check = false; |
| 84 | + for (int r = 0; r < row; r++){ |
| 85 | + if (check) |
| 86 | + break; |
| 87 | + for (int c = 0; c < col; c++){ |
| 88 | + if (maze[r][c].getType().equals(" ")){ |
| 89 | + maze[r][c] = new Square("S"); |
| 90 | + check = true; |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + check = false; |
| 97 | + for (int r = row - 1; r >= 0; r--){ |
| 98 | + if (check) |
| 99 | + break; |
| 100 | + for (int c = col - 1; c >= 0; c--){ |
| 101 | + if (maze[r][c].getType().equals(" ")){ |
| 102 | + maze[r][c] = new Square("E"); |
| 103 | + check = true; |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + /* |
| 110 | + private void writeToFile() throws IOException{ |
| 111 | + try { |
| 112 | + PrintWriter writer = new PrintWriter("Mazes/primsgeneratedfile.dat", "UTF-8"); |
| 113 | + writer.println(this.toString()); |
| 114 | + writer.close(); |
| 115 | + } |
| 116 | + catch (IOException e){ |
| 117 | + throw new IOException("unable to write to file"); |
| 118 | + } |
| 119 | + } |
| 120 | + */ |
| 121 | + private static String removeDoubleWalls(String content){ |
| 122 | + // check and remove double top wall |
| 123 | + int width = widthString(content); |
| 124 | + int iS = indexS(content); |
| 125 | + if (iS > (widthString(content) * 2)){ // double wall on top |
| 126 | + // System.out.println("removed totopp wall"); |
| 127 | + content = content.substring(width + 1); |
| 128 | + } |
| 129 | + |
| 130 | + // check and remove double left wall |
| 131 | + iS = indexS(content); |
| 132 | + width = widthString(content); |
| 133 | + if (iS == width + 3){ // double wall on left |
| 134 | + // System.out.println("removed lefleftt wall"); |
| 135 | + for (int i = 0; i < content.length(); i += width){ |
| 136 | + content = content.substring(0, i) + content.substring(i + 1); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // check and remove double right wall |
| 141 | + int iE = indexE(content); |
| 142 | + width = widthString(content); |
| 143 | + if (content.substring(iE + 3, iE + 4).equals("\n")){ // double wall on right |
| 144 | + // System.out.println("removed right wall"); |
| 145 | + for (int i = width - 1; i < content.length(); i += width){ |
| 146 | + content = content.substring(0, i) + content.substring(i + 1); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + // check and remove double bottom wall |
| 151 | + iE = indexE(content); |
| 152 | + width = widthString(content); |
| 153 | + if (iE < content.length() - (width * 2)){ // double wall on bottom |
| 154 | + // System.out.println("removed bottom wall"); |
| 155 | + content = content.substring(0, content.length() - width); |
| 156 | + } |
| 157 | + |
| 158 | + return content; |
| 159 | + } |
| 160 | + |
| 161 | + private static int indexS(String s){ |
| 162 | + return s.indexOf("S"); |
| 163 | + } |
| 164 | + |
| 165 | + private static int indexE(String s){ |
| 166 | + return s.indexOf("E"); |
| 167 | + } |
| 168 | + |
| 169 | + private static int widthString(String s){ |
| 170 | + return s.indexOf("\n"); |
| 171 | + } |
| 172 | + |
| 173 | + private static int heightString(String s){ |
| 174 | + int height = 1; |
| 175 | + String search = s; |
| 176 | + while (search.indexOf("\n") != -1){ |
| 177 | + height++; |
| 178 | + search = search.substring(search.indexOf("\n") + 1); |
| 179 | + } |
| 180 | + return height; |
| 181 | + } |
| 182 | + |
| 183 | + public String toString(){ |
| 184 | + String output = ""; |
| 185 | + |
| 186 | + int c = col + 2; |
| 187 | + while (c > 0){ |
| 188 | + output += "#"; |
| 189 | + c--; |
| 190 | + } |
| 191 | + output += "\n"; |
| 192 | + |
| 193 | + for (Square[] row : maze){ |
| 194 | + output += "#"; |
| 195 | + for (Square s : row){ |
| 196 | + output += s.getType(); |
| 197 | + } |
| 198 | + output += "#\n"; |
| 199 | + } |
| 200 | + |
| 201 | + c = col + 2; |
| 202 | + while (c > 0){ |
| 203 | + output += "#"; |
| 204 | + c--; |
| 205 | + } |
| 206 | + |
| 207 | + output = removeDoubleWalls(output); |
| 208 | + |
| 209 | + if (output.substring(output.length() - 1).equals("\n")) |
| 210 | + output = output.substring(0, output.length() - 1); |
| 211 | + |
| 212 | + return output; |
| 213 | + } |
| 214 | + |
| 215 | + public static void main(String[] args) throws IOException{ |
| 216 | + PrimsGenerator pg = new PrimsGenerator(100, 200); |
| 217 | + System.out.print(pg); |
| 218 | + } |
| 219 | +} |
0 commit comments