How I built a two-player browser game without a backend
GameBites is a collection of eight browser games hosted on GitHub Pages. In one of them, you answer seven questions about yourself, send a link to a friend and see how many of your answers they can guess.
Here's how it works without a server or database.
Generating the same questions
Each round starts with a 32-bit seed. I use Mulberry32 as the pseudo-random number generator and Fisher-Yates to shuffle questions and answer options. Both browsers generate the same round from the same seed.
Language selection is independent of generation, so one player can play in English and the other in Latvian or Russian.
Encoding answers
There are seven questions with four possible answers each. Every answer is represented by an integer from 0 to 3. That's 16,384 possible combinations, which fit into three base-36 characters.
function encode(answers) {
return answers.reduce((n, x) => n * 4 + x, 0).toString(36).padStart(3, '0');
}
encode([0, 1, 2, 3, 0, 1, 2]); // "1c6"
An invitation looks like this: friend.html?v=1&seed=abcdef12&lang=en#a=1c6
The seed generates the questions, while a contains the first player's answers. Once the friend finishes the quiz, their guesses are added to the URL fragment as g. The answers never need to be stored on a server.
URL fragments are not included in HTTP requests, although anyone with the complete link can decode or change the answers.
Versioning
Changing the question pool or the generation algorithm can change the round produced by a seed. The v parameter allows the game to reject incompatible challenge links. Supporting older links after a generator change would require keeping the corresponding generator and content versions.
The project uses plain HTML, CSS and JavaScript, with tests written using Node's built-in test runner.
Comments
No comments yet. Start the discussion.