Skip to content

Instantly share code, notes, and snippets.

@pankajb64
Last active May 30, 2018 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pankajb64/cd099951f71197e254f4c45b776a86a9 to your computer and use it in GitHub Desktop.
Save pankajb64/cd099951f71197e254f4c45b776a86a9 to your computer and use it in GitHub Desktop.
Ethereum DAPP for playing Rock, Paper, Scissors. Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.24+commit.e67f0147.js&optimize=false&gist=
pragma solidity ^0.4.23;
contract RPS {
address player1;
address player2;
bytes32 commitment;
string play1;
string play2;
uint stake;
bytes32[] moves;
function RPS() public payable {
player1 = msg.sender;
moves.push(keccak256("Rock"));
moves.push(keccak256("Paper"));
moves.push(keccak256("Scissor"));
}
function p1_commit(bytes32 _commitment) public payable {
require(msg.sender == player1);
require(msg.value > 0 ether);
commitment = _commitment;
stake = msg.value;
}
function p2_join(string _play) external payable {
require(_strEmpty(play2));
require(_validMove(_play));
require(msg.value == stake);
play2 = _play;
}
function p1_reveal(string _play, string _salt) external {
require(msg.sender == player1);
require(keccak256(_play, _salt) == commitment);
require(_validMove(_play));
play1 = _play;
}
function p2_payout() external {
require(msg.sender == player2);
}
function p1_replay(bytes32 _commitment) external {
require(msg.sender == player1);
commitment = _commitment;
}
function p2_replay(string _play) external {
require(msg.sender == player2);
play2 = _play;
}
function _strEmpty(string _str) private pure returns(bool) {
return bytes(_str).length == 0;
}
function _validMove(string _play) private returns(bool) {
bytes32 hash = keccak256(_play);
for(uint i = 0; i < moves.length; i++) {
if(hash == moves[i]) {
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment