Building a Trustless Rock-Paper-Scissors Game on Avalanche C-Chain Using Solidity
A beginner-friendly guide to developing and deploying a trustless smart contract game on Avalanche.
Blockchain technology has transformed finance, supply chain, and gaming industries by enabling secure, transparent, and decentralized systems. At the heart of this innovation are smart contracts, which automate and enforce agreements without intermediaries. If you're new to blockchain development, Solidity is the programming language you'll need to create these digital agreements on EVM-compatible platforms like Avalanche's C-Chain.
In this tutorial, we’ll introduce you to Solidity and guide you through building a simple rock-paper-scissors game on Avalanche. This beginner-friendly example demonstrates the potential of smart contracts and the performance benefits of Avalanche.
Why Choose Avalanche C-Chain?
Avalanche is a high-performance, scalable blockchain platform known for its low transaction costs and fast finality. Its C-Chain (Contract Chain) is fully compatible with Ethereum's Virtual Machine (EVM), enabling developers to deploy Solidity smart contracts seamlessly while benefiting from Avalanche's unique advantages:
Speed: Avalanche finalizes transactions in under a second.
Low Fees: Transaction costs are significantly lower than Ethereum's.
Scalability: Avalanche supports thousands of transactions per second without compromising decentralization.
By developing on Avalanche's C-Chain, you can leverage these benefits to build decentralized applications (dApps) optimized for performance and user experience.
What Are Smart Contracts?
Smart contracts are self-executing programs stored on a blockchain. They run when predefined conditions are met, ensuring transactions are traceable, transparent, and immutable. Think of a vending machine: insert money, select an item, and the machine automatically dispenses it. Similarly, smart contracts remove intermediaries in applications like payments, voting, and supply chain management.
Tutorial: Building a Rock-Paper-Scissors Game
In this tutorial, we’ll create a trustless rock-paper-scissors game using Solidity. The game ensures fairness and prevents cheating by leveraging cryptographic hash functions.
Key Criteria
To maintain the integrity of the game, the following criteria must be met:
No Prior Knowledge: Players cannot see each other's choices before submission.
Immutable Choices: Choices cannot be changed after submission.
Tamper-Proof: Choices are protected from third-party interference.
Determine Winner: The smart contract determines the winner based on the choices.
Using Cryptographic Hash Functions
We’ll use a cryptographic function called a hash function to ensure fairness. Hash functions have the following properties:
Deterministic: The same input always produces the same output.
Unique: Different inputs produce different outputs.
Non-Reversible: It's computationally infeasible to determine the input from the output.
Step 1: Define State Variables
Start by defining the core elements of the smart contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract RockPaperScissors {
bytes32 public constant ROCK = "ROCK";
bytes32 public constant PAPER = "PAPER";
bytes32 public constant SCISSORS = "SCISSORS";
mapping(address => bytes32) public choices;
}
Here:
ROCK, PAPER, SCISSORS: Represent possible choices, stored as bytes32.
Choices: A mapping that records hashed choices made by players.
Step 2: Submit Choices
The play function allows players to record their hashed choice.
function play(bytes32 hashedChoice) public {
require(choices[msg.sender] == bytes32(0), "Already played");
choices[msg.sender] = hashedChoice;
}
Validation: Ensures each player submits their choice only once.
Storage: Saves the hashed choice to the contract.
Step 3: Reveal Choices
Players reveal their choices and randomness (nonce) after both have submitted their hashes.
function revealChoice(bytes32 choice, bytes32 nonce) public {
require(
keccak256(abi.encodePacked(choice, nonce)) == choices[msg.sender],
"Invalid choice or nonce"
);
choices[msg.sender] = choice;
}
Validation: Verifies that the revealed choice matches the submitted hash.
Storage: Updates the stored choice with the revealed value.
Step 4: Evaluate the Winner
Once both players have revealed their choices, the smart contract determines the winner.
function determineWinner(
address player1,
address player2
) public view returns (address) {
bytes32 choice1 = choices[player1];
bytes32 choice2 = choices[player2];
require(choice1 != bytes32(0) && choice2 != bytes32(0), "Both players must reveal");
if (choice1 == choice2) return address(0); // Draw
if ((choice1 == ROCK && choice2 == SCISSORS) ||
(choice1 == SCISSORS && choice2 == PAPER) ||
(choice1 == PAPER && choice2 == ROCK)) {
return player1;
}
return player2;
}
Draw: Returns address(0) if both players make the same choice.
Winner Logic: Determines the winner based on game rules.
Deploying on Avalanche C-Chain
To deploy this contract on Avalanche's C-Chain:
Set Up an Avalanche Wallet: Use Core Wallet to interact with the Avalanche network.
Deploy the Contract: Use tools like Remix or Hardhat to deploy your Solidity contract.
Test the Contract: Interact with the contract using tools like Avalanche Explorer.
Conclusion
Building a rock-paper-scissors game on Avalanche's C-Chain taught you how to write and deploy a trustless smart contract using Solidity. With Avalanche’s high throughput and low fees, you can create fast, scalable dApps that provide exceptional user experiences.
For more coding tutorials on solidity visit Avalanche Academy
Dive into the Avalanche ecosystem today! Download the Core Wallet and unlock a world of seamless DeFi, NFTs, and more.
BULLISH
I'm looking forward to bring this to life