Learning F#: The Game of Life

gospers_glider_gunIntro

I have been using my weekends to extend my knowledge of F#.
In doing so, I have implemented a partial blackjack game as well as powered a Lego Mindstorm.
Today, I am going to lay the foundation to build Conway’s Game of Life.

Rules

Like any interaction within the universe, there are a certain laws (or in software development, rules) :

  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by over-population.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

Building the Model

I’m a novice to F#. As a result, I still struggle with the initial approach to write code. In C#, I immediately go into TDD mode. However, when building F# programs, I find myself still spinning my wheels when attempting to get started and ultimately, into flow.

Specifically,  I feel like I am forced to think before I write code. Hence, I’m obsessed with the challenge of making “illegal states unrepresentable”. Therefore, I find myself performing Domain Driven Design (or at least my version of it).

The Chicken or the Egg

I never thought I would actually face this question when creating a program. But when flushing out the domain, I immediately faced the issue of how to create life. Do I perform a Big Bang and create nine living cells initially (including the center-cell) or do I just create one cell?

let private neighbors = [One   { State=Survives; Neighbors=[] }
                         Two   { State=Survives; Neighbors=[] }
                         Three { State=Survives; Neighbors=[] }
                         Four  { State=Survives; Neighbors=[] }
                         Six   { State=Survives; Neighbors=[] }
                         Seven { State=Survives; Neighbors=[] }
                         Eight { State=Survives; Neighbors=[] }
                         Nine  { State=Survives; Neighbors=[] }]

let private root = { State=Survives; Neighbors=neighbors }

 

Currently, my design is as follows:

module GameOfLife

type Neighbor = | One   of Cell
                | Two   of Cell
                | Three of Cell
                | Four  of Cell

                | Six   of Cell
                | Seven of Cell
                | Eight of Cell
                | Nine  of Cell

and CauseOfDeath = | Underpopulated // Fewer than 2 live neighbors
                   | Overpopulated  // More than 3 live neighbors

and State = | Dies of CauseOfDeath
            | Survives       // 2 or 3 live neighbors
            | Resurected     // Is dead and has 3 live neighbors

and Neighbors = Neighbor List

and Cell = { State:State; Neighbors:Neighbors }

let private neighbors = [One   { State=Survives; Neighbors=[] }
                         Two   { State=Survives; Neighbors=[] }
                         Three { State=Survives; Neighbors=[] }
                         Four  { State=Survives; Neighbors=[] }
                         Six   { State=Survives; Neighbors=[] }
                         Seven { State=Survives; Neighbors=[] }
                         Eight { State=Survives; Neighbors=[] }
                         Nine  { State=Survives; Neighbors=[] }]

let private root = { State=Survives; Neighbors=neighbors }

<h1>TODO<h1>
I still have to write some functions to actually create life. I will share this in my next article.

Conclusion

In conclusion, I have been using my weekends to extend my knowledge of F#.
I am currently laying the foundation to build Conway’s Game of Life. In doing so, I have faced some challenges regarding the chicken or egg.

Advertisements
3 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: