Tag Archives: Socket.io

Game of Life in NodeJS

Last weekend I went to Brisbane coding event called code smash. It coincided with node knockout. The event was provided us with a space to work, people to work with and pizza. What we did at the event was up to us. In the spirit of node knockout I was keen to do some serious team coding and get results. Luckily I managed to team up with Andrew who had similar goals. Andrew works with ruby on rails full time and I work on asp.net mvc full time. So clearly the sensible thing to do was to write a Game of Life in NodeJS. Just to keep us on our toes.

We had slightly less than 5 hours. So we decided to take a client side javascript version of the game of life that was already written and make it shared from the server side. We got it running on the server first. That was pretty straightforward as the client side javascript was well-written. Then we got the game displaying as quickly as possible by having clients poll the server. Then we added the ability for formations of cells to be added to the game, so it would be more interesting. Andrew seemed to know a bit about the Game of Life, and was aware of interesting formations that would travel. So we added some of these as possible formations that could be added to the game. We got it working and had something to show; we were happy with the day.

This weekend I was still energetic about the project. So I

  • Changed the polling to push based sockets using socket.io;
  • Made the packets a smaller;
  • Made the field size as big as I could;
  • Switched out the heavy duty game library and did the painting with jCanvas; and
  • Gave our Game of Life a home.

I’m not usually a big fan of games like I used to be back in the day. This is probably because I lack the time. This game is a set-and-forget sort of game, there’s no big time investment required. I’m still thinking about how to make it more interesting by adding to the multi-player aspect of the game.

The game suffers from entropy over time and relies on people visiting to add cells to make the result dynamic and interesting.

I have though of a few other interesting possibilities, that could still be explored:

  • Calculate a big field and be able to zoom and scroll around parts of it.
  • Be able generate a still image of a big field frame of the game. Maybe keep some cell history and heat map the image if it’s sparse.
  • Introduce colours to the cells that mix as cells reproduce.
  • Introduce a multi-player aspect where each player can add spawn points that emit predetermined cell formations.
  • Make the server send only the unpredictable state updates, and get the clients to do the other predictable calculations related to display.
  • Be able to have an infinite sized field that scales as servers are added to calculate more of the field.

The code is on github.