public synchronized void generate() { System.out.println("Generating"); makeGrid(); solved = false; double rand = Math.random(); recursiveDivisionAlgo(); if (rand < 2) { return; } if (rand < 0.05) { // Alduos-Broder isn't liked very much. method = "Alduos-Broder"; alduosBroderAlgo(); } else if (rand < 0.25) { method = "Prim's"; primsAlgo(); } else if (rand < 0.50) { method = "Hunt and Kill"; huntAndKillAlgo(); } else if (rand < 0.75) { method = "DFS / Backtracker"; backtrackerAlgo(); // knockDownRandomWalls(); } else { method = "Recursive Division"; recursiveDivisionAlgo(); } }
private void recursiveDivisionAlgo(int top, int bottom, int left, int right, double texture) { if (bottom - top < 1 || right - left < 1) { return; } // The following Math.random() conditions slightly vary the texture of // the maze. int height = bottom - top, width = right - left; boolean verticalLine = width > height; // Math.random() < (texture)/Math.log(1 + width * height) if (Math.random() < Math.pow(texture, Math.cbrt(width * height))) { verticalLine = !verticalLine; } if (height < 3) { verticalLine = Math.random() > texture; } else if (width < 3) { verticalLine = Math.random() < texture; } if (!verticalLine) { // Note: bottom > top because the origin is in the top left. int randRow = top + (int) (Math.random() * (bottom - top)); int skipColumn = left + (int) (Math.random() * (right - left)); for (int i = top; i <= bottom; i++) { for (int j = left; j <= right; j++) { theGrid[i][j].setColor(green); } } repaint(); for (int i = left; i <= right; i++) { if (i != skipColumn) { theGrid[randRow][i].raiseWall(theGrid[randRow + 1][i]); repaint(); } } repaint(); for (int i = top; i <= bottom; i++) { for (int j = left; j <= right; j++) { theGrid[i][j].setColor(Color.WHITE); } } // repaint(); recursiveDivisionAlgo(top, randRow, left, right, texture); recursiveDivisionAlgo(randRow + 1, bottom, left, right, texture); } else if (right - left > 0) { int randColumn = left + (int) (Math.random() * (right - left)); int skipRow = top + (int) (Math.random() * (bottom - top)); for (int i = top; i <= bottom; i++) { for (int j = left; j <= right; j++) { theGrid[i][j].setColor(green); } } repaint(); for (int i = top; i <= bottom; i++) { if (i != skipRow) { theGrid[i][randColumn].raiseWall(theGrid[i][randColumn + 1]); repaint(); } } repaint(); for (int i = top; i <= bottom; i++) { for (int j = left; j <= right; j++) { theGrid[i][j].setColor(Color.WHITE); } } // repaint(); recursiveDivisionAlgo(top, bottom, left, randColumn, texture); recursiveDivisionAlgo(top, bottom, randColumn + 1, right, texture); } }
private void recursiveDivisionAlgo() { knockDownAllWalls(origin); recursiveDivisionAlgo(0, rows - 1, 0, columns - 1, 0.5 + randomNum() / 2); // uniteCells(); System.out.println("Done!"); }