1

In my smart contract, I want the two participants to send the same amount of ether so they can start a game of rock, paper, scissors. How can I modify the sendEnoughCash() function do that?

This is the contract:

function register (string choice) payable notRegisteredyet()  sentEnoughCash(5 wei) {

        if(player1 == 0)
        player1=msg.sender;
        else if (player2 == 0)
        player2=msg.sender;
        if (msg.sender == player1)
            player1Choice = choice;
        else if (msg.sender == player2)
            player2Choice = choice;




    }

    function rpsgame() 
    {   // constructor
        scenarioMatrix["rock"]["rock"] = 0;
        scenarioMatrix["rock"]["paper"] = 2;
        scenarioMatrix["rock"]["scissors"] = 1;
        scenarioMatrix["paper"]["rock"] = 1;
        scenarioMatrix["paper"]["paper"] = 0;
        scenarioMatrix["paper"]["scissors"] = 2;
        scenarioMatrix["scissors"]["rock"] = 2;
        scenarioMatrix["scissors"]["paper"] = 1;
        scenarioMatrix["scissors"]["scissors"] = 0;
    }



    modifier notRegisteredyet() {

        if (msg.sender == player1 || msg.sender == player2)
         revert();
         else
          _;

    }

     modifier sentEnoughCash(uint amount) {
        if (msg.value < amount)
         revert();
         else
         _;
    }

     function play() returns (int w ){

            if (bytes(player1Choice).length != 0 && bytes(player2Choice).length != 0){

            int winner = scenarioMatrix[player1Choice][player2Choice];
            if (winner == 1)
                player1.transfer(this.balance);
            else if (winner == 2)
                player2.transfer(this.balance);

            else
            {
                player1.transfer(this.balance/2);
                player2.transfer(this.balance);
            }
            // to reset game 
           player1Choice = "";
            player2Choice = "";
            player1 = 0;
           player2 = 0;
            return winner;
        }
        else 
            return -1;
    }
}
4
  • Hi, could you please put your smart contract code so far and reformat your sentence? From what I understood, do you want a smart contract which allows you to start a game when players enter 2 ETH? Thanks in advance
    – Iulian
    Nov 7, 2018 at 21:54
  • 1
    hi I made a simple rock paper and scissors game one of the condition is that both players should place the same amount of ethers to start the game
    – Albero
    Nov 7, 2018 at 22:06
  • Ok so there is more than one approach. My personal opinion is that you should do 2 things: 1) make a requirement to enter the game such as requiring the minimum value to enter to be 0.01 ETH[or any value you want] for all players or, 2) allow players to define the value and then, on the client side render the desired gambled amounts by players. I'm waiting for someone to see if it's a good idea to check with an if condition whether two players entered the same amount.
    – Iulian
    Nov 7, 2018 at 22:17
  • I already set a minimum amount which is 5 Wei u can check that in the code
    – Albero
    Nov 7, 2018 at 22:27

1 Answer 1

0

It seems like you want your sendEnoughCash() function to do two things:

  • Verify that each use has sent at least a minimum amount of ether
  • Verify that the second user sends the same amount as the first user

You left out some of your code, but based on the functions you did share, there seems to be two relevant global variables: player1 and player2 which are both addresses of the users playing your game.

You should be able to add one more global variable, which tracks the cost of the current game, which is set by player, lets call that variable currentGameCost:

uint currentGameCost = 0;

Then when player1 joins the game, you want to update this value to represent the amount they paid. Additionally, you want to check that when player2 joins, they also send that amount. This can all be handled with a modified sendEnoughCash() function:

modifier sentEnoughCash(uint amount) {
    if (msg.value < amount) {
        revert();
    }

    if (player1 == 0) {
        currentGameCost = msg.value;
    } else if (msg.value != currentGameCost) {
        revert();
    }

    _;
}

Then at the end of your play() function, you simply need to reset currentGameCost to 0 like you do with player1/player2.

Not claiming this is perfect, but you should get an idea of how to accomplish this with minimal modification to the rest of your contract.

6
  • yeah your are right about the global variable I have player1 and player2 ,, thanks so much for clarifying ill try that and modify the code and get back to you ,, regards :)
    – Albero
    Nov 8, 2018 at 8:29
  • hey 2 questions question1: when you wrote this command. if (player1 == 0) { currentGameCost = msg.value;... what do you mean by player1== 0 what are you checking here exactly ? second question I tried the code and when I enter different amount it gives an error and say transaction failed but how would the second player know the amount that the first player deposit?
    – Albero
    Nov 8, 2018 at 9:05
  • You would need to expose to player 2 the global variable which stores the currentGameCost. You can do this with a "getter" function. player1 == 0 is the same thing you are doing in the first few lines of your register function. If the player1 variable is not assigned, it will have the value 0, thus we know that it is player1 who will be registering at that time. Nov 8, 2018 at 9:48
  • hi I head a lot talking about the gas and to be honest I never really know what some programmer talking about. I can see that for each player there are 3 words related to gas( Gas, transaction cost , execution cost) what is the gas they talk about exactly??
    – Albero
    Nov 8, 2018 at 21:11
  • I will give you this link, but if you have more questions I suggest you first google, and then open a new question if you still have not found the answer. Nov 8, 2018 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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