2
\$\begingroup\$

A version of CGOL made in Python using the turtle module. Any suggestions for optimisations or improvement:

import time
import turtle
import threading
wn = turtle.Screen()
t = turtle.Turtle()
t.penup()
t.speed(0)
t.ht()
mode = int(input("What mode do you want to use to get the initial state? (1 - from a .cgol file,2 - random)"))
speed = float(input("How long is the delay between generations (seconds)"))
CellList = []
#length = 0
if mode == 1:
    file = open(input('Please input the .cgol file name: ')+'.cgol','r')
    code = file.readlines()
    for i in code:
        templist = list(i)
        while '\n' in templist:
            templist.remove('\n')
        templist2 = []
        for u in templist:
            templist2.append(int(u))
        CellList.append(templist2)
if mode == 2:
    import random
    length = int(input("How many cells across is the grid?"))
    for i in range(0,length):
        ShortList = []
        for u in range(0,length):
            ShortList.append(int(random.randrange(0,2)))
        CellList.append(ShortList)

Display = []
FriendList = []
for i in range(0, len(CellList)):
    FriendList.append([])
while True:
    wn.tracer(0,0)
    Display = []
    for y in range(0,len(CellList)):
        for x in range(0,len(CellList)):
            ymin = y - 1
            xmin = x - 1
            yplus = y + 1
            xplus = x + 1
            if yplus > len(CellList)-1:
                yplus = 0
            if ymin < 0:
                ymin = len(CellList)-1
            if xplus > len(CellList)-1:
                xplus = 0
            if xmin < 0:
                xmin = len(CellList)-1
            FriendList[y].append(CellList[xmin][ymin] + CellList[xmin][yplus] + CellList[xmin][y] + CellList[xplus][ymin] + CellList[xplus][yplus] + CellList[xplus][y] + CellList[x][ymin] + CellList[x][yplus])
    for x in range(0,len(CellList)):
        Display.append("\n")
        for y in range(0,len(CellList)):
            if CellList[x][y] == 1:
                if FriendList[y][x] > 3:
                    CellList[x][y] = 0
                if FriendList[y][x] < 2:
                    CellList[x][y] = 0
            else:
                if FriendList[y][x] == 3:
                    CellList[x][y] = 1
            Display.append(str(CellList[x][y]))
    for i in range(0,(len(CellList)**2)+len(CellList)):
        if Display[i] == '1':
            Display[i] = '||'
        if Display[i] == '0':
            Display[i] = '  '
    for y in range(0,len(CellList)):
        for x in range(0,len(CellList)):
            if CellList[y][x] == 1:
                t.goto(x*10,-y*10)
                t.dot(10)
    FriendList = []
    for i in range(0, len(CellList)):
        FriendList.append([])
    print("".join(Display))
    Display = []
    wn.update()
    t.clear()
    time.sleep(speed)

.cgol files are txt's with a custom extension. they should be made in the format of a grid with 1 being the cell is on and 0 if the cell is dead. eg

00000000000000000000
00000000000000000000
00000001000000000000
00000000000000000000
00000000001000000000
00001000000000100000
00000000000000000000
00000000010000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000100000
00000100000000000000
00000000010000001000
00000000000000000000
00000000000001000000
00000001000000000000
00000000000000000000
\$\endgroup\$
0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.