I'm writing a contract in solidity (for practice) that is a guessing game that is played as such: 1) Users are given 100 MetaCoin (dummy currency) on sign up. 2) Users can pay 1 coin to play a guessing game, if they guess correctly they're given 5 coins, if not, they lose the coin.
However, I'm unsure on how to best implement this. Here's a pseudo implementation:
function takeGuess(int guess) returns (bool){
uint balance = getBalance(msg.sender);
if (balance>0){
int actual = popNumber(); //Retrieves next "random" number
if (guess == actual){
coin.sendCoin(msg.sender, 4);
return true;
} else {
coin.sendCoin(this, 1, {from: msg.sender}); //Not legal syntax
return false;
}
} else {
return false;
}
return false;
}
Now, this doesn't work because the third argument in the else statement is not a valid argument (it's JS abi notation), but it's a representation of what I want to do. Is there a way to take coins from users in-contract like this? Or does the user need to send coins, and then wait for the verification? How would I do that?
Here is the repo if you want to see the full source code: https://github.com/willikers19/GuessTheEth
Edit: This contract is mostly just for practice. It's not intended to have "good" random behavior.