Conway’s Game of Life ☠️ in F# and CodeRetreat

Daniil Ekzarian
Reflash Programming Adventures
3 min readMar 22, 2016
5

Recently I’ve taken part in the coding event that was organized by EPAM in Saint-Petersburg, that’s call CodeRetreat. The basic idea of that is that often developers have no time to improve their coding skills, because they are only developing business needed tasks, so they are constantly under pressure and couldn’t be relaxed. But when developers come to CodeRetreat they are given tasks that they could solve without any pressure like time limits or quality checking, no one requires you’ll solve this task in time. Another kind of challenge is that you’ll try to code the task in different languages, you have only 45 minutes for 1 coding session. Involvement in this event was very interesting for me and that was really funny.

The task was to develop some basic parts of the Conway’s Game of Life rules, so they’ll bypass tests that you’ll write. I’ve decided to try F# language for one of sessions, I’ve just completed the basic tests for this task, but I couldn’t stop, so I’ve ended the task when I’ve been already at home.

If you’re not familiar with Conway’s Life game and its rules, you could follow this link for further information.

The main challenge of that is to develop in pairs using TDD (Test Driven Development), so I’ll show you the basic tests that should be passed. I’ve developed the code to be maximum compact and sure it is not the optimized way to solve this task. I should also mention that the cell field could only be square.

First test will check that the cell with no neighbors will die:

As you see we are passing array of arrays to Game.Step, and then check that it returns the same object, but with dead cell at left upper corner.

I’ve started implementing the function that will be making Game step and I’ve decided firstly to implement that I’d rather need first to finish this one. To follow the game rules we need to know how many neighbors the current cell has, so I’ve written some tests on how I see this function usage, I’ll show you just one of them:

I think the test is rather simple to understand so I’ll just skip the explanation. Let’s look at the implementation:

The idea is that we take a square that is completed of 9 cells including the current and the cells that current border. Then form a list excluding the current cell and the out of borders cells, and just sum their contents, for live cell we’ll get +1, for dead 0, at the end we’ll get the number of the cell’s neighbors.

So now we could implement the Game.Step:

The idea is also very simple we get the current game states and iterate them just replacing the resulting contents with the result of logical expression. In case we have three neighbors we’ll return a live cell in any case and if we have two, then the state will be that it was in previous step, for all other cases the cell will be dead.

That’s all, the tests show that stepping is made correctly, you could try the code on GitHub. I’ve really enjoyed this contest and also F# language, so I hope this maybe will help or be interesting for you. Thank you for reading!

--

--