Skip to content

Instantly share code, notes, and snippets.

@Alex-Huleatt
Last active June 19, 2018 15:29
Show Gist options
  • Save Alex-Huleatt/aed8f157aac1c32cb96d41d150324572 to your computer and use it in GitHub Desktop.
Save Alex-Huleatt/aed8f157aac1c32cb96d41d150324572 to your computer and use it in GitHub Desktop.
Simple dungeon maker. Guaranteed connectedness.
'''
@author AlexHuleatt
Generate a simple, connected dungeon. Focus is on rooms, not maze-like features.
Output of get_dungeon is two sets:
1. A set of (y,x) tuples representing the position of walls
2. A set of (y,x) tuples representing the walls removed to make doors
'''
from random import randint, sample
def get_dungeon(height, width, door_count=1):
q,wa=[(2,2,width-2,height-2)],set()
while len(q):
lx,ly,hx,hy=q.pop()
if hx-lx<3 or hy-ly<3:
continue
w,h=randint(2,hx-lx-1),randint(2,hy-ly-1) #width, height of room
px,py=randint(lx,hx-w-1),randint(ly,hy-h-1) #upper left of room
rx,ry=px+w,py+h
room_walls = set()
for i in range(py,ry+1):
room_walls|={(i,px),(i,rx)}
for i in range(px,rx+1):
room_walls|={(py,i),(ry,i)}
room_walls -= set(sample(room_walls - {(py,px), (ry, px), (py, rx), (ry, rx)}, door_count)) #remove walls for doors
wa |= room_walls
q+=[
(lx,ly,hx,py-2),
(lx,py,px-2,hy),
(rx+2,py,hx,hy),
(px,ry+2,rx,hy),
(px+2,py+2,rx-1,ry-1)
]
return wa
k = 100
walls= get_dungeon(k, k)
for i in range(k):
for j in range(k):
if (i,j) in walls:
print '.',
else:
print ' ',
print
# print deep
#example output
'''
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .
. .
. .
. .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . .
. . . .
. . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .
.
. .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. .
. . . . . . . .
. . . .
. . . . . . . . . . . . . .
. . . . . . . . .
. . . .
. . . .
. . . . . . . . . . . . . .
. . . . . .
. . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . .
. .
. . . . . . . . . .
. . . .
. . . . . . . . . . . . . . .
. . . . . . .
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment