Skip to main content

conway's game of life. - code

Rules of the Game:

The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbors, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
  1. Any live cell with fewer than two live neighbors dies as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by overcrowding.
  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed—births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.



We start by creating a HTML boiler plate with an empty canvas in the the body tag.
after the html we add a script tag and initialize the canvas and some global variables along with some utility fucntions.:
Next, we creat a Class which is representative of a cell in the universe of the game.
We define a draw member function to draw a grid of a cell. We will also define a fill member function which will add a fill to the grid to signify a live cell. Other member variables would be the position and dimensions of the cell.

we will define a method to draw the universe:
we will define a pure function to apply the rules of the game and a method to calculate the next generation.
You will notice in the pure function there is a line lifeCount = lifeCount > 0 ? lifeCount - 1 : 0; we reduce the count because the calculation is inclusive of the current cell.

to close the development on the game we'll add some event handlers to make the game a little interactive:
and then you will have the final result: https://keith3895.github.io/conways-game-of-life/index.html

the code is on github: https://github.com/Keith3895/conways-game-of-life/blob/master/

Comments

Popular posts from this blog

Murmuration: A view into Artificial life.

Murmuration refers to the phenomenon that results when hundreds, sometimes thousands, of starlights(starlings) fly in swooping, intricately coordinated patterns through the sky. My introduction to the word, murmuration, was by the Jazz trio, GoGo penguins. Murmuration is the 5th piece they perform in one of their live performances: link . It is one of the most enthralling pieces of music I've heard. I implore you to check them out:  https://gogopenguin.bandcamp.com/ . When we observe these starlings fly we see a pattern that stands out. And when there's a pattern involved there is a strong possibility an algorithm exists to explain it and implement a computer simulation which helps me to segue into Boids algorithm. Boids algorithm is an artificial life program, developed by Craig Reynolds. It simulates the flocking behavior of birds. Boid is a shortened version of Bird-oid, which refers to the bird-like object. Boids are an example of emergent behavior,i.e. the algorithm wo...

Understanding TDD - Part 2 - Adopting TDD

In part 1 of this series, we explored the fundamentals of Test Driven Development. After wrapping our heads around the principles and understanding the origins of TDD we will now look into applying TDD.  Understanding TDD - Part 1 - "What is TDD?"

The Pursuit Of Clean Code

There are different ways to interpret the meaning of clean code. The idea of clean code is a subjective concept and every developer has their own interpretation of it. Clean code is code that is easy to understand and easy to change"  When we say "easy to understand" what does it really translate to? Easy to understand can be easy to read, it can be easy to understand the flow of the code, easy to understand sequence events. Easy to understand can also quantify to the ease to understand the purpose and function of the methods and variables defined or declared. Easy to change can be interpreted as easy to extend and refactor, and it is easy to fix bugs. Easy to understand can be anything! it's up to the readers interpretation. When we write code it is generally in as higher-order language. This implies that we intend the code to be understood by other developers or other people. A higher-level language is not directly interpreted by the computer. A hig...