In the history of computer games, I think Snake might be one of the most cloned games ever. At least, it has always been my favorite game to implement when learning a new programming langauge or trying a new platform. To me, Snake is the “Hello, World” of game programming.

Even though other classics like Pong, Space Invaders and Tetris are also fun to program, I always preferred Snake for its simplicity. The game is so simple, it can be implemented in less than 50 bytes on a PC, yet even the most minimalistic version still has good entertainment value. Also, considering that you can get it on anything from calculators and mobile phones to advanced gaming consoles, I guess I can’t be the only fan.

I think one of the main reasons I like Snake as a programming exercise is because it’s an easy way of applying known algorithms and concepts in a new environment. I already know where I’m going, I just have to find out how to get there.

Most of my Snake implementations require a minimum of capabilities from the programming language or operating environment, like:

  1. Drawing characters or pixels at given coordinates on screen1.
  2. Reading characters or pixels at given coordinates on screen (not required, see below).
  3. Accepting input from keyboard.
  4. Invoking a real-time rendering loop or frequent events/callbacks.
  5. Obtaining (pseudo)random numbers (for placing food).

If I’m able to implement Snake in a new programming language, at least I know the language is not completely useless. I may not know if it’s turing complete, but at least I know it’s “Snake complete”.

Limitations

Another reason I like to program Snake in different languages is the fun of the challenge when there are limitations in your environment. If you are a seasoned programmer, doing Snake in mainstream languages like assembl, C/C++, Java, Visual Basic, C#, Python or Perl may be a trivial task. However, if you try setting some boundaries for your program, things might change. For example, Snake is an excellent candidate for size optimizing, both for smallest binary and smallest source code. Or how about programming a Snake in Excel, using the cells for “pixels”? Personally, I find the “odd ones” the most fun to write.

For example, the 4DOS batch processing language doesn’t allow reading characters from arbitrary locations on the screen. Unfortunately, the only storage space provided by the language is environment variables (and files, but I didn’t want my program to have “external dependencies”). However, the game only operates in 80x50 resolution, so the screen is small. I was therefore able to encode the entire screen into strings stored in environment variables and updating them whenever the snake moved. This allowed me to check for collisions in much the same way as reading a character from the screen would2.


snake.bat, a snake game implemented in the 4DOS batch file processing language.

I also implemented the Snake in the mIRC scripting language3, but this provides functioanlity to read pixels from the screen, so the screen array was not required.


snake.ini, a snake game implemented in the mIRC scripting language.

Extensions

If you want to go beyond the basics, you can easily extend the game with more features as your experience level with the language, toolkit or platform increases. For example, you may add things like:

  • computer opponent(s) with AI.
  • two-player or multiplayer mode (with networking).
  • sound effects.
  • improved graphics and visual effects.
  • barriers and other obstacles, like walls or enemies (extended collision detection).
  • guns and ammunition.
  • support for peripheral input devices.

So, do you have any game, algorithm or concept you like to implement to learn a new language, toolkit, platform or system? Is Snake still a usable “Hello, World” exercise for people learning programming today, or are there better ways?

Please share your thoughts on this. All comments are welcome.

Notes:

  1. OK, I have to admit, I did implement a Snake in PHP and JavaScript that would redraw the entire screen (HTML page) on the server side, but you get the idea.
  2. The tail array could also have been be implemented in this way to remove the current length limit.
  3. In case you are wondering, yes, I did have a lot of spare time when I wrote these. Both the 4DOS and mIRC snakes were written about ten years ago, when I was still in high school.