Skip to content
Tutorials Center
My learning

Topics

Lesson 118 of 132 ยท Projects

Project: Hangman and Word Guess

Build Hangman and a five-letter word guessing game in React. Store only the guesses, derive wrong letters, win and lose, add a physical keyboard, and score duplicate letters correctly.

Word games are the best practice for derived state. In Hangman you only need to remember one thing: which letters the player has guessed. The masked word, the number of mistakes, the lives left and whether you won or lost can all be calculated from the secret word and that list. The moment you start adding isWon, isLost and wrongCount as separate state, they begin to disagree with each other. Read choosing state structure if you want the theory.

In this project you will build Hangman in two stages, then a five-letter word guess game (a Wordle-style game with our own tiny word list) whose trickiest part is a pure scoring function. The word lists are short stand-ins; a real game would load a bigger one.

Stage 1: guesses are the only state

Pick the secret word once with a lazy state initializer so it does not change on re-render. Everything else is computed during render.

Stage 1: Hangman with derived state

Count the pieces of state: word (fixed) and guessed. That is all. Disabling a used letter button also gives free accessibility: it is announced as unavailable.

Stage 2: keyboard and a new game

Players expect to type letters. A keydown listener on window reads e.key. Because the handler needs the current guesses, we put them in the dependency list so the listener is replaced when they change (effect dependencies). A "New word" button resets state; changing the component's key is the neat way to reset everything at once (you might not need an effect).

Stage 2: keyboard and reset by key

Notice the functional update setGuessed((g) => ...): it avoids reading a stale list. The effect only needs won and lost, so the listener is not recreated on every guess.

Stage 3: a five-letter word guess game

Now the harder cousin. The player has six attempts to find a five-letter word. After each guess every letter is coloured: correct (right place), present (in the word, wrong place) or absent. The catch is repeated letters: if the answer is crane and you guess eerie, you must not mark every e as present. The standard rule is two passes: first mark exact matches and count the answer letters that are left over; then give "present" only while a leftover count remains.

Because this is plain logic, we write it as a pure function.

Stage 3: scoring and the board

The board is derived again: each row is score(guess, answer) computed during render, never stored. Test score('eerie', 'crane') in your head: the final e is an exact match and uses up the answer's only e, so the other two es are absent, r is present and i is absent. That is exactly why the second pass counts leftovers.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. In the Hangman stage, which values are stored in state?

    Everything else (mask, wrong guesses, lives, win and lose) can be calculated from the word and the guesses, so storing it would risk inconsistency.
  2. 2. Why does changing key={round} on Game start a fresh game?

    Component identity includes its key. A different key means a new instance with fresh state and a new secret word.
  3. 3. Why does the scoring function need two passes?

    Marking exact matches first and counting the remaining answer letters ensures a repeated guess letter is not shown as present more times than it occurs in the answer.

Summary

  • Store the minimum (the guesses) and derive masks, lives, win and lose.
  • Listen to the keyboard in an effect with cleanup and correct dependencies.
  • Reset a whole subtree by changing its key.
  • Put game rules like score in pure functions that you can reason about and test.
  • The word lists here are tiny stand-ins for a real dictionary.

Next: Project: Paint App with Canvas