Guess the Number example
xara-guess-the-number is the short game to copy. Players take turns guessing a number from 1 to 10, and the first correct guess wins. A wrong guess says whether it was low or high. A hint costs ₦100.
Run it
npm install
npm run dev # opens with the mock host: both players share the screen
npm test # the rules
npm run build # static files in dist/, ready to host over https
How it is built
There is no server secret. Every phone derives the same number from the session id, then replays the guesses:
export function secretOf(sessionId: number): number {
const n = (Math.imul((sessionId | 0) + 17, 1103515245) + 12345) >>> 0;
return (n % 10) + 1;
}
src/main.ts draws that after every state, and only enables the keypad when xara.isMyTurn(). The correct guess ends the game in the same call. The summary is the same for every player, so it never says "you":
const outcome = n === secret
? { winners: [me], summary: `${n} is the number` }
: undefined;
await xara.move({ n }, { outcome });
The hint
The hint is a band of at most three numbers that contains the secret. It is paid once per session: the reference is hint-<session id>, so asking again returns the same purchase instead of charging twice. After a reload, state.purchases still lists it, and the band comes back.
const paid = await xara.buy({
item: "Hint",
amount: 100,
reference: `hint-${state.session.id}`,
});
if (paid.status === "success") showHint();
Ship it
Build it, host dist/ over https, and submit its URL with 2 players.