Web3 PvP Winner Takes Crypto

PvP, stands for player versus player. This is Eth contract where two parties enter into the contract, they both put some amount of crypto in and depending on the outcome of the game, the winner takes all the crypto.

What is required?

The kiosk, where players are organized to play. A player clicks on another player in a list. Before game commence, a web3 contract is setup for the game, players ante into the game. Wallets are deducted the ante of the game and locked into the contract. They enter and play a game of skill, such as Street Fighter 2, Mortal Kombat or Tekkan style game. The game registers the winner and executes the contract to send all the takings to the winner of the bout.

Here the sol contract is supplied.

The kiosk, the pre-game management, the game and outcome have not been developed.

The contract allows two players to play a game of rock-paper-scissors by calling the play() function and passing in their moves as arguments. The contract then determines the winner and sets the winner variable accordingly. The winner can then call the claimWinnings() function to receive their winnings, which is twice the amount of the bet.

To call the contract's functions from a web program, you could use a library such as web3.js. Call the play() function from a web program:

const Web3 = require('web3'); const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

// Set the contract address and ABI (generated from the Solidity contract) const contractAddress = '0x12345...'; const contractABI = []; const contract = new web3.eth.Contract(contractABI, contractAddress);

// Get the player addresses and bet amount from the form input const player1Address = document.getElementById('player1Address').value; const player2Address = document.getElementById('player2Address').value; const betAmount = document.getElementById('betAmount').value;

// Call the play function contract.methods.play(player1Move, player2Move).send({ from: player1Address, value: betAmount }) .then(function(receipt) { console.log(receipt); });

The code sends a transaction to the Ethereum network to call the play() function on the contract, with the player addresses and bet amount provided as input. The transaction is sent from player1Address, and the value field indicates the amount of ETH to send along with the transaction.

Call claimWinnings() function in a similar way, by replacing play() with claimWinnings() and providing the winner's address as the from field.

Potential Games To Develop

- https://github.com/jkneb/street-fighter-css

- https://github.com/simonsablowski/beatemup

- https://github.com/njbittner/battle-bros-pyarcade

- https://github.com/carabus/battleship-online

- https://github.com/shaan1337/quiz

How The Game Lobby Might Work?

Online multiplayer lobby maintains a list of all users who are currently online and available to play. They must register. When a user starts a new game, the system generates a unique link to the game room that can be shared with other online players. Other players can then accept the invitation to join the game, and the game room is locked to prevent any additional players from joining the game, the smart contract is executed, and the ante is taken from accounts and locked in the smart contract. The game is then initiated and the players can begin the match. The winner of the game is known and the smart contract winner function executed. The users are returned to the lobby to try again.

pragma solidity 0.6.0;

contract Game {

address public player1;

address public player2;

uint public betAmount;

bool public gameOver;

address public winner;

constructor(address _player1, address _player2, uint _betAmount) public {

player1 = _player1;

player2 = _player2;

betAmount = _betAmount;

gameOver = false;

}

function play(uint8 _player1Move, uint8 _player2Move) public payable {

require(!gameOver, "The game is already over.");

require(msg.value == betAmount, "Incorrect bet amount.");

player1.transfer(betAmount);

player2.transfer(betAmount);

// Determine the winner

if ((_player1Move == 1 && _player2Move == 3) || (_player1Move == 2 && _player2Move == 1) || (_player1Move == 3 && _player2Move == 2)) {

winner = player1;

} else if ((_player2Move == 1 && _player1Move == 3) || (_player2Move == 2 && _player1Move == 1) || (_player2Move == 3 && _player1Move == 2)) {

winner = player2;

} else {

winner = address(0);

}

gameOver = true;

}

function claimWinnings() public {

require(gameOver, "The game is not over yet.");

require(msg.sender == winner, "Only the winner can claim the winnings.");

winner.transfer(2 * betAmount);

gameOver = false;

}

}

  

📝 📜 ⏱️ ⬆️