On-Chain Chess: Smart Contract Breakdown | by Nazar Ilamanov | Mar, 2022

  • Game dynamics — what is it about?
  • The smart contract source code
  • On-chain data representation
  • Generating NFTs
  • Game engine
  • The game consists of you (player) playing against the AI ​​that is programmed into the smart contract. The AI ​​always plays black.
  • You can make a move, and the engine will respond with its own move. Making a move requires gas, but you get an NFT in return.
I think that the onboarding on the homepage deserves separate applause — can make a move right away and mint an NFT.
  • All the games and moves are recorded on the blockchain. There are 59 games max and 59 moves per game max.
  • There is just one game being played at a time. Anyone can make the next move and the game will continue.
  • Everything’s on-chain: the engine, the NFT data, and the image (in the form of HTML)
  • There is an interesting adaptation to NFT/web3 world: Each move (and the corresponding counter move by the AI) will be minted as an NFT:
Noticed the owner?
The NFT is interactive
  1. Chess.sol — for data representation. How the chessboard, chess pieces, and moves are represented on-chain.
  2. Engine.sol— the AI ​​that can make moves, capture pieces, etc
  3. fiveoutofnine.sol— ERC-721 implementation allowing to mint moves
  4. fiveoutofnineART.sol — helper used by the above contract for generating the metadata and images for NFTs
index is the index of the 256-bit integer currently in use
Checks if the current partition is full. If full, just add the move. Otherwise, shift existing moves before adding
  • fiveoutofnine.sol — implementation of the ERC-721 standard.
  • fiveoutofnineART.sol — a collection of helper functions for generating the NFT art — called by fiveoutofnine.sol.
  • name: string in the format “Game #X, Move #Y”
  • description: shown in the image below
  • bit border, color generation, dimension, gap, and height: explained below

Leave a Comment