I'm trying to make a quick demo of Conway's Game of life using python. When I run the code I've written below It seems to work, but when used in specific cases, like with an oscillator, everything falls apart.
(I have example outputs below)
What I have tried:
import time
import os
def main():
oldRows=[["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","+","-","-","-","-","-"],
["-","-","-","-","+","-","-","-","-","-"],
["-","-","-","-","+","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"]]
newRows=[["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"],
["-","-","-","-","-","-","-","-","-","-"]]
printBoard(oldRows)
for i in range(5):
cycle(oldRows, newRows)
oldRows=newRows
time.sleep(.2)
os.system('clear')
cycle(oldRows, newRows)
def printBoard(rows):
print("______________________________")
for r in range(len(rows)):
for i in range(len(rows)):
print(" " + rows[r][i] + " ", end="")
print("")
print("______________________________")
def checkPop(oldRows, r, i):
popCount = 0
if (r != 0):
if (i != 0):
if (oldRows[r-1][i-1] == "+"):
popCount+=1
if (oldRows[r-1][i] == "+"):
popCount+=1
if (i != len(oldRows)-1):
if (oldRows[r-1][i+1] == "+"):
popCount+=1
if (i != 0):
if (oldRows[r][i-1] == "+"):
popCount+=1
if (i != len(oldRows)-1):
if (oldRows[r][i+1] == "+"):
popCount+=1
if (r != len(oldRows)-1):
if (i != 0):
if (oldRows[r+1][i-1] == "+"):
popCount+=1
if (oldRows[r+1][i] == "+"):
popCount+=1
if (i != len(oldRows)-1):
if (oldRows[r+1][i+1] == "+"):
popCount+=1
return popCount
def checkBirth(oldRows, r, i):
if (checkPop(oldRows, r, i) == 3):
return True
else:
return False
def cycle(oldRows, newRows):
for r in range(len(oldRows)):
for i in range(len(oldRows)):
if (oldRows[r][i] == "-"):
if checkBirth(oldRows, r, i):
newRows[r][i] = "+"
else:
newRows[r][i] = "-"
else:
if (checkPop(oldRows, r, i) < 2):
newRows[r][i] = "-"
elif (checkPop(oldRows, r, i) > 3):
newRows[r][i] = "-"
else:
newRows[r][i] = "+"
printBoard(newRows)
if __name__ == "__main__":
main()
Here's what happens when I run it ("-" is a dead cell and "+" is a live cell):
**Expected Output:**
Gen 1:
...- - + - - ...
...- - + - - ...
...- - + - - ...
Gen 2:
...- - - - -...
...- + + + -...
...- - - - -...
Gen 3:
...- - + - - ...
...- - + - - ...
...- - + - - ...
etc....
**Actual Output:**
Gen 1:
...- - + - - ...
...- - + - - ...
...- - + - - ...
Gen 2:
...- - - - -...
...- + + + -...
...- - - - -...
Gen 3:
...- - + + - ...
...- + - + - ...
...- - - - - ...
Gen 4:
...- - + + - ...
...- - + + - ...
...- - - - - ...