1
+ # @version ^0.2.0
2
+
3
+ #Contract Guess Number
4
+ #Contrac should support multiple guess the number games.
5
+ #Creator of games should risk 10 ETH to start a new game.
6
+ #A secret number is established during game creation
7
+ #The secret number should be between 0-100.
8
+ #The game creator cannot play in the games he created.
9
+ #Each guess of the number in a game will cost 1 Eth.
10
+ #Each game should allow only 10 guesses, once the tenth guess is made
11
+ #the game will expire and the funds will go to the game creator.
12
+ #Whoever makes the right guess will win the balance of the game.
13
+ #Once the right guess is made a game should not allow people to play.
14
+ #Once a game makes a payment 1% of the amount will go to the contract creator as a fee.
15
+
16
+ event Game_created:
17
+ owner: indexed (address )
18
+ game_index: uint256
19
+ time: uint256
20
+
21
+ event Game_solved:
22
+ solver: indexed (address )
23
+ game_index: uint256
24
+ time: uint256
25
+
26
+ struct game :
27
+ game_owner: address
28
+ secret_number: uint256
29
+ game_balance: uint256
30
+ guess_count: uint256
31
+ is_active: bool
32
+
33
+ curr_id: uint256
34
+
35
+ game_index: HashMap[uint256 , game]
36
+
37
+ contract_owner: address
38
+
39
+ @external
40
+ def __init__ ():
41
+ self .contract_owner = msg .sender
42
+ self .curr_id = 0
43
+
44
+ @external
45
+ @payable
46
+ def create_game (_secret_number:uint256 ) -> bool :
47
+ assert msg .value == 10 * (10 ** 18 ), "You should pay 10 ether to create game "
48
+ assert (_secret_number >= 0 ) and (_secret_number <= 100 ), "The secret number should be within 0-100 "
49
+ self .game_index[self .curr_id].game_owner = msg .sender
50
+ self .game_index[self .curr_id].game_balance = self .game_index[self .curr_id].game_balance + msg .value
51
+ self .game_index[self .curr_id].secret_number = _secret_number
52
+ self .game_index[self .curr_id].guess_count = 0
53
+ self .game_index[self .curr_id].is_active = True
54
+ self .curr_id = self .curr_id + 1
55
+ log Game_created (msg .sender , self .curr_id - 1 , block .timestamp )
56
+ return True
57
+
58
+ @external
59
+ @view
60
+ def get_game_balance (_game_id: uint256 ) -> uint256 :
61
+ return self .game_index[_game_id].game_balance
62
+
63
+ @external
64
+ @view
65
+ def get_game_guesses (_game_id: uint256 ) -> uint256 :
66
+ return self .game_index[_game_id].guess_count
67
+
68
+ @external
69
+ @view
70
+ def is_game_active (_game_id: uint256 ) -> bool :
71
+ return self .game_index[_game_id].is_active
72
+
73
+ @external
74
+ @payable
75
+ def play_game (_game_id: uint256 , _guessed_number:uint256 ) -> bool :
76
+ assert msg .value == 10 ** 18
77
+ assert msg .sender != self .game_index[_game_id].game_owner
78
+ assert (_guessed_number >= 0 ) and (_guessed_number<= 100 )
79
+ assert self .game_index[_game_id].is_active == True
80
+ self .game_index[_game_id].game_balance = self .game_index[_game_id].game_balance + msg .value
81
+ self .game_index[_game_id].guess_count = self .game_index[_game_id].guess_count + 1
82
+ if _guessed_number == self .game_index[_game_id].secret_number:
83
+ send (msg .sender , (self .game_index[_game_id].game_balance * 99 ) / 100 )
84
+ send (self .contract_owner, self .game_index[_game_id].game_balance / 100 )
85
+ self .game_index[_game_id].game_balance = 0
86
+ self .game_index[_game_id].is_active = False
87
+ log Game_solved (msg .sender , _game_id, block .timestamp )
88
+ else :
89
+ if self .game_index[_game_id].guess_count == 10 :
90
+ send (self .game_index[_game_id].game_owner, (self .game_index[_game_id].game_balance * 99 )/ 100 )
91
+ send (self .contract_owner, self .game_index[_game_id].game_balance / 100 )
92
+ self .game_index[_game_id].game_balance = 0
93
+ self .game_index[_game_id].is_active = False
94
+ return True
0 commit comments