How-to
Save and restore a game
Save a game as its setup and moves, restore it by replaying them, and decide what to share.
Save the setup and moves
encodeHitotsu writes setup, names, seed and the ordered moves. It does not trust saved hands or piles. decodeHitotsu starts that setup and replays every move through the rules.
import { decodeHitotsu, encodeHitotsu, hitotsuMoves, playHitotsu, startHitotsu } from "@johnmorrisdotca/hitotsu";
const game = startHitotsu(1, ["Aki", "Ben"], 42)!;
const next = playHitotsu(game, hitotsuMoves(game)[0]!)!;
const restored = decodeHitotsu(encodeHitotsu(next));
console.log(restored?.moves.length, restored?.hands[0]);
console.log(decodeHitotsu("not JSON"));1 [ 'R31', 'R50', 'G00', 'G61', 'BD0', 'WF2' ]
nullCheck a refused save
The decoder returns null for unreadable JSON, unsupported format, invalid setup or an illegal recorded move. Show an error instead of continuing with a partial game. The same seed reproduces the deal; the move log is needed to reproduce the later play.
Decide what to share
The encoded game contains player names and enough information to reconstruct the cards. Choose storage and sharing in the host application. The builder’s setup URL omits private names and does not include the played move log. Embed details distinguish local game events from a network match.