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.
In order:
- 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.
- 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.
- 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.
- The joystick. If you are pushing the stick toward the barrel, or Mario is standing exactly under it, the barrel comes down.
- 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:
| Difficulty | Stick left alone | Stick pushed toward the barrel |
|---|---|---|
| 1 | 1 in 4, then 1 in 4: about 6% | 1 in 4: 25% |
| 2 and 3 | 2 in 4, then 1 in 4: about 13% | 2 in 4: 50% |
| 4 and 5 | 3 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.
| Difficulty | Decisions | Stick left alone | Decisions | Pushed toward |
|---|---|---|---|---|
| 1 | 2,182 | 5.6% | 1,147 | 25.9% |
| 2 | 2,944 | 13.2% | 1,394 | 48.2% |
| 3 | 3,051 | 11.7% | 1,456 | 50.5% |
| 4 | 3,006 | 18.6% | 1,345 | 75.1% |
| 5 | 5,871 | 18.7% | 2,616 | 75.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.