DocsArcade 2048
On this page

Arcade 2048

On-chain 2048. The contract checks every move, deals each new tile from a keccak chain, and keeps a leaderboard of the ten best players on the layer. Arcade2048 has no owner, takes no constructor arguments and holds no money.

#A game, step by step

  1. Session. setSession(session) names an address that may play for you. Any HYPE sent with the call is forwarded straight to that address for its gas. A session address belongs to one player at a time, and only that player can release it with clearSession(session).
  2. Commit. start(commit) opens a game with commit = keccak256(secret). A player has one live game at a time.
  3. Reveal. reveal(secret) checks the secret against the commit, sets the seed to keccak256(secret, blockhash(block.number - 1), player) and deals the first two tiles.
  4. Moves. move(dir) with 0 for left, 1 for right, 2 for up and 3 for down. A move that changes nothing reverts. Every accepted move adds one tile.
  5. End. finish(player) closes a game that has no legal move left, and anyone may call it. surrender() ends your own game early. The best score already recorded stays.

start, reveal, move and surrender can come from the player or from the player's session address. Either way the game is the player's.

#Tiles and score

The board is a uint64: 16 cells of 4 bits, where a cell holding n is the tile 2 to the power n, and a cell's index is row × 4 + column.

For each new tile the contract takes seed = keccak256(seed) and reads it as a number r:

  • the tile goes to empty cell number (r >> 16) mod (number of empty cells), counting empty cells in index order;
  • it is a 4 when (r & 0xffff) mod 10 = 0, and a 2 otherwise, so about one tile in ten is a 4.

Each merge adds the value of the new tile to the score: two 8s make a 16 and add 16. A tile merges at most once per move.

The secret is fixed by the commit before the reveal. At the reveal the seed also mixes in blockhash(block.number - 1) as the layer's EVM returns it (on a layer block.number follows Elysium, see Developers) and the player's address. After the reveal the seed is stored in the contract and returned by game(player), so anyone can compute the coming tiles. What the contract enforces is that the tiles follow from that seed and from nothing else.

#Leaderboard

leaderboard() returns at most ten entries, highest first, one per player, built from each player's best score (bestScore(player)). A best score updates on every move that beats it, so a game climbs the board while it is played. Each new place emits Record.

#The page and its session key

Play on this layer on the app page:

  1. creates a session key in your browser and keeps it in the site's local storage, for this app and your wallet;
  2. if that key is not yet your session, or holds less than 0.0004 HYPE, asks your wallet once to call setSession with 0.002 HYPE;
  3. generates a secret, keeps it in local storage, and has the session key send start and reveal;
  4. sends each move from the session key, with a gas limit of 220,000 and twice the current gas price.

The page shows each move at once, computed in your browser with the contract's own rules, and compares with the contract whenever moves go quiet. If they ever differ, the contract's board wins. New game surrenders the live game and deals a new one.

What a session key can do: play this game for you, with the HYPE it holds as gas. It cannot touch your wallet. Anyone with access to this browser's storage for the site could use it to play your game and spend that gas. To release it, call clearSession(session) from your wallet.

The practice board uses the same sliding rules in the browser and records nothing.

#Functions

Function Who may call What it does
setSession(address) payable Anyone Names a session for the caller and forwards any HYPE to it.
clearSession(address) The player who set it Releases a session address.
start(bytes32 commit) Player or session Opens a game.
reveal(bytes32 secret) Player or session Seeds and deals the first two tiles.
move(uint8 dir) Player or session Slides, merges, scores and adds a tile.
finish(address player) Anyone Closes a game with no legal move left.
surrender() Player or session Ends the player's game early.
game(address) Read Board, score, moves, commit, seed, live.
leaderboard() Read Up to ten entries, highest first.
bestScore(address), sessionOf(address), canMove(uint64) Read A player's best, a session's player, whether a board can move.

Events: SessionSet, Started, Dealt, Moved, Finished, Record.

Errors: NoGame, GameLive, BadCommit, BadReveal, BadDirection, NoMoveMade, StillPlayable, SessionTaken, TransferFailed.