After my last post with the Meggy Jr displaying the weather, I wanted to write a standalone app for the Meggy Jr that doesn’t connect to a PC and does a little more than just display data that’s being sent to it.
So, I decided to implement a version of Conway’s Game of Life on the Meggy, there’s just enough pixels to make it work well and I think it looks pretty cool. If you haven’t heard of the Game of Life, it’s not a cheesy gameshow, it’s a cellular automaton where we have a grid of cells and each cell is either ‘alive’ or ‘dead’.
Each iteration of the cells follows a set of simple rules:
- Any live cell with fewer than two live neighbours dies
- Any live cell with two or three live neighbours lives on to the next iteration
- Any live cell with more than three live neighbours dies
- Any dead cell with exactly three live neighbours becomes a live cell
By setting an initial configuration of cells that are alive we can see how patterns evolve, there’s huge number of patterns that can be created, some oscilate between different states, some are stable and some ‘die’ after a certain number of iterations. Check out http://www.conwaylife.com/ if you want to find out more about the different types of patterns that can be created such as ‘ships’, ‘guns’ and even self replecating machines.
On the Meggy Jr
I’ve implemented a simple version of game of life and to get around the limitation of having only an 8by8 grid to play with I’ve wrapped the edges so that if a pattern goes off the screen it wraps to the other side. I’ve also put an ‘edit’ mode in the game so that when it starts up there’s a red cursor that can be moved with the directional pads, when the B button is pressed it sets that cell to alive. When A is pressed it continues the game with the cell pattern that has been set. Pressing A again during the game will return it to the ‘edit’ mode. (Or at least it should most of the time, I really need to put in some debouncing code). The auxilary LEDs at the top display in binary the number of iterations that have passed, this will reset after 255.
You can see it in action below, I create a pattern called a toad that oscilates between two states.
As always the code is available here, have fun!