Created
February 20, 2018 00:44
-
-
Save mgdigital/4e20e220e9ceb312ae3ae149e3397a32 to your computer and use it in GitHub Desktop.
Ethereum smart contract for Rock Paper Scissors game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.18; | |
contract RockPaperScissors { | |
enum Choice { Rock, Paper, Scissors } | |
enum Outcome { Win, Lose, Draw } | |
address[] private players; | |
mapping(address => Choice) private choices; | |
mapping(address => int) scores; | |
event PlayerAdded(address player); | |
event ChoiceMade(address player, Choice choice); | |
event Played(address indexed p1, Choice p1Choice, address indexed p2, Choice p2Choice, Outcome outcome); | |
function setMyChoice(Choice _choice) public payable { | |
setChoice(msg.sender, _choice); | |
} | |
function setChoice(address _addr, Choice _choice) private { | |
if (!hasPlayer(_addr)) { | |
players.push(_addr); | |
PlayerAdded(_addr); | |
} | |
choices[_addr] = _choice; | |
ChoiceMade(_addr, _choice); | |
} | |
function getMyChoice() public view returns (Choice) { | |
return getChoice(msg.sender); | |
} | |
function getChoice(address _addr) private view returns (Choice) { | |
require(hasPlayer(_addr)); | |
return choices[_addr]; | |
} | |
function hasPlayer(address _addr) public view returns (bool) { | |
for (uint i = 0; i < players.length; i++) { | |
if (players[i] == _addr) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function play(address _opponent) public payable { | |
require(_opponent != msg.sender); | |
Choice myChoice = getMyChoice(); | |
Choice oppChoice = getChoice(_opponent); | |
Outcome outcome = getOutcome(myChoice, oppChoice); | |
handleOutcome(msg.sender, _opponent, outcome); | |
Played(msg.sender, myChoice, _opponent, oppChoice, outcome); | |
} | |
function getMyScore() public view returns (int) { | |
return getScore(msg.sender); | |
} | |
function getScore(address _addr) public view returns (int) { | |
require(hasPlayer(_addr)); | |
return scores[_addr]; | |
} | |
function getOutcome(Choice _choice, Choice _rivalChoice) public pure returns (Outcome) { | |
if (_choice == _rivalChoice) { | |
return Outcome.Draw; | |
} else if (_choice == Choice.Rock) { | |
return _rivalChoice == Choice.Scissors ? Outcome.Win : Outcome.Lose; | |
} else if (_choice == Choice.Paper) { | |
return _rivalChoice == Choice.Rock ? Outcome.Win : Outcome.Lose; | |
} else { | |
return _rivalChoice == Choice.Paper ? Outcome.Win : Outcome.Lose; | |
} | |
} | |
function handleOutcome(address _player, address _opponent, Outcome _outcome) private { | |
if (_outcome == Outcome.Win) { | |
scores[_player] += 1; | |
scores[_opponent] -= 1; | |
} else if (_outcome == Outcome.Lose) { | |
scores[_player] -= 1; | |
scores[_opponent] += 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment