Whack-a-Mole
UI Coding
Medium
35 min
games
timers
effects
state
accessibility
Build a whack-a-mole game. A 3 × 3 field of holes is shown. While the game runs, moles pop up in random holes, and the player scores a point for each mole they hit before it disappears.
The starter renders the static field, a score and a Start game button, and exports a WhackAMole component.
API
export function WhackAMole({
holes = 9,
duration = 15000, // length of one game in ms
moleInterval = 900, // how often a new set of moles appears
random = Math.random, // injectable for deterministic tests
})
Requirements
- Start game begins a round: the score resets, moles appear immediately, and a new random set of 1–2 moles replaces the old set every
moleIntervalms. - Hitting a hole that has a mole adds 1 to the score, and that mole disappears at once, so it can't be hit twice. Hitting an empty hole does nothing.
- Show the time left in whole seconds, counting down while the round runs.
- After
durationms the round ends: all moles disappear, the holes stop responding, andTime's up! Final score: Nappears. The button then reads Play again. - Every timer must be cleaned up when the round ends or the component unmounts. Leaked intervals keep moving moles after the game is over.
- Accessibility:
- Each hole is a button named
Hole 3, moleorHole 3, empty. - Keys <kbd>1</kbd>–<kbd>9</kbd> hit the matching hole (laid out like a phone keypad: 1–3 on the top row), so keyboard users can play at speed.
- The final result is announced (
role="status"). - Respect
prefers-reduced-motionfor the pop-up animation.
- Each hole is a button named
Follow-up
- Moles that stay up for a random, shrinking time as the game goes on.
- Why does a countdown built from
setInterval(…, 1000)drift, and how do you make it accurate? - What happens to your timers when the tab is in the background?
Spotted a mistake or a rights concern? Report an issue