Game Of Live in Clojure with Quil

Posted on

This is my attempt to implement Conway’s Game of Life in Clojure using Quil library.

Game of Life

There is an infinite grid of cells that can be alive or dead. For each iteration state of the grid is recalculated using several rules:

  • Alive cell with 2 or 3 alive neighbors will live, in other cases it will die.
  • A dead cell will become alive only if it has 3 alive neighbors.

Such simple rules create a lot of interesting and complex patterns. For example, oscillator - a pattern that evolves in itself after number of iterations.

Quil

Quil is a Clojure wrapper around Processing. Dynamic nature of Clojure and simple Processing API for graphics gives ability to quickly prototype algorithmic graphics. Checkout Quil LightTable tutorial and try their dynamic workflow.

Basic Quil application with funcional mode consists of 3 main functions: setup, update and draw. Each function works with the state - a map of everything you need on the scene. Instead of updating global variables for each frame, Quil gives us an API to deal with program state in a functional way.

Setup

is called once before the first frame. The main goal is to setup Quil scene and create a state map.

Here :life is a set of all alive cells. Usually, developers implement grid by a twodimensional matrix. I’ve found working with the set just more comfortable for infinitely large grid.

Update

is a function that transforms the state for every frame.

First it creates candidates - the set of all alive cells and their empty neighbors. Only those cells can be alive in the next iteration. Next it creates next-life set from candidates that will survive this iteration.

Draw

function takes a state and draws a frame.

It is simple like that. Just clears scene and draws a square for every alive cell.

TL;TR

The whole project with instructions how to run it is available on github. Fill free to play with it.

Have a nice hack ;)