Skip to content
Snippets Groups Projects
Commit df1ad16a authored by Wai Leung Chan's avatar Wai Leung Chan
Browse files

Add Recursive Division PoC

parent be9a12f1
No related merge requests found
package org.asdb;
public class DungeonGenerator {
public static void main(String[] args) {
DungeonGenerator dungeonGenerator = new DungeonGenerator(150, 30); // Example size: 40x20
dungeonGenerator.generateDungeon();
}
private char[][] dungeonMap;
private int width;
private int height;
public DungeonGenerator(int width, int height) {
this.width = width;
this.height = height;
this.dungeonMap = new char[height][width];
}
public void generateDungeon() {
// Fill the dungeon with walls
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
dungeonMap[i][j] = '.';
}
}
// Divide the dungeon recursively using the Recursive Division algorithm
divideSpace(0, 0, width - 1, height - 1, 3);
// Print the dungeon map
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print(dungeonMap[i][j]);
}
System.out.println();
}
}
private void divideSpace(int x1, int y1, int x2, int y2, int divisions) {
// Check if we reached the desired number of divisions
if (divisions == 0) {
// Draw a horizontal line through the middle of the space
int midY = (y1 + y2) / 2;
for (int j = x1; j <= x2; j++) {
dungeonMap[midY][j] = '#';
}
// Open passages in the walls
int passageX = getRandomNumberInRange(x1, x2 - 2); // Adjusted range to ensure space for 3 pixels
for (int k = passageX; k <= passageX + 2; k++) {
dungeonMap[midY][k] = '.';
}
return;
}
// Decide whether to divide horizontally or vertically
boolean divideHorizontally = Math.random() < 0.5;
// If we are dividing horizontally
if (divideHorizontally) {
// Determine the position of the dividing line
int splitY = getRandomNumberInRange(y1 + 1, y2 - 1);
// Draw the dividing line
for (int j = x1; j <= x2; j++) {
dungeonMap[splitY][j] = '#';
}
// Open passages in the walls
int passageX = getRandomNumberInRange(x1, x2); // Adjusted range
dungeonMap[splitY][passageX] = '.';
// Recur on the resulting subspaces
divideSpace(x1, y1, x2, splitY - 1, divisions - 1);
divideSpace(x1, splitY + 1, x2, y2, divisions - 1);
} else { // If we are dividing vertically
// Determine the position of the dividing line
int splitX = getRandomNumberInRange(x1 + 1, x2 - 1);
// Draw the dividing line
for (int i = y1; i <= y2; i++) {
dungeonMap[i][splitX] = '#';
}
// Open passages in the walls
int passageY = getRandomNumberInRange(y1, y2 - 2); // Adjusted range to ensure space for 3 pixels
for (int k = passageY; k <= passageY + 2; k++) {
dungeonMap[k][splitX] = '.';
}
// Recur on the resulting subspaces
divideSpace(x1, y1, splitX - 1, y2, divisions - 1);
divideSpace(splitX + 1, y1, x2, y2, divisions - 1);
}
}
// Helper method to generate a random number within a range
private int getRandomNumberInRange(int min, int max) {
return (int) (Math.random() * ((max - min) + 1)) + min;
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment