The Sure Hit That Missed One Time in 256

Easy

· Frontend

Incident

SEV-3

BUG-1-IN-256

Priority: Medium · reported by players, for decades

Time to fix: 10 min

Failing checks on the shipped build

  • a 100% move hits on the highest roll

    a sure hit must not miss on 255: expected true but got false

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the value the screen shows back through the state and the pure helpers that compute it; the view is usually only the messenger.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Inspired by

Pokémon Red/Blue · 1996

Modelled on the first-generation Pokémon games (Red and Green in Japan in 1996, Red and Blue abroad): the accuracy check drew a random byte from 0 to 255 and hit only if it was strictly less than the move's accuracy scaled to that range. Scaled accuracy tops out at 255, so a roll of 255 always missed — even a "100% accurate" move missed one time in 256.

This reconstruction is the battle screen's hit check, with the random source injected.

Fix willHit so a 100%-accurate move always hits.

Code map

var rules = require("./rules");

// Accuracy is a percentage, scaled to the range of the random byte.
exports.threshold = function (accuracyPct) {
  var t = Math.floor(accuracyPct * rules.MAX_ROLL / 100);
  return t > rules.MAX_ROLL ? rules.MAX_ROLL : t;
};

exports.willHit = function (accuracyPct, rng) {
  return rng() < exports.threshold(accuracyPct);
};

exports.hitsOutOf256 = function (accuracyPct) {
  var hits = 0;
  for (var roll = 0; roll <= rules.MAX_ROLL; roll++) {
    if (exports.willHit(accuracyPct, function () { return roll; })) hits++;
  }
  return hits;
};

Sign in to edit the file, run the tests and submit a fix

A free account — no card, no trial clock. You come straight back here.

The Sure Hit That Missed One Time in 256 — Bug Hunt

Modelled on the first-generation Pokémon games (Red and Green in Japan in 1996, Red and Blue abroad): the accuracy check drew a random byte from 0 to 255…

  • Language: JavaScript
  • Layer: Frontend
  • Difficulty: Easy
  • Concepts: Randomness, Games
  • Modelled on: Pokémon Red/Blue · 1996
  • Visible tests: a 100% move hits on a low roll; a 100% move hits on the highest roll
  • Reward: 50 XP for a complete fix

Incident

  • Severity: SEV-3 (priority: Medium)
  • Ticket: BUG-1-IN-256
  • Reported by: players, for decades
  • Time to fix: 10 minutes, the target for an easy incident

Failing checks on the shipped build

  • a 100% move hits on the highest roll — a sure hit must not miss on 255: expected true but got false

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the value the screen shows back through the state and the pure helpers that compute it; the view is usually only the messenger.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Briefing

Modelled on the first-generation Pokémon games (Red and Green in Japan in 1996, Red and Blue abroad): the accuracy check drew a random byte from 0 to 255 and hit only if it was strictly less than the move's accuracy scaled to that range. Scaled accuracy tops out at 255, so a roll of 255 always missed — even a "100% accurate" move missed one time in 256.

This reconstruction is the battle screen's hit check, with the random source injected.

Fix willHit so a 100%-accurate move always hits.

Bug report

BUG-1-IN-256 · Priority: Medium · Reported by: players, for decades

willHit(accuracyPct, rng) — rng() returns an integer roll in 0..255.

  • accuracyPct >= 100: always hits, and rng is NOT called (the roll is not drawn)
  • otherwise: draw exactly one roll; hit iff roll < threshold(accuracyPct), where threshold(p) = floor(p * 255 / 100)

hitsOutOf256(p) counts the hits over every possible roll: 256 for a 100% move, 127 for a 50% move.

Observed: a 100% move misses when the roll is 255.

Logs

[battle] TACKLE acc=100 roll=255 -> MISSED

The code as shipped

src/battle/accuracy.js (editable)

var rules = require("./rules");

// Accuracy is a percentage, scaled to the range of the random byte.
exports.threshold = function (accuracyPct) {
  var t = Math.floor(accuracyPct * rules.MAX_ROLL / 100);
  return t > rules.MAX_ROLL ? rules.MAX_ROLL : t;
};

exports.willHit = function (accuracyPct, rng) {
  return rng() < exports.threshold(accuracyPct);
};

exports.hitsOutOf256 = function (accuracyPct) {
  var hits = 0;
  for (var roll = 0; roll <= rules.MAX_ROLL; roll++) {
    if (exports.willHit(accuracyPct, function () { return roll; })) hits++;
  }
  return hits;
};

Read-only context: src/battle/rules.js.

Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More JavaScript bug hunts.