Game Of Life

The Game of Life is not your typical computer game. It is a 'cellular automaton', and was invented by Cambridge mathematician John Conway. This game became widely known when it was mentioned in an article published by Scientific American in 1970. It consists of a collection of cells which, based on a few mathematical rules, can live, die or multiply. Depending on the initial conditions, the cells form various patterns throughout the course of the game.

The rules of the game are:

For a space that is 'populated':

For a space that is empty of unpopulated:

Python code to Game of Life Program

from tkinter import * buttonarray=[] def createbuttons(): #creates the buttons global buttonarray for y in range(0,50): for x in range(0,50): buttonarray.append(Button(mGui,command=lambda i=(y*50)+x :colour(i))) buttonarray[(y*50)+x].place(x=110+(10*x),y=125+(10*y),width=10,height=10) def colour(x): #assigns colours to buttons when clicked global mainarray global buttonarray y=int(x/50) z=x-(50*y) print(z,y,mainarray[y][z]) if mainarray[y][z] == 0: mainarray[y][z]=1 buttonarray[x].configure(background="red") else: mainarray[y][z]=0 buttonarray[x].configure(background="#F0F0ED") def arraycreate(): #creates an 2D array to store the state of each button global mainarray mainarray = [] for i in range(50): mainarray.append([]) for j in range(50): mainarray[i].append(0) def temparraycreate(): #creates a temporary 2D array which can be overwritten global temparray temparray=[] #temparray=[[0]*50]*50 for i in range(50): temparray.append([]) for j in range(50): temparray[i].append(0) def neighbours(): #subroutine to check how many neighbours each button has global mainarray global temparray neighbours=0 for y in range(1,49): for x in range (1,49): neighbours=0 if mainarray[y-1][x-1] == 1: neighbours = neighbours + 1 if mainarray[y-1][x] == 1: neighbours = neighbours + 1 if mainarray[y-1][x+1] == 1: neighbours = neighbours + 1 if mainarray[y][x-1] == 1: neighbours = neighbours + 1 if mainarray[y][x+1] == 1: neighbours = neighbours + 1 if mainarray[y+1][x-1] == 1: neighbours = neighbours + 1 if mainarray[y+1][x] == 1: neighbours = neighbours + 1 if mainarray[y+1][x+1] == 1: neighbours = neighbours + 1 if neighbours>0: #print(y,x,neighbours) rules(y,x,neighbours) newbuttons() mainarray=temparray temparraycreate() def rules(y,x,z): #rules of game of life that each button must follow global mainarray global temparray if mainarray[y][x]==0 and z==3: temparray[y][x]=1 if mainarray[y][x]==1: if z <= 1: temparray[y][x]=0 elif z >= 4: temparray[y][x]=0 else: temparray[y][x]=1 def newbuttons(): #updates the buttons using the rules in update subroutine global buttonarray for y in range(0,50): for x in range(0,50): if temparray[y][x]==0 and mainarray[y][x]==1: buttonarray[(y*50)+x].configure(background="#F0F0ED") elif temparray[y][x]==1 and mainarray[y][x]==0: buttonarray[(y*50)+x].configure(background="red") running=False def autoslow(): global running if running==False: running=True runslow() else: running==False def runslow(): global running if running==True: neighbours() mGui.after(1250-(12*30),runslow) def automed(): global running if running==False: running=True runmed() else: running==False def runmed(): global running if running==True: neighbours() mGui.after(1250-(12*75),runmed) def autofast(): global running if running==False: running=True runfast() else: running==False def runfast(): global running if running==True: neighbours() mGui.after(1250-(12*100),runfast) def stop(): global running running=False def clear(): temparraycreate() newbuttons() arraycreate() mGui=Tk() hent=StringVar() mGui.resizable(width=FALSE, height=FALSE) mGui.geometry("750x750") mGui.title("Anant's 'Conway's Game of Life'") #Jon is the best mCanvas = Canvas (mGui, width=750, height=750), y=-2) mCanvas.create_rectangle(-2,-2,750,750, fill="#123456") nextbutton=Button(mGui,text="Next Frame",command=neighbours).place(x=640,y=125,height=25,width=75) slowbutton=Button(mGui,text="Slow",command=lambda:autoslow()).place(x=640,y=150,height=25,width=75) mediumbutton=Button(mGui,text="Medium",command=lambda:automed()).place(x=640,y=175,height=25,width=75) fastbutton=Button(mGui,text="Fast",command=lambda:autofast()).place(x=640,y=200,height=25,width=75) stopbutton=Button(mGui,text="Stop",command=stop).place(x=640,y=250,height=25,width=75) clearbutton=Button(mGui,text="Clear",command=clear).place(x=640,y=275,height=25,width=75) label1=Label(mGui,text="Conway's Game of Life",font="Arial 35 bold",fg='white',bg='#123456').place(x=75, y=30) ruleslabel=Label(mGui,text="The Rules",font="Arial 10 bold",fg='white',bg='#123456').place(x=107,y=625) ruleslabel2=Label(mGui,text="For a space that is 'populated':",font="Arial 7 bold ",fg='white',bg='#123456').place(x=110,y=645) ruleslabel3=Label(mGui,text="Each cell with one or no neighbours dies, as if by solitude.",font="Arial 7 ",fg='white',bg='#123456').place(x=110,y=660) ruleslabel4=Label(mGui,text="Each cell with four or more neighbours dies, as if by overpopulation.",font="Arial 7 ",fg='white',bg='#123456').place(x=110,y=675) ruleslabel5=Label(mGui,text="Each cell with two or three neighbours survives.",font="Arial 7 ",fg='white',bg='#123456').place(x=110,y=690) ruleslabel6=Label(mGui,text="For a space that is 'empty' or 'unpopulated'",font="Arial 7 bold",fg='white',bg='#123456').place(x=110,y=705) ruleslabel7=Label(mGui,text="Each cell with three neighbours becomes populated.",font="Arial 7 ",fg='white',bg='#123456').place(x=110,y=720) buttonarray=[] mainarray=[] j=0 createbuttons() arraycreate() temparraycreate()