Functional Thread 5 : Predictable State Engine
In the previous threads on functional programming, we have explored a number of abstract concepts, and some of the many simplistic concepts that are employed when writing more functionally oriented code.
Here's the links to those threads:
Now for the goal for this thread:
The aim of this thread is to look more in-depth at the notion of pure functions, immutability and application state engines. This is essentially a continuation of the previous threads, except that we're going to build a simple working game engine and a very simplistic UI. The point being to not get too overwhelmed by the game logic, so that we can focus in on the state engine and its benefits of:
The chosen game is snake.
Whilst I mentioned in thread 4 that I had considered using the Pong game for this; I have decided rather to go with Snake primarily because its far lighter ito game logic, meaning we won't unnecessary be bogged down with too many things (including code) that doesn't relate directly with the concepts of building a Predictable State Engine with a reducer.
Ok, so now for the open challenge
Can you build a game engine for Snake, that is completely independent of its View and Controller(s); in addition the game engine, should be able to run without a UI, and all components (functions, classes, structs, enums, ...) should ideally enable 100% test coverage.
Ok, let's be a bit more specific about which Snake variation we want to build:
Feel free to code this in the language of your preference, and please take liberty with the design of your UI. The version I will finally walk though in this thread will be coded in Swift, together with a very simplistic terminal based UI using emoji characters, for example:

Btw as for the tests: this is an optional requirement, so don't be concerned too concerned if some aspects are not easy to test. This is after all one of the goals of this thread; to demonstrate a way to build predictable application state and logic that is easy to test.
Timeline
Considering the scope of this thread and challenge. I'll only start posting actual game code after approx one and a half weeks from today.
Good luck.
In the previous threads on functional programming, we have explored a number of abstract concepts, and some of the many simplistic concepts that are employed when writing more functionally oriented code.
Here's the links to those threads:
- Category Theory - Intermediate / Advanced
- Category Theory for programmers - Intermediate to Advanced
- Simple Functional Approach Example - Beginner
- Another Functional Programming Challenge - Intermediate / Advanced
- Functional Challenge 3 - Beginner / Intermediate
- Functional Thread 4 - Beginner / Intermediate
Now for the goal for this thread:
The aim of this thread is to look more in-depth at the notion of pure functions, immutability and application state engines. This is essentially a continuation of the previous threads, except that we're going to build a simple working game engine and a very simplistic UI. The point being to not get too overwhelmed by the game logic, so that we can focus in on the state engine and its benefits of:
- Predictable state.
- Clearly delineation between model and view / controller.
- Ease of reuse.
- Ease to test.
The chosen game is snake.
Whilst I mentioned in thread 4 that I had considered using the Pong game for this; I have decided rather to go with Snake primarily because its far lighter ito game logic, meaning we won't unnecessary be bogged down with too many things (including code) that doesn't relate directly with the concepts of building a Predictable State Engine with a reducer.
Ok, so now for the open challenge
Can you build a game engine for Snake, that is completely independent of its View and Controller(s); in addition the game engine, should be able to run without a UI, and all components (functions, classes, structs, enums, ...) should ideally enable 100% test coverage.
Ok, let's be a bit more specific about which Snake variation we want to build:
- No barriers
- No enemies or mines
- 1 apple (the target) generated randomly at a time
- 1 snake controlled by the keypress actions e.g. arrow keys (left, right, up, down).
- When the snake consumes an apple it grows longer by 1 board position, and a new apple is randomly placed on the board.
- Board size: 40 x 28 (ideally this should be configurable)
- If snake exits the board on the right, it reenters automatically on the left, similarly top / bottom.
- Pressing the key 'q' exits the game
- Pressing the key 'r' resets the game
- If a snake collides with its own body; it dies, and the game resets.
Feel free to code this in the language of your preference, and please take liberty with the design of your UI. The version I will finally walk though in this thread will be coded in Swift, together with a very simplistic terminal based UI using emoji characters, for example:

Btw as for the tests: this is an optional requirement, so don't be concerned too concerned if some aspects are not easy to test. This is after all one of the goals of this thread; to demonstrate a way to build predictable application state and logic that is easy to test.
Timeline
Considering the scope of this thread and challenge. I'll only start posting actual game code after approx one and a half weeks from today.
Good luck.
Last edited:

