To achieve this, we'll create a simple Java program with a few classes representing the different components of the dungeon:
Dungeon: Represents the overall dungeon.
Room: Represents a room in the dungeon.
Wall: Represents a wall in the dungeon.
Door: Represents a door between two rooms.
First, let's create the Wall, Door, and Room classes:
class Wall {
int x1, y1, x2, y2;
Wall(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
}
class Door {
int x, y;
Door(int x, int y) {
this.x = x;
this.y = y;
}
}
class Room {
int x, y, width, height;
ArrayList<Door> doors;
Room(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
doors = new ArrayList<>();
}
void addDoor(Door door) {
doors.add(door);
}
boolean overlaps(Room other) {
return !(this.x + this.width <= other.x || other.x + other.width <= this.x ||
this.y + this.height <= other.y || other.y + other.height <= this.y);
}
}
Now, let's create the Dungeon class that will handle generating the dungeon map:
import java.util.ArrayList;
import java.util.Random;
class Dungeon {
ArrayList<Room> rooms;
ArrayList<Wall> walls;
int maxAttempts;
Random random;
Dungeon(int maxAttempts) {
rooms = new ArrayList<>();
walls = new ArrayList<>();
this.maxAttempts = maxAttempts;
random = new Random();
}
void addWall(Wall wall) {
walls.add(wall);
}
boolean generateMap() {
while (!walls.isEmpty()) {
int wallIndex = random.nextInt(walls.size());
Wall selectedWall = walls.remove(wallIndex);
int doorX = random.nextInt(selectedWall.x2 - selectedWall.x1) + selectedWall.x1;
int doorY = random.nextInt(selectedWall.y2 - selectedWall.y1) + selectedWall.y1;
Door wallDoor = new Door(doorX, doorY);
int roomWidth = random.nextInt(10) + 5;
int roomHeight = random.nextInt(10) + 5;
Room newRoom = new Room(doorX - roomWidth / 2, doorY - roomHeight / 2, roomWidth, roomHeight);
newRoom.addDoor(wallDoor);
int attempts = 0;
while (overlapsExistingRooms(newRoom) && attempts < maxAttempts) {
newRoom.x = newRoom.x + random.nextInt(3) - 1;
newRoom.y = newRoom.y + random.nextInt(3) - 1;
attempts++;
}
if (attempts < maxAttempts) {
rooms.add(newRoom);
} else {
return false;
}
}
return true;
}
private boolean overlapsExistingRooms(Room newRoom) {
for (Room existingRoom : rooms) {
if (existingRoom.overlaps(newRoom)) {
return true;
}
}
return false;
}
}
Finally, you can create a Main class to test the dungeon generation algorithm by adding walls, setting the maximum number of attempts to place a room, and generating the dungeon map:
public class Main {
public static void main(String[] args) {
Dungeon dungeon = new Dungeon(100); // Set maxAttempts to 100
// Add walls to the dungeon
dungeon.addWall(new Wall(0, 0, 50, 0));
dungeon.addWall(new Wall(50, 0, 50, 50));
dungeon.addWall(new Wall(50, 50, 0, 50));
dungeon.addWall(new Wall(0, 50, 0, 0));
// Generate the dungeon map
if (dungeon.generateMap()) {
System.out.println("Dungeon generation successful!");
System.out.println("Number of rooms: " + dungeon.rooms.size());
// Print the rooms and doors
for (int i = 0; i < dungeon.rooms.size(); i++) {
Room room = dungeon.rooms.get(i);
System.out.println("Room " + (i + 1) + ": x=" + room.x + ", y=" + room.y + ", width=" + room.width + ", height=" + room.height);
System.out.println("Doors:");
for (Door door : room.doors) {
System.out.println(" x=" + door.x + ", y=" + door.y);
}
}
} else {
System.out.println("Dungeon generation failed!");
}
}
}
This Main class creates a Dungeon object, adds the boundary walls, generates the dungeon map, and prints the rooms and doors if the generation is successful. Note that this implementation only prints the dungeon map to the console. You could create a graphical representation of the dungeon using a library like JavaFX or AWT if desired.