On kanto-tactics.com/team-code, players can build a 28-cell board (4x7) and generate a shareable code. The site promises "Nothing is uploaded" and that the code can be "decoded by eye."
Encoding a board isn't just JSON.stringify. If you do that, you get a massive string. If you Base64 it, it's opaque.
The State Space
A single cell can be: empty, or a unit with a species ID (1-69), a star level (1-3), and an item (0-15).
Instead of storing objects, we can represent a cell as an integer using Bitwise Operations or a custom base-N encoding:
- Species: 7 bits (up to 128)
- Star: 2 bits (up to 4)
- Item: 4 bits (up to 16) A cell fits into 13 bits. A 28-cell board is just 364 bits—less than 46 bytes.
We can serialize this into a string by mapping every 6 bits to a specific character from a safe alphabet (A-Z, a-z, 0-9, -, _). This is exactly how Base64 works, but we define our own alphabet to make the string shorter and omit problematic characters.
The Decoder Must Be Defensive
Because the code is user-generated and pasted, it's an XSS surface. If a user pastes a 10,000-character string, your decoder shouldn't lock up the browser trying to parse it.
function safeDecode(code) { if (!code || code.length > 50) throw new Error('Invalid length'); // Fail fast const cells = []; for (let i = 0; i < code.length; i++) { const val = ALPHABET.indexOf(code[i]); if (val === -1) throw new Error('Invalid character'); // ... extract bits and push to cells } if (cells.length !== 28) throw new Error('Incomplete board'); return cells; } Why "Decoded by Eye"?
Making the format visible (even if slightly longer than pure binary) allows for debugging. If a player reports a bug, a developer can look at the string and see if cell 14 has an impossible unit ID, without writing a custom parser script.
You can test the encoder at kanto-tactics.com/team-code. It's a great exercise in balancing UX, security, and data compression.