The Kill Screen at Level 256 — Java Bug Hunt

Modelled on Pac-Man's level 256 "kill screen" (Namco, 1980): the game keeps its level counter in a single byte.

  • Language: Java
  • Layer: Frontend
  • Difficulty: Medium
  • Concepts: Overflow, Rendering
  • Modelled on: Pac-Man · 1980
  • Visible tests: level 1 draws one fruit; level 256 draws seven fruits and leaves the maze alone
  • Reward: 50 XP for a complete fix

Briefing

Modelled on Pac-Man's level 256 "kill screen" (Namco, 1980): the game keeps its level counter in a single byte. On level 256 the routine that draws the fruit row at the bottom of the screen overflows — its fruit count wraps to zero, the loop that counts down to zero runs 256 times, and it writes garbage over the right half of the maze, leaving the level impossible to finish.

FruitRow.java reconstructs that routine against a small screen model (MazeScreen.java): slots 0–6 are the fruit row, and any other slot lands on maze tiles.

Fix FruitRow so every level draws at most seven fruits and nothing ever touches the maze.

Bug report

BUG-LVL256 · Priority: High · Reported by: a very good player

levelIndex is 0 on level 1 (level 256 is levelIndex 255; levels past 256 must work too).

  • fruitCount(levelIndex) = min(levelIndex + 1, 7), for every levelIndex >= 0
  • draw(screen, levelIndex) puts exactly fruitCount(levelIndex) fruits into slots 0 .. count-1 and returns that count; screen.corruptedTiles stays 0

Observed: on level 256 draw() returned 256 and 249 maze tiles were overwritten.

Logs

[game] level 256 start
[render] fruit row: 256 sprites drawn
[render] maze tiles overwritten: 249

The code as shipped

src/game/FruitRow.java (editable)

class FruitRow {
    // levelIndex is the game's level counter: 0 on level 1, kept in one byte.
    static int fruitCount(int levelIndex) {
        int n = (levelIndex + 1) & 0xFF;
        return n < MazeScreen.FRUIT_SLOTS ? n : MazeScreen.FRUIT_SLOTS;
    }

    static int draw(MazeScreen screen, int levelIndex) {
        int n = fruitCount(levelIndex);
        int slot = 0;
        do {
            screen.putFruit(slot, levelIndex - slot);
            slot++;
            n = (n - 1) & 0xFF;
        } while (n != 0);
        return slot;
    }
}

Read-only context: src/game/MazeScreen.java.

Open the hunt to edit the files, run the visible tests and submit against the hidden ones. More Java bug hunts.