Nine Cells: a merge tower defense game (testnet)

Hi everyone, I’m Karas, a CKBuilder in the current cohort. I’ve been building Nine Cells, a browser tower defense game where you defend a wall by placing cats in lanes and merging two same-level cats into a stronger one. I’d like to share how it uses cells and get feedback, both on the game and on the script design.

Play it: https://nine-cells.karasdev.com/ (CKB testnet)

You can play the battles without a wallet. A CKB testnet wallet (JoyID or UTXO Global) is only needed to mint and keep cats on chain. Testnet CKB from the faucet covers the capacity and fees.

Why cells fit this game

The core action in the game is “two cats in, one stronger cat out”. That is exactly what a CKB transaction does: consume input cells, create output cells. So I didn’t need a contract with balances or a registry. Each cat you own is just a cell under your lock, and the rules live in one type script that checks every transaction touching those cells.

A CatCell is a cell with 8 bytes of data:

Byte Field
0 version
1 level (1 to 255)
2 line / skin (1 to 21, 0 = unopened crate)
3 role (0 to 3)
4 to 7 block number at mint (metadata only)

The capacity locked in the cell (about 134 CKB at the moment) goes back to the owner when a cat is released.

One type script, dispatched by transaction shape

The script is written in TypeScript and runs on ckb-js-vm. It doesn’t read an “action” flag from the witness. It looks at how many cells of its own type are in the inputs and outputs, so a transaction can’t claim to be one action while doing another:

Inputs Outputs Action What the script checks
0 1 or more Mint Only unopened crates at a fixed level (9). Nobody can mint a chosen cat directly.
n n Transfer / Reveal Cat data must be unchanged (gift, batch move), or a crate becomes the cat picked by the seed below.
1 or more 0 Release Burns the cat, capacity returns to the wallet.
2, 4 or 8 1 Merge Same level and version; output level = input level + log2(count); output keeps the line of the last input.

Every rule has its own exit code, so a failing test points at the exact rule that broke. I checked the script against a real devnet node with 30 deliberately invalid transactions, and each was rejected with the expected code.

Random cats without an oracle

Opening a crate is a commit and reveal:

  1. Mint creates an unopened crate cell.
  2. Reveal spends the crate and creates the cat. The cat’s line must equal 1 + u32(seed) % 21, where seed = blake2b(nonce ‖ transactions_root ‖ crate outpoint) of the block right after the block that contains the crate. The transaction carries both headers in header_deps, and the script recomputes the seed.

When the crate is created, that next block doesn’t exist yet, so the player can’t predict or pick the result, and the script refuses any other line.

A known limit I’d like opinions on: a player can mint many crates, open them all, keep the line they want and release the rest for only the fees. Mint is open and fixed at level 9 on purpose (the client can’t prove a battle was won), so I’m unsure whether to treat this as acceptable or change the design.

What stays off chain, on purpose

Battles, placing cats, in-battle merges, stars, XP and gems all stay local in the browser. The battle runs on the client, so putting results on chain wouldn’t prove a fair game. I also looked at Spore DOBs and decided against them, because Spore content is immutable and a merge needs to rewrite the cat’s data.

The game has an optional fast mode: a session wallet you fund once, which then signs Vault actions without a popup and sweeps cats back to your main wallet. The trade-off is that its key lives in browser storage, so it’s for small amounts only.

Status on testnet

10 Likes

Had a lot of fun with it :slight_smile:

Might still be possible with Spore DOBs by melting two Spores and mint a new one in the same txn.

5 Likes