Created
October 31, 2012 01:51
-
-
Save MattRoelle/3984340 to your computer and use it in GitHub Desktop.
first draft of algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
MAP_HEIGHT = 24 | |
MAP_WIDTH = 80 | |
CELL_WIDTH = 3 | |
CELL_HEIGHT = 3 | |
def initMap(Map): | |
for x in xrange(MAP_WIDTH): | |
for y in xrange(MAP_HEIGHT): | |
Map[(x,y)] = "#" | |
return Map | |
def printMap(Map): | |
for y in xrange(MAP_HEIGHT): | |
for x in xrange(MAP_WIDTH): | |
print Map[(x,y)] | |
print '\n' | |
class Cell: | |
#Represents a room | |
#grows, evolves, and mutates | |
def __init__(self,x,y): | |
self.x = x | |
self.y = y | |
self.members = { (x,y) : '.' } | |
def live(self): | |
rand = random.randrange(0,1000) | |
if rand < 2: | |
self.mutate() | |
elif rand < 500: | |
self.grow() | |
else: | |
pass | |
def grow(self): | |
dx = random.randrange(-1*CELL_WIDTH,CELL_WIDTH) | |
dy = random.randrange(-1*CELL_HEIGHT,CELL_HEIGHT) | |
try: | |
if Map[(self.x+dx,self.y+dy)] == ".": | |
dx2 = random.choice([-1,0,1]) | |
dy2 = random.choice([-1,0,1]) | |
Map[(self.x+dx+dx2,self.y+dy+dy2)] = "." | |
except: | |
pass | |
def mutate(self): | |
pass | |
def carve(self,Matrix): | |
for x in xrange(self.x-CELL_WIDTH,self.x+CELL_WIDTH): | |
for y in xrange(self.y-CELL_HEIGHT,self.y+CELL_HEIGHT): | |
try: | |
Matrix[(x,y)] = self.members[(x,y)] | |
except: | |
pass | |
return Matrix | |
def initCells(cells): | |
cells.append(Cell(10,5)) | |
cells.append(Cell(30,10)) | |
cells.append(Cell(50,12)) | |
return cells | |
def manage(cells,lifetimes,i): | |
if i >= lifetimes: | |
return cells | |
for index in cells: | |
index.live() | |
i += 1 | |
manage(cells,lifetimes,i) | |
def carveCells(cells, Map): | |
for index in cells: | |
Map = index.carve(Map) | |
return Map | |
def main(): | |
cells = [] | |
Map = {} | |
Map = initMap(Map) | |
cells = initCells(cells) | |
print cells | |
cells = manage(cells,15,0) | |
print cells | |
Map = carveCells(cells, Map) | |
printMap(Map) | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment