Gandhi and the Nuclear Option

A brief look at one of the most entertaining bugs in software history

In case you haven’t heard, Gandhi loves his nukes.

Yes — that Gandhi. One of humanity’s great leaders who was committed to the power of nonviolent protest and its ability to cause change in society. He loves stockpiling nuclear weapons. Or rather, he does in the virtual sense as a result of a simple software bug in one of the original Civilization simulation games. To understand how this programming error lead to such strange behavior from the computer-generated pacifist, we need to understand how computers remember numbers in memory, and how this all relates to our restless peacemaker.

In the Civilization video game series, you control the development of a single historical human civilization. These civilizations are embodied by a famous leader connected to that particular civilization’s history. Leaders have certain internal traits that define how the computer simulates their behavior during the course of a game. Certain leaders may be predisposed to gobble up as much land as possible on the map, while others may be most interested in destroying all of the other cultures in the game. Gandhi was originally programmed to have a very low aggressiveness score, and was crafted to have a very peaceful disposition — preferring to win the game in more diplomatic or cultural ways than by outright domination and warmongering.

Gandhi's aggressiveness score, like all of the leader’s traits, are represented in the computer’s memory as numbers on a pre-defined scale, ranging from 0–255. 0 is the lowest aggressiveness score a leader can have and indicates that the leader never attacks other civilizations and actively tries to set up new diplomatic arrangements in the game. 255 represents a leader who is constantly going to war, backstabbing his allies, and trying to win by outright domination and military might. Unsurprisingly, aggressive leaders attempt to manufacture nuclear weapons at the first opportunity.

Which finally brings us to the problem at hand. When computers store numbers in memory, they have a finite scale. If you go beyond the pre-defined scale (go out-of-bounds) the stored number will roll over to the lowest-possible number within the scale. You can think about it in terms of car odometer getting to the highest-possible mileage that it can accurately report — 999,999 miles. If you are somehow able to keep your car running long enough to drive 999,999 miles, the next mile you drive will cause your odometer to roll over and show (incorrectly) that your car is fresh-off-the-lot!

With Gandhi in civilization it is virtually the same thing as the really-really-old car example, but in reverse. Gandhi starts off with a naturally low aggressiveness score — lets say 20 on our 0–256 number scale. Through the course of the game, Gandhi takes more and more actions that are non-aggressive in nature. The game then starts reducing Gandhi's aggressiveness scale in response to these changes, dropping it from 20, to 10…

3…2…1…0…

255!

Gandhi's aggressiveness rolls over, and suddenly he’s gone from everyone’s best friend to making the Kim family look reserved and reasonable world leaders.

Woah dude — calm down.

This programming mistake had such hilarious consequences that the developers of Civilization have kept some form of this… tendency of Gandhi's in all subsequent releases of the game, and the internet has embraced this goof in a never-ending trove of Gandhi-Nuke memes. Let this be a lesson to all of us programmers out there though — check your variables, and guard against overflows. No good can come from them!

Further Reading