Skip to content

Instantly share code, notes, and snippets.

@zeburek
Created March 30, 2019 08:29
Show Gist options
  • Save zeburek/67ca0a155b003f1db6aedab29054aa06 to your computer and use it in GitHub Desktop.
Save zeburek/67ca0a155b003f1db6aedab29054aa06 to your computer and use it in GitHub Desktop.
Smart-contract for game Stone Paper Scissors
pragma solidity >=0.4.18;
contract StonePaperScissors{
enum VARIANTS {STONE, PAPER, SCISSORS}
enum WINNER {DRAW, USER, COMP}
mapping (uint => mapping(uint => int)) winLooseMatrix;
uint nonce = 132;
uint houseEdge = 1;
address player;
uint playerChoice;
uint compChoice;
constructor() public {
winLooseMatrix[uint(VARIANTS.STONE)][uint(VARIANTS.STONE)] = int(WINNER.DRAW);
winLooseMatrix[uint(VARIANTS.STONE)][uint(VARIANTS.PAPER)] = int(WINNER.COMP);
winLooseMatrix[uint(VARIANTS.STONE)][uint(VARIANTS.SCISSORS)] = int(WINNER.USER);
winLooseMatrix[uint(VARIANTS.PAPER)][uint(VARIANTS.STONE)] = int(WINNER.USER);
winLooseMatrix[uint(VARIANTS.PAPER)][uint(VARIANTS.PAPER)] = int(WINNER.DRAW);
winLooseMatrix[uint(VARIANTS.PAPER)][uint(VARIANTS.SCISSORS)] = int(WINNER.COMP);
winLooseMatrix[uint(VARIANTS.SCISSORS)][uint(VARIANTS.STONE)] = int(WINNER.COMP);
winLooseMatrix[uint(VARIANTS.SCISSORS)][uint(VARIANTS.PAPER)] = int(WINNER.USER);
winLooseMatrix[uint(VARIANTS.SCISSORS)][uint(VARIANTS.SCISSORS)] = int(WINNER.DRAW);
}
function randomChoice() private returns(uint rndChoice){
uint randomnumber = (uint(keccak256(abi.encode(blockhash(block.number)))) + nonce) % 3;
nonce++;
return randomnumber;
}
function winOrLoose(uint choiceOne, uint choiceTwo) private returns(string memory winner){
int result = winLooseMatrix[choiceOne][choiceTwo];
if (result == int(WINNER.DRAW)) return "Draw";
if (result == int(WINNER.COMP)) return "Computer wins";
if (result == int(WINNER.USER)) return "User wins";
}
function playCUEFA(CUEFA choice) payable public returns(string memory winner) {
playerChoice = uint(choice);
compChoice = randomChoice();
return winOrLoose(playerChoice, compChoice);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment