Life on a Character LCD

I just wrote about a project over at Hackaday where a 16×4 character LCD was used to play Conway’s Game of Life. I’m fond of the game, and have dabbled in programming it myself. I thought it would be fun to take the idea and make the game board a bit bigger.

I’ve divided each character into two pixels by using custom characters. Originally I wanted to use four pixels per character, but the HD44780 protocol only allows for 8 customs and that’s not enough to map every possible combination of four pixels.

But what I ended up with works quite well. I used an Arduino with the LiquidCrystal library which comes with the IDE. The rows and columns wrap so that the game keeps going when it reaches the edge of the screen. I do seem to have one bug in my code which keeps generating garbage at column 0. I’m probably not going to take the time to squash this. It’s not a huge deal, and it keeps the game from becoming stagnant.

Check out my code repository if you’re interested.

Terminal Life

Terminal-life

After completing my first iteration of Conway’s Game of Life I was unhappy with the performance. After a bit of testing I discovered that the real bottleneck wasn’t in my algorithm, but in the overhead associated with the pygame module that was taking care of the graphics.

 

I just completed a branch of the code that does away with the pygame module. Instead, it runs in a bash shell. The program uses tput to read in lines and columns available in the terminal, and also to move the cursor back to the top left before each new generation is drawn. Of course the limitation here is that it only works on Linux. I was able to maintain the look by using a Unicode character which draws a box. That’s a screenshot of the code running.

Conway’s Game of Life

I finally got around to programming Conway’s Game of Life. I’ve long wanted to give this a try but just today decided to take some time to myself and actually do it. I chose Python, a language I’ve worked with quite a bit but one I’ve never used for GUI programming. I spent the majority of my time trying to figure out how to display the Life grid, and decided to use a package called pygame, which I enjoy quite a bit!

About the code:

The game itself was actually pretty easy to code. I decided to make a multidimensional array as a lookup table. The first dimension is indexed by whether the current cell is dead (0) or alive (1). The second dimension is indexed by the sum of the living cells around the test cell. The return value is status of the test cell after the rules are applied for the next generation. The rest is just iterating through the various buffer arrays and then writing to the display.

The number of cells, cell size, gap between cells, delay between generations, and percent of live cells atĀ genesisĀ are all configurable. The game checks for stagnation at the end of evolution and will change the window title to show how many it took to reach equilibrium.

The pygame package turned out to be very easy to work with. I has an event handler that takes care of the delay time between generations. Take a look at it if you are ever working on a game!

I’d love to hear your thoughts about my code. Check it out and then leave a comment or send me a tweet!

Source Code Repository

Follow Me:

@szczys