← All articles

Yes, You Can Steer the Barrels

Stand at the bottom of the first Donkey Kong screen and watch the barrels. Most roll along the girders and drop off the ends. Some turn down a ladder toward you instead. It feels random, and some of it is. But Donkey Kong players have long passed around a piece of advice that sounds like superstition: push the joystick toward a barrel as it reaches a ladder, and it is more likely to come down.

It is not superstition. The original arcade program reads your joystick when it makes that choice. This article walks through that decision in the original 1981 code.

Every ladder is a question

As a barrel rolls along a girder, the game keeps checking whether it has lined up with the top of a ladder. When it has, the barrel asks a short series of questions, and the answers decide whether it turns down the ladder or rolls on. The whole thing is one routine, at address $215F in the program ROM.

How a barrel decides whether to take a ladder A flowchart. If the oil drum is not lit the barrel goes down. If Mario is not below it, it rolls on. A difficulty roll with 1 to 3 chances in 4 must pass. Then if the stick is pushed toward the barrel, or Mario is right under it, it goes down; otherwise a last roll of 1 in 4 decides. A barrel reaches a ladder top Oil drum lit yet? (bottom left) yes no DOWN Is Mario lower than the barrel? yes no ROLLS ON Difficulty roll: 1 to 3 chances in 4 pass miss ROLLS ON Stick toward barrel, or Mario right under? no yes DOWN One last roll: 1 chance in 4 yes DOWN no ROLLS ON
The questions a barrel asks at the top of every ladder, in order.

In order:

  1. Is the oil drum lit yet? The drum at the bottom left starts dark. Until a barrel reaches it and sets it alight, every barrel takes the first ladder it finds. That is why the opening barrels of a screen always come down at you.
  2. Is Mario lower than the barrel? If Mario is level with the barrel or above it, the barrel rolls on. Barrels only chase you downward.
  3. The difficulty roll. The game takes a random number from 0 to 3 and compares it against a window that grows with difficulty. At the lowest difficulty only one value in four gets through; in the middle, two; at the top, three. A miss means the barrel rolls on.
  4. The joystick. If you are pushing the stick toward the barrel, or Mario is standing exactly under it, the barrel comes down.
  5. One last roll. If not, the game looks at two other bits of the same random number. One time in four, the barrel comes down anyway.

The difficulty in step 3 is not the level number. It is a separate value from 1 to 5 that creeps up while you play. It goes up one step roughly every 34 seconds until it reached 5, and started over at 1 each time Mario died.

The steering, in the code

Here is the joystick check, with the "toward" logic spelled out:

ld   hl,joystick    ; what the player is pushing this frame
ld   a,(mario_x)
cp   e              ; E holds the barrel's X
jp   z,down         ; Mario right under the barrel: go down
jp   nc,check_left  ; Mario to the right: is LEFT held?
bit  0,(hl)         ; Mario to the left: is RIGHT held?

The routine continues the same way: a held direction that points from Mario toward the barrel jumps to the code at $21B2 that sends it down the ladder.

So "toward" means toward the barrel from where Mario stands. If the barrel is up and to your left, hold left. If it is up and to your right, hold right. Holding the other way, or not touching the stick, leaves it to the last roll.

Putting numbers on it

Combine the steps and the odds fall out. Once a barrel is at a ladder top with Mario below it and the drum lit:

DifficultyStick left aloneStick pushed toward the barrel
11 in 4, then 1 in 4: about 6%1 in 4: 25%
2 and 32 in 4, then 1 in 4: about 13%2 in 4: 50%
4 and 53 in 4, then 1 in 4: about 19%3 in 4: 75%

Steering makes a barrel four times as likely to come down. It also has a ceiling: no amount of pushing gets past the difficulty roll. At difficulty 1 the best you can do is one barrel in four.

Two hours of barrels

Numbers from reading code are a prediction. To test them, the original program was run under a test script with a breakpoint at every step of the routine, logging where the barrel and Mario were, what the joystick was doing, the difficulty, the random number, and which way the barrel went.

Mario stayed on the bottom girder for two runs of an hour each: one with the joystick never touched, one with it held toward the nearest barrel above him the whole time. To keep the game going, the script kept his lives topped up and removed each barrel once every decision above him was made. Redoing the code's arithmetic for every logged decision gave the same answer as the barrel every time.

Measured chance a barrel takes the ladder Bar chart by difficulty 1 to 5. With the stick left alone barrels took the ladder 6, 13, 12, 19 and 19 percent of the time; with the stick pushed toward the barrel, 26, 48, 51, 75 and 76 percent. Red marks show the code's predictions of 6, 13, 13, 19, 19 and 25, 50, 50, 75, 75 percent. 0% 25% 50% 75% 100% 1 2 3 4 5 DIFFICULTY stick alone pushed toward code
What happened at the ladder tops, against what the code predicts (red marks).
DifficultyDecisionsStick left aloneDecisionsPushed toward
12,1825.6%1,14725.9%
22,94413.2%1,39448.2%
33,05111.7%1,45650.5%
43,00618.6%1,34575.1%
55,87118.7%2,61675.5%

Every row lands within a point or two of the prediction. The steering run also gave a free comparison: barrels that saw the stick pushed away from them came down 8 to 20 percent of the time, the same as leaving the stick alone. Only "toward" counts.

Standing exactly under a barrel works too, whatever the stick is doing. It just rarely happens by accident: it came up 37 times in nearly 14,000 decisions while the script chased barrels around.

Is the random number fair?

The difficulty roll and the last roll both read the same random byte, just different bits of it. If those bits were lumpy, or tied to each other, the odds above would be off. Across all 38,283 decisions that reached the difficulty roll, counting a shorter third run with Mario parked under a ladder, each of the four values in each pair of bits came up between 24.7 and 25.5 percent of the time, and knowing one pair told you nothing about the other. Where that random byte comes from in the first place is a story of its own.

One correction

The published notes this research started from say barrels only make this choice while rolling left. The code says otherwise: both directions of travel run into the same check, and barrels rolling right along the top girder made the same decision at the ladders there.

Credits and further reading

  • Kef Schecter and contributors, furrykef/dkdasm, the annotated disassembly of Donkey Kong where the ladder routine was first identified. Every address here was checked against the original ROM.