Conway's Game of Life in JavaScript

Introduction

Conway's Game of Life is an example of cellural automation. It was created by British mathematican John Horton Conway in 1970, and according to wikipedia it is "best-known example of cellurar automation".

For some reason Conway's invention was always fascinating me. As a big fan of computer games myself, it was quite interesting to find out there is a game that involves no players! Because this is exactly what the Game of Life is - a unique kind of game that plays itself based only on its initial state.

Rules

Conway's Game of Life is a matrix of cells. Cell can be in active state (marked later as:    ) or in inactive state (marked as:    ). Every iteration (game loop step) is checking how many active neighbours particular cell has, and based on the number of neighbours it either activate or deactive itself. The rules are:

  1. Any live cell with less than 2 live neighbours dies, as if caused by underpopulation.
  2. Any live cell with more than 3 live neighbours dies, as if by overcrowding.
  3. Any live cell with 2 or 3 live neighbours lives on to the next generation.
  4. Any dead cell with exactly 3 live neighbours becomes a live cell.

My implementation

When I decided to write my own version of The Game, I knew I will do it in JavaScript. And yes, I know JavaScript is probably the last programming language we should consider for this task because it offers poor performance, especially when dealing with complex, multiple callculations but what the hell. Let's do it for for the sake of it.

Below is ready output from my script and few demos so you can preview it in action.

GridOptions
Recording
Select Demo:
Interval:

I also added recording option, so if you want to test your own input use the Record button.

TBC.

Did you like my article? Share it!
Share/Bookmark

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <csharp>, <css>, <html>, <javascript>, <vb>, <vbnet>, <xml>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.
served by y.co.uk