Code: Select all
import pygame
import random
color = False
fullcopy = False
colors = ["black", "gray", "white", "red", "green", "dodgerblue", "orange", "purple", "yellow", "magenta"]
world = []
cursorpos = [100, 100]
selection = [[100, 100], [100, 100]]
selectphase = 0
buffer = ""
rule = input("Rule: ")
def rparse(rule):
# Complete isotropic tables (made by hand) for rule identification.
x0 = ["00000000"]
x8 = ["11111111"]
c1 = ["01000000","00010000","00000100","00000001"]
c2 = ["01010000","00010100","00000101","01000001"]
c3 = ["01010100","00010101","01000101","01010001"]
c4 = ["01010101"]
c5 = ["10101110","10111010","11101010","10101011"]
c6 = ["10111110","11111010","11101011","10101111"]
c7 = ["10111111","11101111","11111011","11111110"]
e1 = ["10000000","00100000","00001000","00000010"]
e2 = ["10100000","00101000","00001010","10000010"]
e3 = ["10101000","00101010","10001010","10100010"]
e4 = ["10101010"]
e5 = ["01011101","01110101","11010101","01010111"]
e6 = ["01011111","01111101","11110101","11010111"]
e7 = ["01111111","11111101","11110111","11011111"]
k2 = ["10010000","10000100","00100100","00010010","00001001","01001000","01000010","00100001"]
k3 = ["10100100","10010010","01001010","00101001"]
k4 = ["10110100","10010110","11010010","10100101","01001011","01101001","00101101","01011010"]
k5 = ["10110101","11010110","01011011","01101101"]
k6 = ["10111101","11011110","11110110","10110111","11011011","11101101","01101111","01111011"]
a2 = ["11000000","01100000","00110000","00011000","00001100","00000110","00000011","10000001"]
a3 = ["11100000","00111000","00001110","10000011"]
a4 = ["11110000","01111000","00111100","00011110","00001111","10000111","11000011","11100001"]
a5 = ["01111100","00111110","10001111","11100011"]
a6 = ["11111100","01111110","00111111","10011111","11001111","11100111","11110011","11111001"]
i2 = ["10001000","00100010"]
i3 = ["11000001","01110000","00011100","00000111"]
i4 = ["11011000","00110110","10001101","01100011"]
i5 = ["11111000","00111110","10001111","11100011"]
i6 = ["01110111","11011101"]
n2 = ["01000100","00010001"]
n3 = ["11010000","10000101","00110100","00010110","00001101","01011000","01000011","01100001"]
n4 = ["01110100","00010111","11010001","11000101","01000111","01110001","00011101","01011100"]
n5 = ["10111100","10011110","11110010","10100111","11001011","11101001","00101111","01111010"]
n6 = ["11101110","10111011"]
y3 = ["10010100","00100101","01001001","01010010"]
y4 = ["11010100","10010101","01010011","01100101","01001101","01011001","00110101","01010110"]
y5 = ["01101011","10101101","10110110","11011010"]
q3 = ["11000100","10010001","00010011","01100100","01001100","00011001","00110001","01000110"]
q4 = ["11100100","10010011","01001110","00111001"]
q5 = ["11101100","10011011","10110011","11100110","11001110","10111001","00111011","01101110"]
j3 = ["10110000","10000110","11000010","10100001","00001011","01101000","00101100","00011010"]
j4 = ["10101100","10011010","10110010","10100110","11001010","10101001","00101011","01101010"]
j5 = ["11110100","10010111","11010011","11100101","01001111","01111001","11100101","11010011"]
r3 = ["11001000","10001001","00100011","01100010","10001100","10011000","00110010","00100110"]
r4 = ["11101000","10001011","10100011","11100010","10001110","10111000","00111010","00101110"]
r5 = ["11011100","10011101","01110011","01100111","11001101","11011001","00110111","01110110"]
t4 = ["10011100","01110010","11001001","00100111"]
w4 = ["11011000","01100011","10001101","00110110"]
z4 = ["11001100","10011001","00110011","01100110"]
numstr = "012345678/"
if rule == "":
rule = "B3/S23"
rulelist = list(rule)
blist = rulelist[1:rulelist.index("S")]
slist = rulelist[rulelist.index("S") + 1:]
bconds = []
sconds = []
if "0" in blist:
for c in x0:
bconds.append(c)
if "1" in blist:
if blist[blist.index("1") + 1] in numstr:
for c in c1:
bconds.append(c)
for c in e1:
bconds.append(c)
for n in range(blist.index("1") + 1,len(blist)):
if blist[n] in numstr:
blist1 = blist[blist.index("1"):n]
break
if "-" in blist1:
B1Negating = True
else:
B1Negating = False
if B1Negating == True:
if "c" not in blist1:
for c in c1:
bconds.append(c)
if "e" not in blist1:
for c in e1:
bconds.append(c)
else:
if "c" in blist1:
for c in c1:
bconds.append(c)
if "e" in blist1:
for c in e1:
bconds.append(c)
if "2" in blist:
if blist[blist.index("2") + 1] in numstr:
for c in c2:
bconds.append(c)
for c in e2:
bconds.append(c)
for c in k2:
bconds.append(c)
for c in a2:
bconds.append(c)
for c in i2:
bconds.append(c)
for c in n2:
bconds.append(c)
for n in range(blist.index("2") + 1,len(blist)):
if blist[n] in numstr:
blist2 = blist[blist.index("2"):n]
break
if "-" in blist2:
B2Negating = True
else:
B2Negating = False
if B2Negating == True:
if "c" not in blist2:
for c in c2:
bconds.append(c)
if "e" not in blist2:
for c in e2:
bconds.append(c)
if "k" not in blist2:
for c in k2:
bconds.append(c)
if "a" not in blist2:
for c in a2:
bconds.append(c)
if "i" not in blist2:
for c in i2:
bconds.append(c)
if "n" not in blist2:
for c in n2:
bconds.append(c)
else:
if "c" in blist2:
for c in c2:
bconds.append(c)
if "e" in blist2:
for c in e2:
bconds.append(c)
if "k" in blist2:
for c in k2:
bconds.append(c)
if "a" in blist2:
for c in a2:
bconds.append(c)
if "i" in blist2:
for c in i2:
bconds.append(c)
if "n" in blist2:
for c in n2:
bconds.append(c)
if "3" in blist:
if blist[blist.index("3") + 1] in numstr:
for c in c3:
bconds.append(c)
for c in e3:
bconds.append(c)
for c in k3:
bconds.append(c)
for c in a3:
bconds.append(c)
for c in i3:
bconds.append(c)
for c in n3:
bconds.append(c)
for c in y3:
bconds.append(c)
for c in q3:
bconds.append(c)
for c in j3:
bconds.append(c)
for c in r3:
bconds.append(c)
for n in range(blist.index("3") + 1,len(blist)):
if blist[n] in numstr:
blist3 = blist[blist.index("3"):n]
break
if "-" in blist3:
B3Negating = True
else:
B3Negating = False
if B3Negating == True:
if "c" not in blist3:
for c in c3:
bconds.append(c)
if "e" not in blist3:
for c in e3:
bconds.append(c)
if "k" not in blist3:
for c in k3:
bconds.append(c)
if "a" not in blist3:
for c in a3:
bconds.append(c)
if "i" not in blist3:
for c in i3:
bconds.append(c)
if "n" not in blist3:
for c in n3:
bconds.append(c)
if "y" not in blist3:
for c in y3:
bconds.append(c)
if "q" not in blist3:
for c in q3:
bconds.append(c)
if "j" not in blist3:
for c in j3:
bconds.append(c)
if "r" not in blist3:
for c in r3:
bconds.append(c)
else:
if "c" in blist3:
for c in c3:
bconds.append(c)
if "e" in blist3:
for c in e3:
bconds.append(c)
if "k" in blist3:
for c in k3:
bconds.append(c)
if "a" in blist3:
for c in a3:
bconds.append(c)
if "i" in blist3:
for c in i3:
bconds.append(c)
if "n" in blist3:
for c in n3:
bconds.append(c)
if "y" in blist3:
for c in y3:
bconds.append(c)
if "q" in blist3:
for c in q3:
bconds.append(c)
if "j" in blist3:
for c in j3:
bconds.append(c)
if "r" in blist3:
for c in r3:
bconds.append(c)
if "4" in blist:
if blist[blist.index("4") + 1] in numstr:
for c in c4:
bconds.append(c)
for c in e4:
bconds.append(c)
for c in k4:
bconds.append(c)
for c in a4:
bconds.append(c)
for c in i4:
bconds.append(c)
for c in n4:
bconds.append(c)
for c in y4:
bconds.append(c)
for c in q4:
bconds.append(c)
for c in j4:
bconds.append(c)
for c in r4:
bconds.append(c)
for c in t4:
bconds.append(c)
for c in w4:
bconds.append(c)
for c in z4:
bconds.append(c)
for n in range(blist.index("4") + 1,len(blist)):
if blist[n] in numstr:
blist4 = blist[blist.index("4"):n]
break
if "-" in blist4:
B4Negating = True
else:
B4Negating = False
if B4Negating == True:
if "c" not in blist4:
for c in c4:
bconds.append(c)
if "e" not in blist4:
for c in e4:
bconds.append(c)
if "k" not in blist4:
for c in k4:
bconds.append(c)
if "a" not in blist4:
for c in a4:
bconds.append(c)
if "i" not in blist4:
for c in i4:
bconds.append(c)
if "n" not in blist4:
for c in n4:
bconds.append(c)
if "y" not in blist4:
for c in y4:
bconds.append(c)
if "q" not in blist4:
for c in q4:
bconds.append(c)
if "j" not in blist4:
for c in j4:
bconds.append(c)
if "r" not in blist4:
for c in r4:
bconds.append(c)
if "t" not in blist4:
for c in t4:
bconds.append(c)
if "w" not in blist4:
for c in w4:
bconds.append(c)
if "z" not in blist4:
for c in z4:
bconds.append(c)
else:
if "c" in blist4:
for c in c4:
bconds.append(c)
if "e" in blist4:
for c in e4:
bconds.append(c)
if "k" in blist4:
for c in k4:
bconds.append(c)
if "a" in blist4:
for c in a4:
bconds.append(c)
if "i" in blist4:
for c in i4:
bconds.append(c)
if "n" in blist4:
for c in n4:
bconds.append(c)
if "y" in blist4:
for c in y4:
bconds.append(c)
if "q" in blist4:
for c in q4:
bconds.append(c)
if "j" in blist4:
for c in j4:
bconds.append(c)
if "r" in blist4:
for c in r4:
bconds.append(c)
if "t" in blist4:
for c in t4:
bconds.append(c)
if "w" in blist4:
for c in w4:
bconds.append(c)
if "z" in blist4:
for c in z4:
bconds.append(c)
if "5" in blist:
if blist[blist.index("5") + 1] in numstr:
for c in c5:
bconds.append(c)
for c in e5:
bconds.append(c)
for c in k5:
bconds.append(c)
for c in a5:
bconds.append(c)
for c in i5:
bconds.append(c)
for c in n5:
bconds.append(c)
for c in y5:
bconds.append(c)
for c in q5:
bconds.append(c)
for c in j5:
bconds.append(c)
for c in r5:
bconds.append(c)
for n in range(blist.index("5") + 1,len(blist)):
if blist[n] in numstr:
blist5 = blist[blist.index("5"):n]
break
if "-" in blist5:
B5Negating = True
else:
B5Negating = False
if B5Negating == True:
if "c" not in blist5:
for c in c5:
bconds.append(c)
if "e" not in blist5:
for c in e5:
bconds.append(c)
if "k" not in blist5:
for c in k5:
bconds.append(c)
if "a" not in blist5:
for c in a5:
bconds.append(c)
if "i" not in blist5:
for c in i5:
bconds.append(c)
if "n" not in blist5:
for c in n5:
bconds.append(c)
if "y" not in blist5:
for c in y5:
bconds.append(c)
if "q" not in blist5:
for c in q5:
bconds.append(c)
if "j" not in blist5:
for c in j5:
bconds.append(c)
if "r" not in blist5:
for c in r5:
bconds.append(c)
else:
if "c" in blist5:
for c in c5:
bconds.append(c)
if "e" in blist5:
for c in e5:
bconds.append(c)
if "k" in blist5:
for c in k5:
bconds.append(c)
if "a" in blist5:
for c in a5:
bconds.append(c)
if "i" in blist5:
for c in i5:
bconds.append(c)
if "n" in blist5:
for c in n5:
bconds.append(c)
if "y" in blist5:
for c in y5:
bconds.append(c)
if "q" in blist5:
for c in q5:
bconds.append(c)
if "j" in blist5:
for c in j5:
bconds.append(c)
if "r" in blist5:
for c in r5:
bconds.append(c)
if "6" in blist:
if blist[blist.index("6") + 1] in numstr:
for c in c6:
bconds.append(c)
for c in e6:
bconds.append(c)
for c in k6:
bconds.append(c)
for c in a6:
bconds.append(c)
for c in i6:
bconds.append(c)
for c in n6:
bconds.append(c)
for n in range(blist.index("6") + 1,len(blist)):
if blist[n] in numstr:
blist6 = blist[blist.index("6"):n]
break
if "-" in blist6:
B6Negating = True
else:
B6Negating = False
if B6Negating == True:
if "c" not in blist6:
for c in c6:
bconds.append(c)
if "e" not in blist6:
for c in e6:
bconds.append(c)
if "k" not in blist6:
for c in k6:
bconds.append(c)
if "a" not in blist6:
for c in a6:
bconds.append(c)
if "i" not in blist6:
for c in i6:
bconds.append(c)
if "n" not in blist6:
for c in n6:
bconds.append(c)
else:
if "c" in blist6:
for c in c6:
bconds.append(c)
if "e" in blist6:
for c in e6:
bconds.append(c)
if "k" in blist6:
for c in k6:
bconds.append(c)
if "a" in blist6:
for c in a6:
bconds.append(c)
if "i" in blist6:
for c in i6:
bconds.append(c)
if "n" in blist6:
for c in n6:
bconds.append(c)
if "7" in blist:
if blist[blist.index("7") + 1] in numstr:
for c in c7:
bconds.append(c)
for c in e7:
bconds.append(c)
for n in range(blist.index("7") + 1,len(blist)):
if blist[n] in numstr:
blist7 = blist[blist.index("7"):n]
break
if "-" in blist7:
B7Negating = True
else:
B7Negating = False
if B7Negating == True:
if "c" not in blist7:
for c in c7:
bconds.append(c)
if "e" not in blist7:
for c in e7:
bconds.append(c)
else:
if "c" in blist7:
for c in c7:
bconds.append(c)
if "e" in blist7:
for c in e7:
bconds.append(c)
if "8" in blist:
for c in x8:
bconds.append(c)
if "0" in slist:
for c in x0:
sconds.append(c)
if "1" in slist:
try:
if slist[slist.index("1") + 1] in numstr:
for c in c1:
sconds.append(c)
for c in e1:
sconds.append(c)
for n in range(slist.index("1") + 1,len(slist)):
if slist[n] in numstr:
slist1 = slist[slist.index("1"):n]
break
if n == len(slist) - 1:
slist1 = slist[slist.index("1"):n + 1]
break
except IndexError:
slist1 = ["1"]
for c in c1:
sconds.append(c)
for c in e1:
sconds.append(e)
if "-" in slist1:
S1Negating = True
else:
S1Negating = False
if S1Negating == True:
if "c" not in slist1:
for c in c1:
sconds.append(c)
if "e" not in slist1:
for c in e1:
sconds.append(c)
else:
if "c" in slist1:
for c in c1:
sconds.append(c)
if "e" in slist1:
for c in e1:
sconds.append(c)
if "2" in slist:
try:
if slist[slist.index("2") + 1] in numstr:
for c in c2:
sconds.append(c)
for c in e2:
sconds.append(c)
for c in k2:
sconds.append(c)
for c in a2:
sconds.append(c)
for c in i2:
sconds.append(c)
for c in n2:
sconds.append(c)
for n in range(slist.index("2") + 1,len(slist)):
if slist[n] in numstr:
slist2 = slist[slist.index("2"):n]
break
if n == len(slist) - 1:
slist2 = slist[slist.index("2"):n + 1]
break
except IndexError:
slist2 = ["2"]
for c in c2:
sconds.append(c)
for c in e2:
sconds.append(c)
for c in k2:
sconds.append(c)
for c in a2:
sconds.append(c)
for c in i2:
sconds.append(c)
for c in n2:
sconds.append(c)
if "-" in slist2:
S2Negating = True
else:
S2Negating = False
if S2Negating == True:
if "c" not in slist2:
for c in c2:
sconds.append(c)
if "e" not in slist2:
for c in e2:
sconds.append(c)
if "k" not in slist2:
for c in k2:
sconds.append(c)
if "a" not in slist2:
for c in a2:
sconds.append(c)
if "i" not in slist2:
for c in i2:
sconds.append(c)
if "n" not in slist2:
for c in n2:
sconds.append(c)
else:
if "c" in slist2:
for c in c2:
sconds.append(c)
if "e" in slist2:
for c in e2:
sconds.append(c)
if "k" in slist2:
for c in k2:
sconds.append(c)
if "a" in slist2:
for c in a2:
sconds.append(c)
if "i" in slist2:
for c in i2:
sconds.append(c)
if "n" in slist2:
for c in n2:
sconds.append(c)
if "3" in slist:
try:
if slist[slist.index("3") + 1] in numstr:
for c in c3:
sconds.append(c)
for c in e3:
sconds.append(c)
for c in k3:
sconds.append(c)
for c in a3:
sconds.append(c)
for c in i3:
sconds.append(c)
for c in n3:
sconds.append(c)
for c in y3:
sconds.append(c)
for c in q3:
sconds.append(c)
for c in j3:
sconds.append(c)
for c in r3:
sconds.append(c)
for n in range(slist.index("3") + 1,len(slist)):
if slist[n] in numstr:
slist3 = slist[slist.index("3"):n]
break
if n == len(slist) - 1:
slist3 = slist[slist.index("3"):n + 1]
break
except IndexError:
slist3 = ["3"]
for c in c3:
sconds.append(c)
for c in e3:
sconds.append(c)
for c in k3:
sconds.append(c)
for c in a3:
sconds.append(c)
for c in i3:
sconds.append(c)
for c in n3:
sconds.append(c)
for c in y3:
sconds.append(c)
for c in q3:
sconds.append(c)
for c in j3:
sconds.append(c)
for c in r3:
sconds.append(c)
if "-" in slist3:
S3Negating = True
else:
S3Negating = False
if S3Negating == True:
if "c" not in slist3:
for c in c3:
sconds.append(c)
if "e" not in slist3:
for c in e3:
sconds.append(c)
if "k" not in slist3:
for c in k3:
sconds.append(c)
if "a" not in slist3:
for c in a3:
sconds.append(c)
if "i" not in slist3:
for c in i3:
sconds.append(c)
if "n" not in slist3:
for c in n3:
sconds.append(c)
if "y" not in slist3:
for c in y3:
sconds.append(c)
if "q" not in slist3:
for c in q3:
sconds.append(c)
if "j" not in slist3:
for c in j3:
sconds.append(c)
if "r" not in slist3:
for c in r3:
sconds.append(c)
else:
if "c" in slist3:
for c in c3:
sconds.append(c)
if "e" in slist3:
for c in e3:
sconds.append(c)
if "k" in slist3:
for c in k3:
sconds.append(c)
if "a" in slist3:
for c in a3:
sconds.append(c)
if "i" in slist3:
for c in i3:
sconds.append(c)
if "n" in slist3:
for c in n3:
sconds.append(c)
if "y" in slist3:
for c in y3:
sconds.append(c)
if "q" in slist3:
for c in q3:
sconds.append(c)
if "j" in slist3:
for c in j3:
sconds.append(c)
if "r" in slist3:
for c in r3:
sconds.append(c)
if "4" in slist:
try:
if slist[slist.index("4") + 1] in numstr:
for c in c4:
sconds.append(c)
for c in e4:
sconds.append(c)
for c in k4:
sconds.append(c)
for c in a4:
sconds.append(c)
for c in i4:
sconds.append(c)
for c in n4:
sconds.append(c)
for c in y4:
sconds.append(c)
for c in q4:
sconds.append(c)
for c in j4:
sconds.append(c)
for c in r4:
sconds.append(c)
for c in t4:
sconds.append(c)
for c in w4:
sconds.append(c)
for c in z4:
sconds.append(c)
for n in range(slist.index("4") + 1,len(slist)):
if slist[n] in numstr:
slist4 = slist[slist.index("4"):n]
break
if n == len(slist) - 1:
slist4 = slist[slist.index("4"):n + 1]
break
except IndexError:
slist4 = ["4"]
for c in c4:
sconds.append(c)
for c in e4:
sconds.append(c)
for c in k4:
sconds.append(c)
for c in a4:
sconds.append(c)
for c in i4:
sconds.append(c)
for c in n4:
sconds.append(c)
for c in y4:
sconds.append(c)
for c in q4:
sconds.append(c)
for c in j4:
sconds.append(c)
for c in r4:
sconds.append(c)
for c in t4:
sconds.append(c)
for c in w4:
sconds.append(c)
for c in z4:
sconds.append(c)
if "-" in slist4:
S4Negating = True
else:
S4Negating = False
if S4Negating == True:
if "c" not in slist4:
for c in c4:
sconds.append(c)
if "e" not in slist4:
for c in e4:
sconds.append(c)
if "k" not in slist4:
for c in k4:
sconds.append(c)
if "a" not in slist4:
for c in a4:
sconds.append(c)
if "i" not in slist4:
for c in i4:
sconds.append(c)
if "n" not in slist4:
for c in n4:
sconds.append(c)
if "y" not in slist4:
for c in y4:
sconds.append(c)
if "q" not in slist4:
for c in q4:
sconds.append(c)
if "j" not in slist4:
for c in j4:
sconds.append(c)
if "r" not in slist4:
for c in r4:
sconds.append(c)
if "t" not in slist4:
for c in t4:
sconds.append(c)
if "w" not in slist4:
for c in w4:
sconds.append(c)
if "z" not in slist4:
for c in z4:
sconds.append(c)
else:
if "c" in slist4:
for c in c4:
sconds.append(c)
if "e" in slist4:
for c in e4:
sconds.append(c)
if "k" in slist4:
for c in k4:
sconds.append(c)
if "a" in slist4:
for c in a4:
sconds.append(c)
if "i" in slist4:
for c in i4:
sconds.append(c)
if "n" in slist4:
for c in n4:
sconds.append(c)
if "y" in slist4:
for c in y4:
sconds.append(c)
if "q" in slist4:
for c in q4:
sconds.append(c)
if "j" in slist4:
for c in j4:
sconds.append(c)
if "r" in slist4:
for c in r4:
sconds.append(c)
if "t" in slist4:
for c in t4:
sconds.append(c)
if "w" in slist4:
for c in w4:
sconds.append(c)
if "z" in slist4:
for c in z4:
sconds.append(c)
if "5" in slist:
try:
if slist[slist.index("5") + 1] in numstr:
for c in c5:
sconds.append(c)
for c in e5:
sconds.append(c)
for c in k5:
sconds.append(c)
for c in a5:
sconds.append(c)
for c in i5:
sconds.append(c)
for c in n5:
sconds.append(c)
for c in y5:
sconds.append(c)
for c in q5:
sconds.append(c)
for c in j5:
sconds.append(c)
for c in r5:
sconds.append(c)
for n in range(slist.index("5") + 1,len(slist)):
if slist[n] in numstr:
slist5 = slist[slist.index("5"):n]
break
if n == len(slist) - 1:
slist5 = slist[slist.index("5"):n + 1]
break
except IndexError:
slist5 = ["5"]
for c in c5:
sconds.append(c)
for c in e5:
sconds.append(c)
for c in k5:
sconds.append(c)
for c in a5:
sconds.append(c)
for c in i5:
sconds.append(c)
for c in n5:
sconds.append(c)
for c in y5:
sconds.append(c)
for c in q5:
sconds.append(c)
for c in j5:
sconds.append(c)
for c in r5:
sconds.append(c)
if "-" in slist5:
S5Negating = True
else:
S5Negating = False
if S5Negating == True:
if "c" not in slist5:
for c in c5:
sconds.append(c)
if "e" not in slist5:
for c in e5:
sconds.append(c)
if "k" not in slist5:
for c in k5:
sconds.append(c)
if "a" not in slist5:
for c in a5:
sconds.append(c)
if "i" not in slist5:
for c in i5:
sconds.append(c)
if "n" not in slist5:
for c in n5:
sconds.append(c)
if "y" not in slist5:
for c in y5:
sconds.append(c)
if "q" not in slist5:
for c in q5:
sconds.append(c)
if "j" not in slist5:
for c in j5:
sconds.append(c)
if "r" not in slist5:
for c in r5:
sconds.append(c)
else:
if "c" in slist5:
for c in c5:
sconds.append(c)
if "e" in slist5:
for c in e5:
sconds.append(c)
if "k" in slist5:
for c in k5:
sconds.append(c)
if "a" in slist5:
for c in a5:
sconds.append(c)
if "i" in slist5:
for c in i5:
sconds.append(c)
if "n" in slist5:
for c in n5:
sconds.append(c)
if "y" in slist5:
for c in y5:
sconds.append(c)
if "q" in slist5:
for c in q5:
sconds.append(c)
if "j" in slist5:
for c in j5:
sconds.append(c)
if "r" in slist5:
for c in r5:
sconds.append(c)
if "6" in slist:
try:
if slist[slist.index("6") + 1] in numstr:
for c in c6:
sconds.append(c)
for c in e6:
sconds.append(c)
for c in k6:
sconds.append(c)
for c in a6:
sconds.append(c)
for c in i6:
sconds.append(c)
for c in n6:
sconds.append(c)
for n in range(slist.index("6") + 1,len(slist)):
if slist[n] in numstr:
slist6 = slist[slist.index("6"):n]
break
if n == len(slist) - 1:
slist6 = slist[slist.index("6"):n + 1]
break
except IndexError:
slist6 = ["6"]
for c in c6:
sconds.append(c)
for c in e6:
sconds.append(c)
for c in k6:
sconds.append(c)
for c in a6:
sconds.append(c)
for c in i6:
sconds.append(c)
for c in n6:
sconds.append(c)
if "-" in slist6:
S6Negating = True
else:
S6Negating = False
if S6Negating == True:
if "c" not in slist6:
for c in c6:
sconds.append(c)
if "e" not in slist6:
for c in e6:
sconds.append(c)
if "k" not in slist6:
for c in k6:
sconds.append(c)
if "a" not in slist6:
for c in a6:
sconds.append(c)
if "i" not in slist6:
for c in i6:
sconds.append(c)
if "n" not in slist6:
for c in n6:
sconds.append(c)
else:
if "c" in slist6:
for c in c6:
sconds.append(c)
if "e" in slist6:
for c in e6:
sconds.append(c)
if "k" in slist6:
for c in k6:
sconds.append(c)
if "a" in slist6:
for c in a6:
sconds.append(c)
if "i" in slist6:
for c in i6:
sconds.append(c)
if "n" in slist6:
for c in n6:
sconds.append(c)
if "7" in slist:
try:
if slist[slist.index("7") + 1] in numstr:
for c in c7:
sconds.append(c)
for c in e7:
sconds.append(c)
for n in range(slist.index("7") + 1,len(slist)):
if slist[n] in numstr:
slist7 = slist[slist.index("7"):n]
break
if n == len(slist) - 1:
slist7 = slist[slist.index("7"):n + 1]
break
except IndexError:
slist7 = ["7"]
for c in c7:
sconds.append(c)
for c in e7:
sconds.append(c)
if "-" in slist7:
S7Negating = True
else:
S7Negating = False
if S7Negating == True:
if "c" not in slist7:
for c in c7:
sconds.append(c)
if "e" not in slist7:
for c in e7:
sconds.append(c)
else:
if "c" in slist7:
for c in c7:
sconds.append(c)
if "e" in slist7:
for c in e7:
sconds.append(c)
if "8" in slist:
for c in x8:
sconds.append(c)
l = [bconds,sconds]
return(l)
conditions = rparse(rule)
for y in range(0, 200):
world.append([0] * 200)
def makeworld(rle, pos): # Assists in the conversion of an RLE to a matrix
global world
rlel = list(rle)
digits = "0123456789"
y = pos[0] if pos[0] != -1 else 0
x = pos[1] if pos[1] != -1 else 0
num = 0
for char in rlel:
if char in digits:
num = num * 10 + int(char)
else:
if char == "b":
if num == 0:
num2 = 1
else:
num2 = num
for n in range(0, num2):
world[y][x] = 0
x += 1
if char == "o":
if num == 0:
num2 = 1
else:
num2 = num
for n in range(0, num2):
world[y][x] = 1
x += 1
if char == "$":
x = pos[1]
if num == 0:
num2 = 1
else:
num2 = num
for n in range(0, num2):
y += 1
num = 0
return world
def makerle(mat, selection): # Does the opposite of the previous function
rlel = []
num = 1
if selection[0] == selection[1]:
xstart = 0
xend = 200
ystart = 0
yend = 200
else:
xstart = selection[0][1]
xend = selection[1][1]
ystart = selection[0][0]
yend = selection[1][0]
xdir = 1 if xstart < xend else -1
ydir = 1 if ystart < yend else -1
for y in range(ystart, yend, ydir):
for x in range(xstart, xend, ydir):
cell = mat[y][x]
if cell == 0:
rlel.append("b")
else:
rlel.append("o")
rlel.append("$")
rlel[len(rlel) - 1] = "!"
listinit = rlel
rle = "".join(listinit)
return(rle)
rle = input("RLE (must be headerless, may be empty): ")
world = makeworld(rle, [0, 0])
pygame.init()
screen = pygame.display.set_mode((1000, 1000))
rectworld = []
for y in range(0, 200):
rectworld.append([])
for x in range(0, 200):
rectworld[-1].append(pygame.Rect(x * 5, y * 5, 5, 5))
clock = pygame.time.Clock()
step = 0
running = True
dt = 0
def advance(world):
global color
global conditions
# neigh = [[1, 1], [1, 0], [1, -1], [0, 1], [0, -1], [-1, 1], [-1, 0], [-1, -1]]
neigh = [[1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1], [0, 1], [1, 1]]
for y in range(0, 200):
for x in range(0, 200):
neighs = 0
neighlist = []
for n in range(0, 8):
newx = x + neigh[n][1]
newy = y + neigh[n][0]
if newx == 200:
newx = 199
if newx == -1:
newx = 0
if newy == 200:
newy = 199
if newy == -1:
newy = 0
if (world[newy][newx] != 0):
neighlist.append("1")
neighs += 1
else:
neighlist.append("0")
neighstr = "".join(neighlist)
if world[y][x] == 0 and (neighstr in conditions[0]):
if not color:
world[y][x] = 1
else:
world[y][x] = neighs + 1
continue
if world[y][x] != 0 and (neighstr not in conditions[1]):
world[y][x] = 0
continue
if world[y][x] != 0 and (neighstr in conditions[1]):
if color:
world[y][x] = neighs + 1
else:
if world[y][x] != 1:
world[y][x] = 1
return world
def show(world):
for y in range(0, 200):
for x in range(0, 200):
if world[y][x] != 0:
if not color:
pygame.draw.rect(screen, "white", rectworld[y][x])
else:
pygame.draw.rect(screen, colors[world[y][x]], rectworld[y][x])
if y == cursorpos[0] and x == cursorpos[1] and world[y][x] != 0:
if world[y][x] == 3:
pygame.draw.rect(screen, "blue", rectworld[y][x])
else:
pygame.draw.rect(screen, "red", rectworld[y][x])
if y == cursorpos[0] and x == cursorpos[1] and world[y][x] == 0:
pygame.draw.rect(screen, "maroon", rectworld[y][x])
if (selection[0][0] <= y < selection[1][0] and selection[0][1] <= x < selection[1][1]) or (selection[0][0] >= y > selection[1][0] and selection[0][1] >= x > selection[1][1]) or (selection[0][0] >= y > selection[1][0] and selection[0][1] <= x < selection[1][1]) or (selection[0][0] <= y < selection[1][0] and selection[0][1] >= x > selection[1][1]):
if world[y][x] != 0:
pygame.draw.rect(screen, (128, 192, 128), rectworld[y][x])
if world[y][x] == 0:
pygame.draw.rect(screen, (0, 128, 0), rectworld[y][x])
if y == cursorpos[0] and x == cursorpos[1] and world[y][x] == 0:
pygame.draw.rect(screen, (64, 64, 0), rectworld[y][x])
if y == cursorpos[0] and x == cursorpos[1] and world[y][x] != 0:
pygame.draw.rect(screen, (128, 64, 0), rectworld[y][x])
def randfill(selection):
global world
if selection[0] == selection[1]:
xstart = 0
xend = 200
ystart = 0
yend = 200
else:
xstart = selection[0][1]
xend = selection[1][1]
ystart = selection[0][0]
yend = selection[1][0]
xdir = 1 if xstart < xend else -1
ydir = 1 if ystart < yend else -1
for y in range(ystart, yend, ydir):
for x in range(xstart, xend, xdir):
world[y][x] = random.randint(0, 1)
return world
def zerofill(selection):
global world
if selection[0] == selection[1]:
xstart = 0
xend = 200
ystart = 0
yend = 200
else:
xstart = selection[0][1]
xend = selection[1][1]
ystart = selection[0][0]
yend = selection[1][0]
xdir = 1 if xstart < xend else -1
ydir = 1 if ystart < yend else -1
for y in range(ystart, yend, ydir):
for x in range(xstart, xend, xdir):
world[y][x] = 0
return world
def invfill(selection):
global world
if selection[0] == selection[1]:
xstart = 0
xend = 200
ystart = 0
yend = 200
else:
xstart = selection[0][1]
xend = selection[1][1]
ystart = selection[0][0]
yend = selection[1][0]
xdir = 1 if xstart < xend else -1
ydir = 1 if ystart < yend else -1
for y in range(ystart, yend, ydir):
for x in range(xstart, xend, xdir):
world[y][x] = 0 if world[y][x] != 0 else 1
return world
keysheld = []
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
keysheld.append(event.key)
if event.key == pygame.K_k:
if color:
color = False
else:
color = True
if event.key == pygame.K_q:
running = False
if event.key == pygame.K_r:
world = randfill(selection)
if event.key == pygame.K_i:
world = invfill(selection)
if event.key == pygame.K_c:
print(makerle(world, selection))
buffer = makerle(world, selection)
if selection[0] == selection[1]:
fullcopy = True
else:
fullcopy = False
if event.key == pygame.K_x:
print(makerle(world, selection))
buffer = makerle(world, selection)
world = zerofill(selection)
if selection[0] == selection[1]:
fullcopy = True
else:
fullcopy = False
if event.key == pygame.K_v:
if fullcopy:
world = zerofill([[100, 100], [100, 100]])
world = makeworld(buffer, [-1, -1])
else:
world = makeworld(buffer, cursorpos)
if event.key == pygame.K_t:
rule = input("Rule: ")
conditions = rparse(rule)
if event.key == pygame.K_EQUALS:
world = advance(world)
if event.key == pygame.K_UP:
cursorpos[0] -= 1
cursorpos[0] %= 200
if selectphase == 1:
selection[1][0] -= 1
selection[1][0] %= 200
if event.key == pygame.K_DOWN:
cursorpos[0] += 1
cursorpos[0] %= 200
if selectphase == 1:
selection[1][0] += 1
selection[1][0] %= 200
if event.key == pygame.K_LEFT:
cursorpos[1] -= 1
cursorpos[1] %= 200
if selectphase == 1:
selection[1][1] -= 1
selection[1][1] %= 200
if event.key == pygame.K_RIGHT:
cursorpos[1] += 1
cursorpos[1] %= 200
if selectphase == 1:
selection[1][1] += 1
selection[1][1] %= 200
if event.key == pygame.K_RETURN:
world[cursorpos[0]][cursorpos[1]] += 1
world[cursorpos[0]][cursorpos[1]] %= 2
if event.key == pygame.K_LSHIFT or event.key == pygame.K_RSHIFT:
if selectphase == 0:
selection[0][0] = cursorpos[0]
selection[0][1] = cursorpos[1]
selection[1][0] = cursorpos[0]
selection[1][1] = cursorpos[1]
selectphase = 1
else:
selection[1][0] = cursorpos[0]
selection[1][1] = cursorpos[1]
selectphase = 0
if event.key == pygame.K_8:
selection = [[100, 100], [100, 100]]
selectphase = 0
if event.key == pygame.K_BACKSPACE:
world = zerofill(selection)
if (pygame.K_LSHIFT in keysheld or pygame.K_RSHIFT in keysheld) and pygame.K_s in keysheld:
path = input("Path to save pattern: ")
with open(path, "w") as file:
newrule = rule
if rule == "":
newrule = "B3/S23"
file.write(f"x = 0, y = 0, rule = {newrule}\n")
file.write(makerle(world, [[0, 0], [0, 0]]))
elif event.type == pygame.KEYUP:
keysheld.remove(event.key)
left, middle, right = pygame.mouse.get_pressed()
x, y = pygame.mouse.get_pos()
if left:
xx = x // 5
yy = y // 5
world[yy][xx] = 1
cursorpos = [yy, xx]
if right:
xx = x // 5
yy = y // 5
world[yy][xx] = 0
cursorpos = [yy, xx]
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
world = advance(world)
if keys[pygame.K_w]:
cursorpos[0] -= 1
cursorpos[0] %= 200
if selectphase == 1:
selection[1][0] -= 1
selection[1][0] %= 200
if keys[pygame.K_s]:
cursorpos[0] += 1
cursorpos[0] %= 200
if selectphase == 1:
selection[1][0] += 1
selection[1][0] %= 200
if keys[pygame.K_a]:
cursorpos[1] -= 1
cursorpos[1] %= 200
if selectphase == 1:
selection[1][1] -= 1
selection[1][1] %= 200
if keys[pygame.K_d]:
cursorpos[1] += 1
cursorpos[1] %= 200
if selectphase == 1:
selection[1][1] += 1
selection[1][1] %= 200
screen.fill("black")
show(world)
pygame.display.flip()
dt = clock.tick(60) / 1000
pygame.quit()
Once again, I encourage everyone to optimize my code, especially the rule parsing algorithm. Post all optimized versions of the code here.