Cellular automata and image manipulation

defcon8 defcon8 at gmail.com
Sun May 14 04:44:58 EDT 2006


import Image
x = []
buff = []

buff = [[0 for y in range(41)] for x in range(21)]
buff[0][(len(buff)-1)/2] = 1
def rule1():
    for i in range(len(buff)-1):
	for j in range(len(buff[0])-1):
	    if i == len(buff)-1:
		break
	    elif j == 0:
		if buff[i][j+1] == 1:
		    buff[i+1][j] = 1
	    elif j == len(buff[0])-1:
		if buff[i][j-1] == 1:
		    buff[i+1][j] = 1
	    elif buff[i][j-1] == 1:
		buff[i+1][j] = 1
	    elif buff[i][j+1] == 1:
		buff[i+1][j] = 1

def rule2():
    for i in range(len(buff)-1):
	for j in range(len(buff[0])-1):
	    if i == len(buff)-1:
		break
	    elif j == 0:
		if buff[i][j+1] == 1:
		    buff[i+1][j] = 1
	    elif j == len(buff[0])-1:
		buff[i+1][j] = 1
	    elif buff[i][j-1] == 1 and buff[i][j+1] != 1:
		buff[i+1][j] = 1
	    elif buff[i][j+1] == 1 and buff[i][j-1] != 1:
		buff[i+1][j] = 1


rule2()
nim = Image.new("1", (400,600))
nim.putdata(buff)
nim.resize((400,600)).save("output.png")

for a in buff:
    for x in a:
       if x == 1:
	    print "X",
       else: print " ",
    print ""

That is my code. Could you tell me what maybe is wrong? Rule2 makes the
fibonacci triangle btw.




More information about the Python-list mailing list