Build a drawing game · Exercise 03

Draw and guess

Turn guesses into a game: rounds, a win rule you choose, and a page that copes with slow or failed guesses.

Draw and guess

Your app can guess a drawing. Now make it a game: a word to draw, a way to win, and a next round. Then make it hold up when the network doesn’t.

What makes it a game

A round has a target word. The player draws it and presses Guess. If Clef’s guess matches, by a rule you choose, they win and go again. That rule is yours to decide, and it changes how the game feels: a strict rule is a challenge, and a loose one is friendly for young kids.

The highest bar isn’t always right

Clef always has a top guess, even for an empty canvas. The probabilities only cover the words on your list, and they always add up to 100%, so something has to come first. When we sent an empty canvas with an eight-word list, Clef said “person” with 34%. It wasn’t seeing a person: there was simply nothing better on the list.

That’s why “the top guess is the target” isn’t enough on its own. There are three ways to make winning mean something, and you can combine them:

  • A threshold: the target has to be the top guess and above, say, 60%. On that empty canvas, 34% wouldn’t win.
  • A “nothing” word on your list, described as “Nothing recognizable yet: a blank canvas or a scribble”. With one, the empty canvas came back “nothing” at 63%. A random scribble still came back “tree” at 24%, but with “nothing” close behind at 20%, so a threshold still helps.
  • A second question in the same request. Clef can answer several questions about one drawing at once, so you can also ask a noul question: is the drawing finished enough that a person would recognize it? The empty canvas scored 6% and the scribble 16%, while a clear drawing of a house scored 86%.

When a guess is slow, fails, or arrives late

A guess usually takes under half a second, but your game shouldn’t assume it will. Three things can go wrong:

  • It’s slow, or never comes back. Give up after a few seconds and say so.
  • It fails, because the network drops or the Worker returns an error. Show a message, and let the player try again.
  • It arrives late. A player presses Guess and then Skip. The answer for the old drawing arrives after the new round has started, and shows up under the wrong word.

The fix for the last one is a round counter: remember which round a request belongs to, and ignore the answer if the round has moved on:

app/App.tsxconst sentInRound = round.current;
const res = await fetch("/api/guess", {
  method: "POST",
  headers: { "content-type": "application/json" },
  body: JSON.stringify({ image }),
  signal: AbortSignal.timeout(8000),
});
const data = await res.json();
if (sentInRound !== round.current) return; // this answer belongs to an earlier round

Prerequisites