New React Challenge: Numbers Game
Six tiles, four operators, one target. Instead of scattering a game across a dozen useState calls, this challenge asks you to keep the whole round in a single useReducer — then add a stopwatch that keeps ticking until you win.
🧩 Overview
Build a Countdown-style numbers game. Combine six number tiles with +, −, × and ÷ to reach a random three-digit target while a stopwatch measures your time. Along the way you'll practice useReducer for multi-field game state, a self-scheduling effect, and truncating a step history when a step is deleted.
👉 https://www.reactchallenges.com/challenges/numbers-game
✅ Requirements
- Implement the game in
App.tsxandreducer.ts.NumberTile.tsx,OperatorPad.tsx,StepsList.tsx,utils.tsandindex.cssare read-only. - The app starts idle:
start-gamereads "Start",targetreads---,timerreads0s, no tile is rendered, neithererrornoroutcomeis present, and the board shows sixempty-tileplaceholders. - Clicking
start-gamestarts a round: it disappears, the placeholders become exactly sixtilebuttons, andtargetshows the generated integer from 100 to 999. -
timercounts elapsed seconds up by one each tick, starting from0s. - Each
tileis a<button type="button">withdata-valueandaria-pressed; clicking selects it, clicking again deselects it. - The four operator buttons show
+,−,×,÷with accessible labels, and the active operator is reflected inaria-pressed. - With one tile selected and an operator chosen, clicking a second tile combines them: both operands leave the board, the result becomes a new tile, and a step such as
25 × 3 = 75is added. - Addition and multiplication always combine; subtraction only when the result is positive, and division only when it is exact with a non-zero divisor.
- An invalid combination shows an
error, keeps every tile, adds no step, and clears the pending tile and operator; the error disappears as soon as the next selection starts. - Reaching the target exactly wins the round: the counter stops, tiles and operators are disabled, an
outcomeis shown, andstart-gamereads "Play again". - Every step has a delete control at
delete-step-{index}. - Deleting a step removes that step and every step after it, and rebuilds the board to the state it had before that step.
- Deleting the first step clears the history and restores the six starting tiles; deleting the last step removes only that step.
- After a finished round,
start-gamestarts a fresh round with six tiles, a0sclock, and no steps, outcome or error.
💡 Notes
- Keep the entire round in
reducer.ts: tiles, target, steps, pending tile, operator, error, elapsed time and status. One reducer avoids impossible states like a "won" round with a running clock. - Store the six starting tiles. Deleting a step then becomes "truncate the steps to a prefix and replay it from the starting tiles" — no inverse arithmetic needed.
- Use a self-rescheduling
setTimeoutand clear it on cleanup, and stop scheduling once the round is no longer playing so a late tick can't move the clock after a win.
🧪 Tests
- starts idle with a hidden target, a zeroed clock, and no tiles
- starts a round with six tiles and a three-digit target
- counts elapsed time up one second per tick
- selects a tile and deselects it when clicked again
- marks the chosen operator as pressed
- combines two tiles with addition and records a step
- combines two tiles with subtraction and records a step
- combines two tiles with multiplication and records a step
- combines two tiles with exact division and records a step
- rejects a subtraction that would go negative
- rejects a division that is not exact
- clears the error when the next selection starts
- rejects division by zero and other non-positive results
- wins when the target is reached, freezing the clock and input
- shows a delete control on every step
- deleting a middle step removes it and every later step
- deleting the first step clears the history and restores six tiles
- deleting the last step only removes that step
- starts a fresh round with a clean board after playing again
By the end you'll have a clean mental model for reducer-driven game state, effects that reschedule themselves, and history you can rewind without writing a single inverse operation.
原文:https://www.a8q9.cn/en/article/new-react-challenge-numbers-game-247278.html