Designing the Money Game Smart Contract
The Money Game smart contract that we are creating in this section will have the following capabilities:
When the smart contract is created, there will be a random number that is generated by the smart contract
Only the creator of the smart contract can read the number that is generated by the smart contract
Other users can interact with the smart contract to try and guess that number
Each time a user submits a guess, they have to submit 0.25 ETH
The smart contract will not allow the creator of the game to play the game
Once the number is guessed correctly, the smart contract will release the funds to whoever guessed the number correctly.
1
// SPDX-License-Identifier: UNLICENSED
2
3
pragma solidity ^0.6.12;
4
5
contract MoneyGame {
6
address private owner;
7
uint8 private targetNumber;
8
9
constructor (uint8 _targetNumber) public {
10
owner = msg.sender;
11
targetNumber = _targetNumber;
12
}
13
14
modifier onlyOwner() {
15
require(msg.sender == owner);
16
_;
17
}
18
19
function play (uint8 _guess) payable public returns (string memory) {
20
require(msg.value == 0.5 ether, "You must provide 0.5 ether to play");
21
require(msg.sender != owner, "Owner of this contract cannot play the game");
22
if (_guess == targetNumber) {
23
msg.sender.transfer(address(this).balance);
24
return ("You win the Money Game!");
25
} else {
26
return ("You lose the Money Game. Please try again");
27
}
28
}
This page is a preview of Creating an ERC20 Token on Ethereum