Implementing the Game of Life

What Was Used

Implementation — Part 1

class Cell:
def __init__(self, col, row):
self.col = col
self.row = row
# all cells are initially dead, we input the alive ones.
self.state = False
class Grid:
def __init__(self, cell_width, width_, height_):
self.width_ = width_
self.height_ = height_
self.cell_width = cell_width
self.cols = self.width_ // self.cell_width
self.rows = self.height_ // self.cell_width

“Creating” the Grid.

grid = [column1, column2, column3] # as an example
column1 = [cell1, cell2, cell3]
column2 = [cell4, cell5, cell6]
column3 = [cell7, cell8, cell9]
grid = [
[cell1, cell2, cell3], # used to be column1
[cell4, cell5, cell6], # used to be column2
[cell7, cell8, cell9] # used to be column3
]

Showing the Grid

for column in self.grid:
for column in self.grid:
for cell in column:
for column in self.grid:
for cell in column:
cell.show()
For some reason while writing the code, I decided to use the name `col` instead of `column`.

Erasing the Grid

Getting the Neighbors of a Cell

return [cell.state for cell in neighbors]

Default Configurations

Implementation — Part 2

  1. Create a grid, called reading_grid.
  2. Create another empty grid, called writing_grid. (By empty, I mean a grid where all cells are initialized to being dead.)
  3. Based on reading_grid, fill in writing_grid.
  4. Make writing_grid the reading_grid of the next generation.
  5. Show reading_grid on the screen.
  6. Erase the elements of writing_grid.
reading_grid = Grid(cell_width, *DIM)
writing_grid = Grid(cell_width, *DIM)
reading_grid.create()
def setup():
size(*DIM) # unpack the values of DIM
def keyPressed():
global running
if key == ENTER:
running = not running
def keyPressed():
global running
if key == ENTER:
running = not running
if not running:
if key == "c":
reading_grid.kill_cells()

Improvements

--

--

--

A highschool student interested in programming. Writes up some articles from time to time.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to enable Reddit dark mode on the website: 4 step guide

Practical computer science: connected components in a graph

Alif: an Arabic word bank

My Favorite Password One-Liner

Everything You Need to Know About Python Dictionaries

Count The Number of Moon Rocks Using Python

The Newbie Backlog: How we welcome our new co-workers at Discngine

How to Use EOSJS API (1)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ahmed Addous

Ahmed Addous

A highschool student interested in programming. Writes up some articles from time to time.

More from Medium

Find whether (0,0) lies inside a triangle or not (2-axis)

Neural Networks — What is it and Why does it matter?

Problems Vs Solutions: What exactly is an Algorithm?

Breaking into Data Science