Bug Parade: When Gandhis Attack

Sometimes we can learn a great deal about how to do things right from the times when things went wrong. Welcome to a new occasional series: The Bug Parade!

Screenshot from Civilizaiton 1: Mahatma Gandhi, leader of the Indians, says "Very well, we will mobilize our armies for WAR! You will pay for your foolish pride!"
Gandhi attacks

The Civilization series is one of the most enduringly successful computer strategy games of all time. The games let you control a human civilization, starting in 4000 B.C. and guiding them into the 21st century. You control all aspects of your civilization, from military expansion to scientific advancement to keeping your citizens happy. Along the way, you encounter other civilizations and their leaders. You can go to war with these civilizations, make peace, manage alliances, and negotiate with their leaders. The series is one of the most incredibly addictive, just-one-more-turn strategy games ever created.

It all started in 1991 with the original game of that name, now informally known as Civ 1, created by legendary game designer Sid Meier. Nearly 30 years later, the original game still has a devoted fanbase, who not only still play the game but also dig deep into the game’s code to create fan mods to play and share.

Creating a game that people still enjoy after 30 years is an amazing achievement on its own, but it becomes absolutely mind-blowing when you realize that the entire game takes up just 2.6 megabytes of space, less than a single song stored in MP3 format.

Size comparison: the entire game (2.4 Megabytes) against a single MP3 (3.4 Megabytes)

Creating such a complex game in such a tiny amount of storage space necessarily meant that many compromises had to be made – and from one of these compromises came one of the most famous software bugs in history.

In 1991, the Internet had yet to penetrate into public consciousness, and so the idea of playing a game online was still new. Meier knew this, and knew that Civilization presented enough of a design and programming challenge already, so he decided to keep it firmly a one-player strategy game. And that meant that the quality of the game would depend on the quality of the player’s computer-controlled artificial intelligence (AI) opponents. If the AI opponents made good decisions, the single human player could respond to the challenges presented with decisions of their own.

But that was the challenge – how could the game developers make convincing AI for opposing world leaders while making the entire game fit into an absurdly low 2.6 megabytes? All opposing leader decisions where based on a set of simple formulas. Will Hammurabi offer a peace treaty? Check the formula. Will Mao Tse Tung launch a nuclear weapon? Check the formula. And to account for differences between leaders, the input to the formula was a simple scale from 1 to 5, ranging from most peaceful to most warlike: Mahatma Gandhi scored 1, Genghis Khan scored 5.

But there was another game design point to consider. One of the things that made Civilization so groundbreaking was that it had multiple ways to win. You can win by conquering the entire world, by winning the space race, or by having the highest score when the game ends in 2100 AD. To reward the player for sticking around for the modern era, – as well as to reflect historical experience – opponents become less aggressive. Specifically, each leader’s aggression rating drops by two.

But if aggression drops by two, what happens to Gandhi, who already had an aggression rating of one? Logically, his aggression rating should drop to -1, making him even more peaceful than he was already. But if logic prevailed, we wouldn’t be talking about this in a recurring series called The Bug Parade.

Remember that with only 2.6 megabytes to work with, programmers were desperate to save space, so they used every strategy they could think of to make their code use memory efficiently. Since they knew the aggression rating went from 1 to 5, they used only one byte of memory to store the value. One byte of memory can store values from 0 (in binary, 00000000) to 255 (in binary, 11111111). You can probably already see the problem with this.

If the program could only store values from zero to 255, and it had to calculate 2-1, what was it supposed to do? The only thing that made any sense at all was for the odometer to turn over, for the calculation to wrap around, and to return the result 255.

Subtracting 1 from 00000000 (equal to zero) gives 11111111 (equal to 255)
Illustration of an integer underflow error

Thus, as soon as the player discovers Democracy, Gandhi’s aggression jumps to 255.

Out of five.

And so as soon as Gandhi gains the ability to launch nuclear missiles, he immediately launches them at everyone on the next turn, and on every turn after that until he is dead or everyone else is dead.

The bug was never caught in testing, but of course as soon as the game shipped, millions of players discovered it, and it became legend. The bug was quickly fixed in the sequel, but by that point “Nuclear Gandhi” had become enshrined in popular culture.

Memes weren’t really a thing in 1991, but “Nuclear Gandhi” was an inside joke among computer gaming fans for literally decades. And as soon as memes became a thing, Nuclear Gandhi memes became a thing too. Here’s a mashup of Nuclear Gandhi with another famous meme, Der Untergang subtitles:

Coding the game in such a way as to allow a variable underflow error wasn’t even really a mistake, just a design decision that had consequences that were unexpected – and hilarious.

But that’s the nature of the parade – the bugs show up when you least expect them.

I hope you enjoyed this light-hearted introduction to where software bugs come from and what they can do – because when the Bug Parade returns, the next one will be pure nightmare fuel.

Leave a Reply