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

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?"

"That's not my job"

As an individual, I am a person always looking to change things quickly. The mundanity of repeating the same things over and over again gets me agitated to question the efficiency of the activity. With this naive outlook, and fueled by Pink Floyd's album "Welcome to the machine" I am trying to write this blog post about the changes I've seen. Over the years I've seen some changes that happen in an organization and this blog is more of a commentary on the things that I've seen. Without question, this blogpost has a major TL;DR warning. Life in a corporate office might seem a little kafkaesque. Kafkaesque, A term associated with Frans Kafka generally is used to describe unnecessarily complicated and frustrating experiences, like being forced to navigate labyrinths of bureaucracy. The sort of complex, unclear processes in which no one individual ever really has a comprehensive grasp of the system or what is going on.  In massive companies that are well establishe...

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...