Welcome to the Rock Paper Scissors Lizard Spock game built on the Aptos blockchain! This fun and engaging game extends the classic Rock Paper Scissors with two additional moves: Lizard and Spock.
- Live Site: Play the game here
- Video Demo: Watch how to play
- Contract Address:
0x24520160f6fec0a0ecf2a522c4538f34f7bc37927d89387d75b40157be335624
This game is implemented as a decentralized application (dApp) on the Aptos blockchain. Players can compete against a computer opponent (affectionately named "Sheldon Cooper") in this extended version of the classic game.
The game follows these rules:
- Scissors cuts Paper
- Paper covers Rock
- Rock crushes Lizard
- Lizard poisons Spock
- Spock smashes Scissors
- Scissors decapitates Lizard
- Lizard eats Paper
- Paper disproves Spock
- Spock vaporizes Rock
- Rock crushes Scissors
- Connect your Aptos wallet to play (In this case Petra Wallet)
- Start and stop games at will
- Choose your move from Rock, Paper, Scissors, Lizard, or Spock
- Compete against a computer opponent
- View game results and running scores
- Blockchain-based game logic and score tracking from past games
- Frontend: React with TypeScript
- Blockchain: Aptos
- Smart Contract: Move language
- Wallet Integration: Aptos Wallet Adapter
The game's smart contract, implemented in RockPaperScissorsLizardSpock.move
, handles score tracking through the GameResult
struct:
struct GameResult has key {
computer_move: String,
game_result: String,
player_score: u64,
computer_score: u64,
draws: u64
}
This struct stores the current game state and cumulative scores. The duel function updates these scores based on the game outcome:
- Player wins:
player_score
is incremented. - Computer wins:
computer_score
is incremented. - Draw:
draws
is incremented.
This struct stores the current game state and cumulative scores. The duel function updates these scores based on the game outcome:
-
Connecting to the blockchain: The frontend uses the Aptos client to connect. The
Aptos
object handle everything that requires a connection to the Aptos network. A connection is established as soon as the object created.const aptosConfig = new AptosConfig({ network: Network.TESTNET }); const client = new Aptos(aptosConfig);
-
Fetching scores: The
fetchScores
function retrieves the current scores from the blockchain:const fetchScores = async () => { if (!account) return; try { const resource = await client.getAccountResource({ accountAddress: account.address, resourceType: `${moduleAddress}::${moduleName}::GameResult`, }); setScores({ user: Number(resource.player_score), computer: Number(resource.computer_score), draws: Number(resource.draws), }); } catch (error) { console.log("Error fetching scores: " + error); } };
The
getAccountResource
method is used to fetch the resource from the blockchain, which contains the scores. -
Payload: The payload is an object that contains the data needed to execute a transaction on the Aptos blockchain:
const payload: InputTransactionData = { data: { function: `${moduleAddress}::${moduleName}::duel`, functionArguments: [move], }, };
-
signAndSubmitTransaction: This function is provided by the Aptos Wallet Adapter and is used to sign and submit the transaction to the Aptos network:
const response = await signAndSubmitTransaction(payload);
It takes the payload, signs it with the user's wallet, and submits it to the blockchain. This function handles the complexities of transaction signing and submission.
- Connect your wallet.
- Click "Start Game" to begin a new game session & approve the transaction.
- Choose your move by clicking on one of the five options.
- The computer (Sheldon) will make its move.
- The result will be displayed, and scores will be updated.
- Continue playing or click "Stop Game" to end the session.
The game's core logic and score tracking are implemented in the RockPaperScissorsLizardSpock.move
smart contract. This contract handles:
- Game creation
- Move validation
- Result determination
- Score tracking
Enjoy playing Rock Paper Scissors Lizard Spock on the Aptos blockchain!