• PicŪ Basic

  • Game of Life on an LED matrix

    I've been meaning to write a "Game of Life" simulation for many years, but I could never find the right motivation. That is, until I discovered the Sure 2416 LED dot matrix board.

    I discussed interfacing to one of these lovely little boards in a previous article. Interfacing to the Sure2416 LED dot matrix display board So we'll use the same companion shield layout for the Game of Life simulator, and the "2416.inc" file that talks to the Sure 2416 board.

    What is the Game of Life?
    The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970. The game is really a zero-player simulation, meaning that its evolution is determined by its initial state, requiring no further input.

    Conway chose the rules of the simulation after trying many possibilities, some of which caused the cells to die too fast and others which caused too many cells to be born. Life balances these tendencies, making it hard to predict whether a pattern will die out completely, form a stable population, or continue to grow forever.

    Rules of the Game of Life.
    Life is played on a grid of square cells (24 x 16 in our case). A cell (pixel) can be alive or dead. A live cell is shown by illuminating a pixel in its grid location, and a dead cell is shown by extinguishing the pixel in it's grid location. Each cell in the grid has a neighbourhood consisting of the eight cells in every direction including diagonals.

    To apply one step of the rules, we count the number of live neighbours for each cell. What happens next depends on this value.

    • A dead cell with exactly three live neighbours becomes a live cell (Birth).
    • A live cell with two or three live neighbours stays alive (survival).
    • In all other cases, a cell dies or remains dead (overcrowding or loneliness).

    The number of live neighbours is always based on the cells before the rules are applied. In other words, we must first find all of the cells that change before changing any of them. Which is why the BASIC source code uses a double buffer. The rules are applied to the shadow buffer, then transferred to the main buffer for display on the LEDs.

    Proton implementation.
    The code for the Life game is far from complex in its operation, as it must follow the strict rules of the game. Its relative complexity comes from the double buffering method that must be used, as outlined in the above paragraph.

    This is normally accomplished by creating two arrays, however, this is rather wasteful of RAM and cumbersome in operation, especially when we're using an interface that already has its display mapped to RAM. Therefore we only require a single shadow RAM area. i.e. a byte array.

    The shadow RAM is manipulated in the same manner as the display RAM, by versions of the HT_Plot, and HT_Pixel routines. These are named Shadow_Plot and Shadow_Pixel. This makes for an easier to follow program flow.

    The Proton BASIC source codes for the Game of Life can be downloaded from here: Game_Of_Life_Code.zip

    There are two versions within the folder, the first is named "Game_Of_Life.bas" and uses a fixed pseudo random number generator for its initial startup pattern. The second version is named "Game_Of_Life_Button.bas", and this uses a 16-bit timer to seed the pseudo random number generator based upon when the button is pressed. The second version also has a scrolling text start-up screen, which you may find interesting.

    A circuit for the addition of the button to the existing companion shield to Sure 2416 display is shown below:

    A layout for the above circuit is shown below:

    The Google Sketchup model of the layout, as well as the original companion shield to Sure 2416 layout can be downloaded from here:Sketchup Companion Shield with 2416 Header and Button.zip

    A video of the Game of Life running on the Sure 2416 LED display is shown below:

    Les Johnson
  • Recent Activity

    John Drew-26

    Mid$ not recognised

    Thread Starter: John Drew

    I'm trying to use Mid$ on a string using the 16F1827 which is an enhanced device. The compiler doesn't recognise the MID$. The compiler does...

    John Drew Today, 00:54 Go to last post