Created
March 30, 2019 08:29
-
-
Save zeburek/67ca0a155b003f1db6aedab29054aa06 to your computer and use it in GitHub Desktop.
Smart-contract for game Stone Paper Scissors
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 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