I coded a CGoL simulator in python:
Code: Select all
import sys
trashvar = None
yn = None
continuevar = False
continueProgvar = True
cellnum = 0
nw = 0
n = 0
ne = 0
e = 0
se = 0
s = 0
sw = 0
w = 0
total = 0
#info
print('you are using this version of python:')
print(sys.version)
print()
print('''this program simulates Conway's Game of Life on a torus with shift 1.
It uses inputted coordinates to turn cells on and off.
It asks the user what it should do next.''')
print()
print('this program was coded by Moosey in 2019')
print('this program is version 1.0.1')
print('''version history:
1.0: first version. VERY buggy.
1.0.1: second version. assumes true when misinterpreting responses.''')
print('''bugs:
coordinate 0,0 does not turn on
entering y coordinate 10 appears to fail (but not x)
ADVANCING A GENERATION DOES NOT WORK!!!!''')
print()
print('press enter to start')
trashvar = sys.stdin.readline()
#define cells in 10-by-10 grid
cells = []
newcells = []
prettycells = []
selectedcell = None
selectedcellx = None
selectedcelly = None
for x in range(0, 100):
cells.append(0)
newcells.append(0)
prettycells.append('.')
#print(cells) -- does print all cellstates
#define functions
def maketotal():
total = nw + n + ne + e + se + s + sw + w
def defnesw():
if cellnum >= 10 and cellnum < 90:
nw = cells[cellnum-11]
n = cells[cellnum-10]
ne = cells[cellnum-9]
e = cells[cellnum+1]
se = cells[cellnum+11]
s = cells[cellnum+10]
sw = cells[cellnum+9]
w = cells[cellnum-1]
elif cellnum < 10:
nw= cells[cellnum+89]
n = cells[cellnum+90]
ne = cells[cellnum+91]
e = cells[cellnum+1]
se = cells[cellnum+11]
s = cells[cellnum+10]
sw = cells[cellnum+9]
w = cells[cellnum-1]
elif cellnum >= 90:
nw = cells[cellnum-11]
n = cells[cellnum-10]
ne = cells[cellnum-9]
e = cells[cellnum+1]
se = cells[cellnum-89]
s = cells[cellnum-90]
sw = cells[cellnum-91]
w = cells[cellnum-1]
else:
print('''var cellnum has an error. If you are reading this I coded wrong.
the value of cellnum is ''' + cellnum)
def putcells():
continuevar = True
while continuevar == True:
print('continue putting cells? 1=yes, 0=no')
yn = int(sys.stdin.readline())
if yn == 0:
continuevar = False
break
elif yn == 1:
continuevar = True
else:
print('did not understand')
print('assuming true')
continuevar = True
print('Put your cells down, now.')
print()
print('What X coordinate? (Left = 0, Right = 10)')
selectedcellx = int(sys.stdin.readline())
if selectedcellx >= 0 and selectedcellx <= 10:
print('What Y coordinate? (Top = 0, Bottom = 10)')
selectedcelly = int(sys.stdin.readline())
if selectedcelly >=0 and selectedcelly <=10:
selectedcell = int(selectedcellx + (10 * selectedcelly))
if cells[int(selectedcell)] == 0:
cells[int(selectedcell)] = 1
else:
cells[selectedcell] = 0
#test
#print(cells)
else:
print('did not understand')
else:
print('did not understand')
#something is broken in the below function; the cells never update
def updatecells():
cellnum = 0
for i in cells:
defnesw()
if cellnum > 99:
print('somehow cellnum has gone past 99. this should not have happened.')
while cellnum > 99:
cellnum = int(cellnum-100)
if i == 0:
#update
maketotal()
if total == 3:
newcells[cellnum] = 1
else:
newcells[cellnum] = 0
if i == 1:
#update
maketotal()
if total == 2 or total == 3:
newcells[cellnum] = 1
else:
newcells[cellnum] = 0
#print(cellnum) -- works
cellnum = cellnum + 1
cellnum = 0
for i in newcells:
cells[cellnum] = newcells[cellnum]
def printgrid():
cellnum = 0
for i in cells:
if i == 0:
prettycells[cellnum] = '.'
elif i == 1:
prettycells[cellnum] = 'O'
else:
print('invalid cellstate; certain things may be broken.')
cellnum = cellnum + 1
#print the grid
print(prettycells[0:9])
print(prettycells[10:19])
print(prettycells[20:29])
print(prettycells[30:39])
print(prettycells[40:49])
print(prettycells[50:59])
print(prettycells[60:69])
print(prettycells[70:79])
print(prettycells[80:89])
print(prettycells[90:99])
def rungrid():
continuevar = True
while continuevar == True:
print('continue? 1= yes, 0=no')
yn = int(sys.stdin.readline())
if yn == 0:
continuevar = False
break
if yn == 1:
continuevar = True
else:
print('did not understand')
print('assuming true')
continuevar = True
updatecells()
printgrid()
#begin program
while continueProgvar == True:
print('continue program? 1= yes, 0=no')
yn = int(sys.stdin.readline())
if yn == 0:
continueProgvar = False
break
if yn == 1:
continueProgvar = True
else:
print('did not understand')
print('assuming true')
print()
continueProgvar = True
putcells()
rungrid()
#test
#putcells()
print()
print('thank you for your time')