Conway’s Game of Life in Python (Automate the Boring Stuff with Python Practical Programming for Total Beginners)

Conway’s Game of Life

this code is about Conway's Game Of Life in Python.
and this is a brief summary of it.

Conway’s Game of Life is an example of cellular automata: a set of rules governing the behavior of a field made
up of discrete cells. In practice, it creates a pretty animation to look at. You can draw out each step on graph
paper, using the squares as cells. A filled-in square will be “alive” and an empty square will be “dead.” If a
living square has two or three living neighbors, it continues to live on the next step. If a dead square has
exactly three living neighbors, it comes alive on the next step. Every other square dies or remains dead on
the next step. You can see an example of the progression of steps in Figure 4-8



the game of life.py
import random, copy
first_matrix=[]
new_matrix=[]
column=7
row=5
for i in range(row):
  first_cell=[]
  for e in range(column):
    if random.randint(0,1)==1:
      first_cell.append(1)
    else: first_cell.append(0)
  print(first_cell)
  first_matrix.append(first_cell)
newcell=copy.deepcopy(first_matrix)
saved=copy.deepcopy(first_matrix)


#The main loop
for k in range(3):
  print('\n\n\n\n\n')

  first_matrix=copy.deepcopy(newcell)
  for x in range(row):
   for y in range(column):

    right=first_matrix[x][(y+1)%column]
    left=first_matrix[x][(y-1)%column]
    above=first_matrix[(x-1)%row][y]
    below=first_matrix[(x+1)%row][y]
    BR=first_matrix[(x+1)%row][(y+1)%column]
    BL=first_matrix[(x+1)%row][(y-1)%column]
    UR=first_matrix[(x-1)%row][(y+1)%column]
    UL=first_matrix[(x-1)%row][(y-1)%column]
    all=right+left+above+below+BR+BL+UR+UL
    cor1=right+below+BR
    cor2=left+below+BL
    cor3=above+right+UR
    cor4=above+left+UL
    firstrow=left+right+below+BL+BR
    lastrow=left+right+above+UR+UL
    firstcolumn=above+below+right+UR+BR
    lastcolumn=above+below+left+UL+BL
    alive=0
    if first_matrix[x][y]==1:
       alive=1
    else:alive=0

    if alive==1 and x==0 and y==0 and cor1>=2:
       newcell[x][y]=1
    elif alive==0 and x==0 and y==0 and cor1==3:
       newcell[x][y]=1
    elif alive==1 and x==0 and y==(column-1) and cor2>=2:
       newcell[x][y]=1
    elif alive == 0 and x == 0 and y == (column - 1) and cor2 == 3:
       newcell[x][y] = 1
    elif alive==1 and x==(row-1) and y==0 and cor3>=2:
       newcell[x][y]=1
    elif alive==0 and x==(row-1) and y==0 and cor3==3:
       newcell[x][y]=1
    elif alive==1 and x==(row-1) and y==(column-1) and cor4>=2:
       newcell[x][y]=1
    elif alive == 0 and x == (row - 1) and y == (column - 1) and cor4 == 3:
       newcell[x][y] = 1
    elif alive==1 and x==0 and y>0 and y<(column-1) and firstrow>=2 and firstrow<=3:
       newcell[x][y]=1
    elif alive == 0 and x == 0 and y>0 and y<(column-1) and firstrow == 3 :
       newcell[x][y] = 1
    elif alive==1 and x==(row-1) and y>0 and y<(column-1) and lastrow>=2 and lastrow<=3:
       newcell[x][y]=1
    elif alive == 0 and x == (row - 1) and lastrow == 3 and y>0 and y<(column-1) :
       newcell[x][y] = 1
    elif alive==1 and y==0 and firstcolumn>=2 and firstcolumn<=3 and x>0 and x<(row-1):
       newcell[x][y]=1
    elif alive == 0 and y == 0 and firstcolumn == 3 and x>0 and x<(row-1):
       newcell[x][y] = 1
    elif alive==1 and y==(column-1) and  lastcolumn>=2 and lastcolumn<=3and x>0 and x<(row-1):
       newcell[x][y]=1
    elif alive == 0 and y == (column - 1) and lastcolumn == 3 and x>0 and x<(row-1):
       newcell[x][y] = 1
    elif alive==1 and x>0 and y>0 and x<(row-1) and y<(column-1) and all>=2 and all<=3:
       newcell[x][y]=1
    elif alive == 0 and x > 0 and y > 0 and x < (row - 1) and y < (column - 1) and all == 3 :
       newcell[x][y] = 1
    else: newcell[x][y]=0

   print(newcell[x])

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post