Skip to content

Instantly share code, notes, and snippets.

@Turupawn
Last active April 23, 2021 21:53
Show Gist options
  • Save Turupawn/93ebe5f5ad7d8d9a81bb63f2fa6f60b2 to your computer and use it in GitHub Desktop.
Save Turupawn/93ebe5f5ad7d8d9a81bb63f2fa6f60b2 to your computer and use it in GitHub Desktop.
pragma solidity 0.7.4;
contract YourContract {
enum Choice { Rock, Paper, Scissors }
uint256 public challenge_count = 0;
mapping(uint256 => address payable) public challenger_addresses;
mapping(uint256 => address payable) public challenged_addresses;
mapping(uint256 => uint256) public bets;
mapping(uint256 => Choice) private challenger_choices;
mapping(uint256 => Choice) private challenged_choices;
mapping(uint256 => bool) private is_finished;
// Private functions
function challenge(address payable challenged, Choice choice, uint256 bet) public payable
{
require(msg.value == bet);
challenge_count += 1;
challenger_addresses[challenge_count] = msg.sender;
challenged_addresses[challenge_count] = challenged;
bets[challenge_count] = bet;
challenger_choices[challenge_count] = choice;
}
function answerChallenge(uint256 challenge_id, Choice choice) public payable
{
require(msg.value == bets[challenge_id]);
require(msg.sender == challenged_addresses[challenge_id]);
challenged_choices[challenge_id] = choice;
is_finished[challenge_id] = true;
if(isTie(challenger_choices[challenge_id], challenged_choices[challenge_id]))
{
challenger_addresses[challenge_id].transfer(bets[challenge_id]);
challenged_addresses[challenge_id].transfer(bets[challenge_id]);
}else if (challengerWon(challenger_choices[challenge_id], challenged_choices[challenge_id]))
{
challenger_addresses[challenge_id].transfer(2*bets[challenge_id]);
}else
{
challenged_addresses[challenge_id].transfer(2*bets[challenge_id]);
}
}
// View functions
function getChallenger(uint256 hackathon_id) public view returns(address challenger_address)
{
return challenger_addresses[hackathon_id];
}
function getChallenged(uint256 hackathon_id) public view returns(address challenged_address)
{
return challenged_addresses[hackathon_id];
}
function getBet(uint256 hackathon_id) public view returns(uint bet)
{
return bets[hackathon_id];
}
function getChallengerChoice(uint256 hackathon_id) public view returns(Choice challenger_choice)
{
require(is_finished[hackathon_id]);
return challenger_choices[hackathon_id];
}
function getChallengedChoice(uint256 hackathon_id) public view returns(Choice challenged_choice)
{
return challenged_choices[hackathon_id];
}
// Private functions
function challengerWon(Choice challenger_choice, Choice challenged_choice) private pure returns (bool)
{
return (challenger_choice == Choice.Rock && challenged_choice == Choice.Scissors)
|| (challenger_choice == Choice.Paper && challenged_choice == Choice.Rock)
|| (challenger_choice == Choice.Scissors && challenged_choice == Choice.Paper);
}
function isTie(Choice challenger_choice, Choice challenged_choice) private pure returns (bool)
{
return challenger_choice == challenged_choice;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment