The Incredible Adventures of Rattles McCoil

Bob l'eponge avec serpent!

Oh! ♫ Who lives in a ♪ pineapple under the ♪ sea, SpongeBob ♬ SquarePants…

Imagine this scenario. You are sitting in front of the TV watching the Spongebob Squarepants movie1 and you’re a little peckish. In fact, what you could really, really do with for that authentic movie experience is some popcorn. No problem! In your cupboard, you have microwave popcorn. So off you trot to the kitchen (pressing pause because it’s a particularly funny scene where Mr Crabs is about to say ‘with cheese, Mr Squidward, with cheese’), slap it in the microwave and hang around for a couple of minutes until it’s done. Nice job! So for maximum efficiency, you now put the popcorn in a bowl and leave it in the kitchen. Then, whenever you want a bit of popcorn, you walk into the kitchen, get one piece, go back to the movie, eat it, rinse, repeat. What? Inefficient, you say? You’d have taken the bowl with you? Of course you would. Which is why this post on Stack Overflow seemed so staggering. A colleague suggested it might be a setup for the Daily WTF, which I think we all hope it is, but are tainted having actually seen production code in actual products that’s as bad, if not worse, than this which is, at least, from a beginner. Furthermore, the guy in question has asked several such questions so… well, who knows. In a nutshell, the chap is writing an adventure game. He wants to show a map, just in simple text, with an X marking the location of the player. The map is 13 x 10 grid and has, therefore, 130 locations. Here’s an example of a line:

[X][ ][ ][ ][ ][ ][ ][ ][T][ ][ ][ ][ ]

I thought that the Ts were trees. Which would have been nice, because Ts do actually look like trees, but it turns out that they represent towns. His solution, and this is almost unbelievable, is to repeat the code that shows the map one hundred and thirty times. Then, using “if my location is 3, 4” type programming constructs he chooses which one to show.

Thanks to wonderful resources such as stack overflow, instead of allowing his natural programming spidey sense to develop, he simply posted an example of one instance of the map display with this as the money-shot:

“I have the map typed out 130 times in 130 different if statements, each time the X is somewhere else. I would think there would be a better way to do this, but I have no idea.”

No idea? What, none? You didn’t have even the smallest alarm bell dinging about, say, 50 copy-pastes in to this massive affair? You completed the entire exercise of creating 130 repeats of the same block of code with a minor, predictable difference without thinking “perhaps I could do this with a loop, or something”? You had to ask on an Internet resource with absolutely no clues at all? Did the brain even warm up? Or did sheer laziness prevent any thinking whatsoever? For crying out loud, this is worse than cutting your lawn with nail clippers and then, having finished, observing to your neighbour that it is “amazing that there isn’t a better way of doing this”.

It’s wonderful that people are learning to program. It’s a valuable, interesting skill that — in theory — teaches all sorts of problem solving skills but for those to develop, they need to be practiced. This means attempting to figure it out for yourself. When you finally reach a dead end, show your working when asking for help (he was asked “what have you tried?” to which he replied “I’ve not really tried anything yet”, emphasis mine.). It’s OK to ask for help, but the assumption is that you’ve at least made an effort that can be quantified without requiring an electron microscope to find it. As an added bonus, people can advise you on your problem solving and how you might save some time later. It’s win, win and win. You don’t win friends with bone idleness, just as you don’t with salad. Laziness should neither be encouraged nor tolerated: I genuinely hate to appear negative to someone who is ‘learning’ programming, but this is programming by proxy, or, more specifically, programming by piecing together other peoples’ solutions to your problems.

A while back, I said I was going to do a C++ tutorial via the medium of writing a 1980s style text adventure game. Needless to say, I completely failed to get around to it. This isn’t from lack of desire, it’s from lack of time (plus only one person was pestering me and not nearly often enough – you know who you are, Montaigne). However, since I’ve had a draft of this hanging around on my hard drive for the best part of nearly a year, I figured it was worth brushing it down, beating it into shape and slapping it onto the blog to make it look like I’m doing regular updates.

So let’s crack on.

Obviously, we won’t be worrying our fragile little minds with minor details like a schedule or a proper design document because we’re making this up as we’re going along. Furthermore, this is an exercise in general amusement that may or may not last longer than a few parts depending on whether it turns out to be as fun as I imagine it to be (particularly since most of that imagination takes place after a gin and tonic and some glasses of wine.) Still, we can’t simply write code blind, so we’re going to need something to start from: A plot! Here it is. Have some tissues handy, it’s a real tear-jerker:

“Rattles McCoil is a rattlesnake. He lives in the jungle with his wife-snake and three children-snakes. Every day he goes out to hunt for food to feed his family: shrews, mice, that sort of thing. Unfortunately, one day, he was slithering around the jungle when suddenly… an evil exotic pet dealer leaps out from behind a tree and captures him! Oh no! Shoved into a dark, frightening bag, he rattles away but no-one can hear him. The next thing he knows he’s bumping along on the back of a jeep away from his home, friends and family. Then, when it looked like all hope was lost, the jeep hits a large pot-hole and comes screeching to a halt with a broken wheel. Rattles notices that his bag has become untied with the bumping and quickly, he slithers like he’s never slithered before being extra-specially careful not to rattle as he escaped. But Rattles is a long, long way from home. Can you guide him back? CAN YOU? Of course you can.”

You’ve got to admit, it’s genius. We’ll also need a rough map to work from – something that describes the lie of the land and the general location of everything that we’ll use to build our adventure game. Here’s what I’ve got so far:

So many wonderful places to explore, but much more importantly, features an OXBOW LAKE! Yeah for my ‘B’ in O Level Geography! I knew it would come in handy eventually.

Eh? Eh? Honestly, I’ll bet you’re gagging to play this game now. Who wouldn’t want to traverse the Marsh of Crocodoom or the Plains of Waiting Disaster? And those Shrubs of Terror are particularly inviting. Of course the map features a plateau and a volcano. All good adventure games have volcanoes, because they’re cool and they feature deadly lava.

Finally, we need at least some form of specification even if it is “specification-light” to guide us in the early development. This need not be in any way comprehensive. Which is good, because this is possibly the least comprehensive design document ever produced by mankind2. Here’s the product summary:

“Text adventure game as they used to be done. Simple verb-noun commands such as ‘slither north’, ‘eat shrew’ and ‘rattle’. Adventure game will consist of a hundred or so “rooms” and thirty or forty objects that can be used in a variety of different ways. An effectiveness score is calculated based on the efficiency of the user’s journey through the optimal path. Various puzzles need to be solved and some solutions require the player to find and assemble things whereas others are traditional mazes. A very simple fighting system provides a little more excitement than a simple ‘fight giant spider’ and comes with various augments such as armour, specialised weapons (fangs, poison) and food based bonuses. A sense of urgency will be provided using other game characters that autonomously attempt to hunt Rattles wherever he is. Scattered around the gaming environment will be a selection of treasure chests, keys and other relics that Rattles can take home to complete the adventure with additional style and satisfaction!”

It’s also kind of nice to have some idea as to what we might do with it in the future after the first release, in the unlikely event that there is a first release:

“Enhance the game with improved puzzles and basic graphics to illustrate the various scenes. Allow for the possibility of multi-player where more than one player can take on roles inside the adventure at once. Look towards creating an iOS version so that it can be made available on the Apple App Store”.might take this as the days, months, years and generations roll forwards. If nothing else, it lets us avoid accidental closing of doors by designing for a future regardless of whether we make it there or not.

Armed with our plot, the map and the “design” we’re about ready to begin laying down some code, which is a shame, because we’re flat out of time on this episode of “Stringing it out for years”. But next time, next time we’ll code like there is no tomorrow. Bring a toothbrush. And wine. Here’s a “hello world” program to get you going because, well, it wouldn’t be quite fair to have a programming tutorial that had no software in it at all, now, would it?

#include <iostream>

int main(int argc, const char* argv[])
{
    std::cout << "Hello, giraffe and snake lovers!\n";
    return (0);
}

In the meanwhile, if you're planning on following this tutorial (and you'd better be really, really patient to do so) then you might want to get yourself sorted with a C++ compiler and see if you can get the above to compile and run as a command line program. If you can, you're ready to start putting together the adventure of a lifetime, saving Rattles McCoil from doooOOOooOOOoom!

  • Windows users, try Visual Studio Express.
  • Mac users, install Xcode from the App store.
  • Linux users, well, you don't need my help, you probably already compile half your applications 😉

Anyhoo, tune in spring 2014 for the next gripping part of this series, perhaps earlier if anyone pesters me about it sufficiently.

Oh, and go and watch the Spongebob Squarepants movie. With or without popcorn.

-
1 It's a fantastic movie. Even if you're not a Spongebob fan (which you should be), it's a really fun watch.
2 This is not true. I have personally worked on major titles that have had no specification at all and two of them were Amiga titles that I was the only programmer on. Sometimes, someone knows the design so well in their head that it “just happens”. On the other hand, other titles have had virtually no design and have ended up a complete catastrophe of misplaced dreams, hopes and ideas. Like porridge, it’s too easy for it to be “too hot” or “too cold”. Just right, on the other hand, is tough. Getting it wrong incurs the wrath of bears, so be careful out there.

This entry was posted in Software development and tagged , , , , , , , . Bookmark the permalink.

8 Responses to The Incredible Adventures of Rattles McCoil