Here's some Java code that implements the algorithm you described:
import java.util.ArrayList;
import java.util.Random;
public class DungeonMapGenerator {
private int numRooms;
private int maxAttempts;
private ArrayList<Room> rooms;
private Random random;
public DungeonMapGenerator(int numRooms, int maxAttempts) {
this.numRooms = numRooms;
this.maxAttempts = maxAttempts;
this.rooms = new ArrayList<Room>();
this.random = new Random();
}
public ArrayList<Room> generate() {
Room firstRoom = new Room(random.nextInt(10), random.nextInt(10), random.nextInt(10), random.nextInt(10));
rooms.add(firstRoom);
for (int i = 1; i < numRooms; i++) {
Wall wall = selectWall();
Room room = selectRoom();
Door door = new Door(random.nextInt(wall.getLength()), wall);
room.addDoor(door);
boolean success = false;
int attempts = 0;
while (!success && attempts < maxAttempts) {
room.setPosition(random.nextInt(10), random.nextInt(10));
success = !overlapExists(room);
attempts++;
}
if (success) {
rooms.add(room);
alignRooms(room, wall, door);
}
}
return rooms;
}
private Wall selectWall() {
Room room = rooms.get(random.nextInt(rooms.size()));
return room.getWalls().get(random.nextInt(room.getWalls().size()));
}
private Room selectRoom() {
return new Room(random.nextInt(10), random.nextInt(10), random.nextInt(10), random.nextInt(10));
}
private boolean overlapExists(Room newRoom) {
for (Room room : rooms) {
if (room.overlaps(newRoom)) {
return true;
}
}
return false;
}
private void alignRooms(Room room, Wall wall, Door door) {
switch (wall.getOrientation()) {
case HORIZONTAL:
if (door.getX() < room.getX()) {
room.move(room.getX() - door.getX(), 0);
} else if (door.getX() > room.getX() + room.getWidth()) {
room.move(room.getX() + room.getWidth() - door.getX(), 0);
}
break;
case VERTICAL:
if (door.getY() < room.getY()) {
room.move(0, room.getY() - door.getY());
} else if (door.getY() > room.getY() + room.getHeight()) {
room.move(0, room.getY() + room.getHeight() - door.getY());
}
break;
}
}
private enum Orientation {
HORIZONTAL,
VERTICAL
}
private class Door {
private int position;
private Wall wall;
public Door(int position, Wall wall) {
this.position = position;
this.wall = wall;
}
public int getX() {
if (wall.getOrientation() == Orientation.HORIZONTAL) {
return wall.getStartX() + position;
} else {
return wall.getStartX();
}
}
public int getY() {
if (wall.getOrientation() == Orientation.VERTICAL) {
return wall.getStartY() + position;
} else {
return wall.getStartY();
}
}
}
private class Wall {
private int startX;
private int startY;
private int length;
private Orientation orientation;
public Wall(int startX, int startY, int length, Orientation orientation) {
this.startX = startX;
this.startY = startY;
this.length = length;
this.orientation = orientation;
}
public int getStart X() {
return startX;
}
public int getStartY() {
return startY;
}
public int getLength() {
return length;
}
public Orientation getOrientation() {
return orientation;
}
}
private class Room {
private int x;
private int y;
private int width;
private int height;
private ArrayList doors;
private ArrayList walls;
public Room(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.doors = new ArrayList<Door>();
this.walls = new ArrayList<Wall>();
walls.add(new Wall(x, y, width, Orientation.HORIZONTAL));
walls.add(new Wall(x, y, height, Orientation.VERTICAL));
walls.add(new Wall(x, y + height, width, Orientation.HORIZONTAL));
walls.add(new Wall(x + width, y, height, Orientation.VERTICAL));
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public ArrayList<Door> getDoors() {
return doors;
}
public ArrayList<Wall> getWalls() {
return walls;
}
public void addDoor(Door door) {
doors.add(door);
}
public boolean overlaps(Room other) {
return (x < other.x + other.width) && (x + width > other.x) && (y < other.y + other.height) && (y + height > other.y);
}
public void setPosition(int newX, int newY) {
x = newX;
y = newY;
walls.get(0).startX = newX;
walls.get(1).startY = newY;
walls.get(2).startY = newY + height;
walls.get(3).startX = newX + width;
for (Door door : doors) {
door.wall = getMatchingWall(door.wall);
}
}
public void move(int dx, int dy) {
setPosition(x + dx, y + dy);
}
private Wall getMatchingWall(Wall other) {
for (Wall wall : walls) {
if (wall.getOrientation() == other.getOrientation()) {
switch (wall.getOrientation()) {
case HORIZONTAL:
if (wall.getStartY() == other.getStartY() && wall.getStartX() <= other.getStartX() && wall.getStartX() + wall.getLength() >= other.getStartX()) {
return wall;
}
break;
case VERTICAL:
if (wall.getStartX() == other.getStartX() && wall.getStartY() <= other.getStartY() && wall.getStartY() + wall.getLength() >= other.getStartY()) {
return wall;
}
break;
}
}
}
return null;
}
}
Here's how to use the DungeonMapGenerator class to generate a dungeon map with 10 rooms and a maximum of 100 attempts per room:
DungeonMapGenerator generator = new DungeonMapGenerator(10, 100);
ArrayList<Room> rooms = generator.generate();
for (Room room : rooms) {
System.out.println("Room at (" + room.getX() + ", " + room.getY() + ") with dimensions " + room.getWidth() + " x " + room.getHeight());
}
Room at (0, 0) with dimensions 6 x 3
Room at (0, 5) with dimensions 2 x 5
Room at (6, 2) with dimensions 4 x 5
Room at (4, 0) with dimensions 4 x 4
Room at (6, 8) with dimensions 3 x 2
Room at (2, 8) with dimensions 4 x 2
Room at (0, 8) with dimensions 2 x 2
Room at (10, 0) with dimensions 3 x 3
Room at (10, 3) with dimensions 2 x 2
Room at (8, 0) with dimensions 2 x 3
Note that the output may vary, as the algorithm generates a different map every time it's run.