The 2D results are in. I made a lump generator.
Next step, 3D! ... but that's going to have to saturday, when I get my room & computer back.
Overall, it was difficult to eliminate the inherent stringy-ness of the generation system. I was focused on actually making it work, so it's uncommented and a little messy, not in proper class format at all.# -*- coding: utf-8 -*-
import random
import copy
#This is a test program which generates a meteorite using cellular automata.
grid = []
def display(grid):
display = ""
tileA = 0
tileB = 0
for a in range(0,len(grid)):
for b in range(0,len(grid[a])):
display += grid[a][b]
if grid[a][b]==grid[0][0]:
tileA += 1
else:
tileB += 1
display += ("\n")
print display,
print grid[0][0] + ": " + str(tileA) + ", !" + grid[0][0] + ": " + str(tileB),
def generate(grid):
for a in range(0,50):
grid.append([])
for b in range(0,50): #Fls Tru
grid[a].append(random.choice(["⚪","⚫"]))
generate(grid)
def inBounds(grid, x, y):
return (x < len(grid) and x >= 0 and y < len(grid[x]) and y >= 0)
def neighbours(grid, x, y):
if inBounds(grid, x, y):
count = 0
for a in range(-1,1):
for b in range(-1,1):
if inBounds(grid, x+a, y+b) and grid[x+a][y+b] == grid[x][y]:
count += 1
return count
return -1
def make(grid):
newGrid=copy.deepcopy(grid)
for a in range(0,len(grid)):
for b in range(0,len(grid[a])):
if 1 <= neighbours(newGrid, a, b) <= 9 and newGrid[a][b] == "⚫":
grid[a][b]="⚪"
#print "changed -"
if 2 <= neighbours(newGrid, a, b) <= 9 and newGrid[a][b] == "⚪":
grid[a][b]="⚫"
#print "changed +"
def mark(grid, x, y, match):
for a in [(-1,0),(1,0),(0,-1),(0,1)]:
a,b = a
if inBounds(grid, a+x, b+y) and grid[a+x][b+y] == match:
#print "marked y"
grid[a+x][b+y] = "Y"
grid = mark(grid, a+x, b+y, match)
elif inBounds(grid, a+x, b+y) and grid[a+x][b+y] != "Y":
#print "marked n"
grid[a+x][b+y] = "N"
#display(grid)
return grid
def edgeNeighbour(grid, x, y, match):
for a in [(-1,0),(1,0),(0,-1),(0,1)]:
a,b = a
if grid[x+a][y+b] == match:
return True
return False
def fill(grid, match):
newGrid = copy.deepcopy(grid)
newGrid[0][0] = "N"
newGrid[0][len(grid[0])-1] = "N"
newGrid[len(grid)-1][0] = "N"
newGrid[len(grid)-1][len(grid[0])-1] = "N"
change = True
while change != False:
change = False
for a in range(0,len(grid)):
for b in range(0,len(grid[a])):
if edgeNeighbour(newGrid, a, b, "N") and newGrid[a][b] == "⚪":
newGrid[a][b] = "N"
change = True
for a in range(0,len(grid)-1):
for b in range(0,len(grid[a])-1):
if newGrid[a][b] != "N":
grid[a][b] = "⚫"
return grid
def puff(grid, match):
newGrid = copy.deepcopy(grid)
for a in range(0,len(grid)-1):
for b in range(0,len(grid[a])-1):
if edgeNeighbour(newGrid, a, b, "⚫"):
grid[a][b] = "⚫"
return grid
def getGroup(grid, x, y):
newGrid = copy.deepcopy(grid)
for a in range(0,len(newGrid)):
for b in range(0,len(newGrid[a])):
newGrid[a][b]
newGrid = mark(newGrid, x, y, grid[x][y])
for a in range(0,len(grid)):
for b in range(0,len(grid[a])):
if newGrid[a][b] != "Y":
grid[a][b] = "⚪"
else:
grid[a][b] = "⚫"
grid = puff(grid, "⚫")
return fill(grid, "⚫")
def getCount(grid, match):
matches = 0
for a in range(0,len(grid)):
for b in range(0,len(grid[a])):
if grid[a][b]==match:
matches += 1
return matches
def getEdgeCount(grid, match):
matches = 0
for a in range(0,len(grid)):
for b in [0,len(grid[a])-1]:
if grid[a][b]==match:
matches += 1
for a in [0,len(grid)-1]:
for b in range(0,len(grid[a])):
if grid[a][b]==match:
matches += 1
return matches
def prune(grid):
#len(grid)/2, len(grid[0])/2
if grid[len(grid)/2][len(grid[0])/2] == "⚪":
print "rejected blank center"
return False
grid = getGroup(grid, len(grid)/2, len(grid[0])/2)
if getCount(grid, grid[len(grid)/2][len(grid[0])/2]) < 100:
print "rejected wrong sized asteroid"
return False
if getEdgeCount(grid, grid[len(grid)/2][len(grid[0])/2]) > 5:
print "rejected edged asteroid"
return False
return True
while prune(grid) != True:
grid = []
generate(grid)
make(grid)
display(grid)