Basic Random Tile Map Generation

Currently I've got a map like this:

It's pretty simple but what I want is that area of tiles to be random, with the number of tiles present being random (Within a minimum and maximum amount of tiles of course) and the positions of the tiles being random (but still all connected to each other), but I'm not exactly sure how I should go about implementing that.

I'm sort of looking for a system that will generate random sections of tiles, a bunch of tiles within a group at random positions around the screen with all these sections being connected to each other via a larger section of tiles. Like the example shown in this perfect drawing I've quickly done:

Each square would be a group of tiles, they're all of different shapes and sizes but all connected together via another large (or small) group of tiles in the middle.

I have looked into grid map generation before making this post but all the information I found would tend to go over more advanced map generation methods that have multiple layers which is not really what I need for my game so I was wondering if someone would possibly be able to advice me on what the best method would be to do this?

I'd very much appreciate it.

Generate a room.

Roll a die for how many adjacent rooms should be near it.

For each of those, Generate a Room

while(true)

Pick an edge-tile of the original room

Attempt to place this room there

if successful

break


I think that's all you're looking for. Let me know if you'd like something more detailed than an algorithm

NOTE: the above loop technically has a flaw that could cause an infinite loop. You would probably want a second break condition that says "if all adjacent squares have been checked, break"

• Would that actually generate the room though? Since surely that would just pick randomly where the room will be placed but the full room won't be generated. May 2, 2018 at 23:08
• Well, you'll have to find a place where you can generate a room; but given you already have code that places the first X * Y tile room in the middle, I assume you can build another p * q tile room where you find the space to build it? If I have the room 4 < x < 10 to 6 < y < 11 (a 6 x 4 room), if you choose the spot (4, 7) you will realize that you attach on your (new room's) right side. given nothing else has been placed, if we generate a 3x2 room, we will place it such that square (4,7) is adjacent, then go 3 to the left (1) and 2 up or down (dealer's choice.) May 3, 2018 at 6:35
• I sort of see. I have implemented a map generation system now but it doesn't work exactly how I want but at least it works. Just wondering though, what would be a good way to get a edge tile of a room since the center room is going to be bigger then a single tile so I can't simply use that single tile in the middle as the edge tile. May 3, 2018 at 19:08
• Mind you, I'm going from having none of your code: You may consider storing some identifier on Tiles telling you if there's a room there (it has an identifier stored) and which room it is. This may help in debugging and it will also aid you in building your tiles until you're more confident. In theory you can just track the size of your room and generically walk forward and backward, but this can be algorithmically difficult. May 3, 2018 at 19:10