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 1 - “What is TDD?”

Understanding TDD This post is written with the intent to help understand the concepts around TDD and help in the adaptation of the TDD approach in the development process. The intended targets are developers, Team leads, and managers. The idea behind creating this was to explain the background of TDD. This first part covers the origins of TDD and the basic principles that define development with TDD. ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- --- What is TDD? Test-driven Development abbreviated to TDD,  is an Approach to problem-solving or application/software development when unit tests are written prior to writing code.  Before jumping in headfirst into Test-driven Development (TDD) let’s look back at the background of how TDD came to be. History of TDD "Generally, the modern “rediscovery” of TDD is attributed to

"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

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