1

For a homework problem, I need to write a contract for a rock-paper-scissors game for two players who would deposit the same amount of money then the winner will get both deposits. I am new to Solidity and don't know where to start.


here is the assignment

2

If the assignment is meant for people who are only starting their Solidity path and the assignment really is written as you said, I would argue that the assignment is a really bad one. Mostly because:

1) To do it right is actually quite complicated. Check out my other answer and @Tjaden Hess's comment on it.

2) To do it wrong leads to wrong kind of thinking.

There are many ways to do it wrong and it's really impossible to say which level of "wrongness" is expected of a "correct" answer.

  • im experiencing it right now – Jack.Mc Nov 4 '18 at 16:01
  • From the question added in the edit, it looks like the assignment is looking for a commit-reveal protocol, which is the correct way to implement this. A 1-phase solution is simply incorrect – Tjaden Hess Nov 5 '18 at 1:14
1

I assume this is for some study reasons.

If you need to do this for any serious reasons (for a company or so), you'd face serious issues with synchronization: if player A sends his transaction with Rock, player B can just check the transaction and send his transaction with Paper as he knows what player A played.

Nevertheless, for a basic idea you could use something like the following. Note that the concept of "Rock" is here represented as number 1 in variable move, "Paper" as number 2 and "Scissors" as number 3:

pragma solidity ^0.4.24;

contract RPS {

    uint256 previousMove;
    uint256 previousStake;
    address previousPlayer;

    function play(uint256 move) payable {

        // TODO: make sure stakes are the same

        if (previousMove == 0) {
            // If there is no move before this, make this the first move and Ethers stay in the contract
            previousMove = move;
            previousStake = msg.value;
            previousPlayer = msg.sender;
            return;
        }

        if (previousMove == 1) {
            if (move == 2) {
                // ...

                // if wins:
                msg.sender.transfer(address(this).balance);

                // if loses:
                previousPlayer.transfer(address(this).balance);
            }
        }
        // ...

    }
}

This is a very crude version but you'll hopefully get the point.

  • hey thanks for the reply , yeah it is for study reason so basically u say that palyer 2 is cheating ? – Jack.Mc Nov 3 '18 at 18:37
  • I'm not saying player 2 is cheating. I'm saying it would be trivial for player 2 to cheat. – Lauri Peltonen Nov 3 '18 at 18:38
  • ok i'm running the code in value box i input the number of ether but in play field what should input ? – Jack.Mc Nov 3 '18 at 18:49
  • move i know , but address? – Jack.Mc Nov 3 '18 at 18:54
  • 1
    You can avoid the cheating issue by doing a 2-phase game. Fist submit the hash of the move, with a salt, then reveal the move after both players have committed. – Tjaden Hess Nov 3 '18 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.