A Tribute to Snake

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 80×50 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.
Be Sociable, Share!
Share

4 Comments

  1. Posted July 29, 2008 at 09:29 | Permalink

    It’s not Snake, but I just found an implementation of Tetris in sed. Pretty awesome. It’s cool to see I’m not the only one who enjoy implementing old classics in odd languages.

  2. bryan
    Posted September 13, 2008 at 21:22 | Permalink

    Good day Sir…!,
    can you give me the code of the simple snake program.., please i really need it for my project..!, thanks…

  3. Posted September 13, 2008 at 23:20 | Permalink

    @bryan I only have the 4DOS and mIRC implementations posted here. All my other versions were lost in a hard drive crash years ago.

  4. Dave Benham
    Posted November 14, 2013 at 04:16 | Permalink

    You may be interested in a pure Windows batch implementation of SNAKE at http://www.dostips.com/forum/viewtopic.php?f=3&t=4741. No 3rd party tools or hybrid scripting was used.

    The biggest trick (hack) was coming up with a non-blocking technique for detecting a key press that works on all modern versions of Windows from XP onward. (XP does not have CHOICE). There are actually two CMD.EXE processes running in the same console window, one is the game itself with all logic and screen output, and the other is a controller for user input. The processes communicate via a pair of text files. The controller detects key presses in a blocking way using XCOPY of all things! Each key press is written to an input file. The game can check to see if a new key has been written to the input file without blocking – it proceeds with or without a new key press. The game sends commands to the controller via a command file.

    One other major technique that helps provide good performance is the use of “macros” – code stored in environment variables. I know simple batch “macros” is an old technique. But at DosTips we developed a sophisticated method to create complex macros that take parameters! http://www.dostips.com/forum/viewtopic.php?f=3&t=1827

    I hope you take time to download the game, give it a try, and examine the code. I think you will enjoy it!

    Dave Benham

3 Trackbacks

  1. By The Programming “High” « loop label on August 12, 2008 at 23:34

    […] at least one of the two. Most of the time you can get both. Even a seemingly trivial task, like creating a simple snake game, can be challenging if you want it to […]

  2. By How I Got Started Programming « loop label on August 21, 2008 at 21:20

    […] Again, it’s hard for me to remember, since it’s so many years ago, but I do remember writing a rather long text adventure game that could be played with multiple outcomes and various ways to complete the game. Later on I also wrote more graphics-oriented games, usually cloning classics like Space Invaders, Pong and Snake. […]

  3. […] but often I find it easier and more convenient just to simulate the behavior I want. Over the years I have been playing around with a fair share of odd programming environments, so here I present you with a collection of some […]