forked from vyperfun/vyper.fun
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path2.6-finished-code.vy
59 lines (47 loc) · 1.63 KB
/
2.6-finished-code.vy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# @version >=0.2.4 <0.3.0
struct Pokemon:
name: String[32]
dna: uint256
HP: uint256
matches: uint256
wins: uint256
DNA_DIGITS: constant(uint256) = 16
DNA_MODULUS: constant(uint256) = 10 ** DNA_DIGITS
HP_LIMIT: constant(uint256) = 1000
NAME_MODULUS: constant(uint256) = 20
battleCount: uint256
pokemonNames: HashMap[uint256, String[32]]
@external
def __init__():
self.pokemonNames[0] = "Bulbasaur"
self.pokemonNames[1] = "Charmander"
self.pokemonNames[2] = "Charizard"
self.pokemonNames[3] = "Squirtle"
self.pokemonNames[4] = "Blastoise"
self.pokemonNames[5] = "Pidgey"
self.pokemonNames[6] = "Raticate"
self.pokemonNames[7] = "Pikachu"
self.pokemonNames[8] = "Raichu"
self.pokemonNames[9] = "Venomoth"
self.pokemonNames[10] = "Arcanine"
self.pokemonNames[11] = "Abra"
self.pokemonNames[12] = "Machop"
self.pokemonNames[13] = "Golem"
self.pokemonNames[14] = "Onix"
self.pokemonNames[15] = "Hypno"
self.pokemonNames[16] = "Rhydon"
self.pokemonNames[17] = "Kangaskhan"
self.pokemonNames[18] = "Scyther"
self.pokemonNames[19] = "Mewtwo"
@internal
def _generateRandomDNA() -> uint256:
random: uint256 = convert(keccak256(convert(self.battleCount, bytes32)), uint256)
return random % DNA_MODULUS
@external
def battle(pokemon: Pokemon) -> (bool, String[32], uint256, uint256):
randomDNA: uint256 = self._generateRandomDNA()
randomName: String[32] = self.pokemonNames[randomDNA%NAME_MODULUS]
randomHP: uint256 = randomDNA % HP_LIMIT
self.battleCount += 1
if pokemon.HP > randomHP:
return True, randomName, randomDNA, randomHP