Skip to main content
blog title image

7 minute read - Technical Testing JavaScript

Hacking JavaScript Games - Cellular Automata - Supporting Notes

Dec 1, 2016

Hacking JavaScript Games - Cellular Automata - Supporting Notes

Notes on Slideshare

And there are more notes below:

Hacking JavaScript Games - Cellular Automata - Supporting Notes

We will Hack This Game

Cellular Automata Life Game Suitable for Hacking

Supporting Videos

Challenge - Change Colour in Cellular Automata

The simplest thing we can do to this game of life is to change the colour.

It is also the most visible thing.

Using the game:

Colour in the Game

In the code we have the World function - which is an object class.

And the World object has an entityColour.

If we can change that, then we can change the colour.

I could use any six values from 0-9, A-F to make an HTML colour code


Change colour by debugging

I set a break point on the line where the colour is set in the sources view.

  • Line 153
  • this.entityColour = "#FF0000";


  • Step over that line so the colour is set.
  • press esc to get the console up.
  • change the colour in the console:
this.entityColour = "#111111";

Change colour from console when runnning game

  • change the colour using the console
  • inspect element and find where World is instantiated with new. i.e. ctrl+F and search for new World.
var world = new World(worldSizes.myWorldWidth, 

And I can change that whenever I want:

world.entityColour = "#111111";

Watch The Video

Change Colour Automatically

Simple - have a bot to change colour every set time interval - say 1000 milliseconds.

The challenge is that I need to use a hex value to do that. So let’s figure that out first.

Random Hex Value

  • I can use 0xFF as a Hex integer value, and it is 255
  • I can use the toString method on a Number to convert a number to a base e.g. base 2 for binary or base 16 for hex:
  • I need to get a 6 character string, even if my hex value is only FF and I can do that by creating a string and using the last 6 characters e.g.
  • And I want my string to be "0" padded at the front so I’ll just add "000000" at the front of any string:
  • And I want a Hash # in front
var myNum=3;
"#" + (("000000" + myNum.toString(16)).substr(-6))

Use Game Random Int Code

The game already has a random number generator:

getRandomInt(0, 0xFFFFFF);

Use that

"#" + (("000000" + 
getRandomInt(0, 0xFFFFFF).toString(16)).substr(-6))

And I have a way to generate a random HTML hex colour value.

Randomly set the colour every second

Create a bot to change the colour every 1000 milliseconds.

var colourChangeBot = setInterval(function() {
    world.entityColour = "#" + (("000000" + 
        getRandomInt(0, 0xFFFFFF).toString(16)).substr(-6));
}, 1000);

Stop the bot with:


Make Bot Faster or Slower

Change the value 1000, to make it faster, or slower:

  • 500 - every half second
  • 200 - every 200 milliseconds

Danger Flashing Lights.

Just be careful you don’t make it so fast that you have a seizure.

Cycle over all the colours

This is a bit slower:

var currColour = 0;
var colourChangeBot = setInterval(function() {
    world.entityColour = "#" + ("000000" + 
    if (currColour >= 0xFFFFFF) {
        currColour = 0;
}, 1)

Watch The Video


An Invasion Challenge

Control the Invasion

Stop the invasion by calling:


Start an invasion with:


Where 100 is the amount of milliseconds before the next lifeform is added.

Create a Glider


Which creates a glider at position (10,0)

I could create an invasion of gliders like that

var gliderInvasion = setInterval(function() {
    addLifeForms.glider(world, 10, 0);
}, 1000);

But an invasion should be random

First stop the old invasion:


Then start our new invasion:

var gliderInvasion = setInterval(function() {
       getRandomInt(0, world.xSize - 50), 
       getRandomInt(0, world.ySize - 50));
}, 100);

Watch The Video

Change World Size

One of the challenges in the Cellular Automata game is changing the world size.

There is a clue in the game code, because we know that it does some resizing when we change the window size.



var world = new World(

Let’s Change the World - Make is smaller


Let’s Change the World - Make is bigger


Nope, that didn’t work

Let’s Change the World - Make is bigger

tell the world that it is bigger

world.xSize=1000; world.ySize=1000;

That worked - and its a little slower now, because thats 1000 x 1000 grid, which is 1,000,000 (1 million) cells!

Simulate ZX-81

The ZX-81 had a resolution of 64x48, and it was black and white, so let’s simulate that.

worldSizes.myWorldWidth=64; worldSizes.myWorldHeight=48;

And slow it down:


Watch The Video

Create A Random World

We first want total control over the game.

  • start game
    • startGame(5)
  • stop game
    • stopGame()
  • stop invasion
    • stopInvasion()

Stop the Game and Clear the world


It would be useful to also clear the display.


But we’ll only see the results if we start & stop the game again:


Randomly generate stuff

Reset the population:

world.population = [];

I’ll add items in a loop, and create 100 of them:

for(var itemLoop=0; itemLoop < 100; itemLoop++){
	world.addToPopulation(new LifeForm().move(0,
	getRandomInt(0, world.xSize), 
	getRandomInt(0, world.ySize)));

Start the game, we should see the results:


100 might not actually be big enough, you might need to create more for a viable world.

Experiment and Watch the Video

Experiment with different loop sizes to see what works for your world - remember its a harsh world out there for these cells so sometimes you need to start with a lot of them.

History of Game Of Life

John Horton Conway’s Game of Life

Programs to download, install and experiment with:

online games of life:

Cellular Automata Life Game Suitable for Playing

Cellular Automata Life Game Suitable for Hacking

Good simple Overview

Good set of resources

Wolfram description

History of Computer Game Design - Stanford

  • Artificial Life - by Stephen Levy
  • The Garden in the Machine by Clause Emmeche

Watch A Video on the History of the Game of Life

Other Contributions

Danny Dainton experimented with the Game of life and created a ’text’ image.

And he has released thecode for it on github.

Thanks Danny

How to Create Text

I took a different approach to Danny for the text creation:

  • I thought it would be easier to write text to the screen
  • scrape the pixels
  • add the pixels as blocks to the world

First Write the Text

// stop the world and clear the world and screen
world.population = [];

// plot the text we want

context.font = "14px verdana";
// kerning hack
var padding = String.fromCharCode(8202)+String.fromCharCode(8202);
context.strokeText("Thank".split("").join(padding), 0, 15);
context.strokeText("You".split("").join(padding), 0, 30);
context.strokeText("Danny".split("").join(padding), 0, 45);

Then get the pixels and add them to the world

var imageData = context.getImageData(0,0,200,60);

var scale=4; // less than 4 and it will evolve
var imageX=0;var imageY=0;
for (var i=0;i<;i+=4)
  if([i]!==0 ||[i+1]!==0 ||[i+1] !== 0){

    addLifeForms.block(world, imageX*scale,imageY*scale);

Then startGame(5);


We could use this for image generation, just with the canvas.

  • resize canvas
  • create a border
  • plot text that shows the size
  • save the image

And we could create custom sized images for testing.

Resize Canvas


Create a Border

context.rect(0,0,canvas.width, canvas.height);

Add Text

context.font = "80px verdana";
var padding = String.fromCharCode(8202)+
context.strokeText("500px".split("").join(padding), 0, 80);
context.strokeText("500px".split("").join(padding), 0, 160);

Watch A Bonus Video - Create Text For Cellular Automata

What can you do with it?

Experiment and see what you come up with