Game on Solana: Guess the Number
A minimalist on-chain guessing game built with Rust and Anchor: the contract picks a number, the player guesses it.
A simple interactive number guessing game, powered by smart contracts on the Solana blockchain. Written in Rust with the Anchor framework.
The game is a minimalist on-chain program built with Anchor on Solana. It combines basic game logic with the chain infrastructure: the contract thinks of a number and the player has to guess it.
It works well as a demo for:
- Learning how to build interactive dApps on Solana
- Understanding Solana account state and transactions
- Combining game mechanics with blockchain immutability
Stack
- Rust, for the smart contract logic
- Anchor, for building and testing Solana programs
- A local Solana validator for development and simulation
Features
- On-chain random number generation. The program stores a secret number within its state, pseudo-random for now.
- Guess by transaction. Users guess the number by sending a transaction with their attempt.
- Immediate response. The program replies whether the guess is too low, too high or correct.
- Stateless client. All game logic and storage is managed on-chain, so the client only sends guesses.
Getting started
-
Start the local validator:
solana-test-validator -
Build and deploy the program:
anchor build && anchor deploy -
Play the game. Send transactions with your guess to the validator:
anchor run guess -- --number 42This assumes you have set up a client command or script to interact with the program using the Anchor CLI or custom JS/TS scripts.
How it works
- Upon initialization, the program stores a secret number, generated on start.
- Each guess is a transaction carrying an integer.
- The program processes the guess and emits a response via logs or account state.
License
This project is open-source and licensed under the MIT License. Feel free to build upon, modify, or expand it.
Neighbors in time
Related: the posts that share this one's subject, and where the thread goes next.