In the previous chapter, we initialized our contract with 20 pokemon names. Now, it's the time we battle ⚔️
A function can also return multiple return values like the following:
struct Student:
name: String[32]
age: uint256
# a mapping between roll number and
# student details
studentList: HashMap[uint256, Student]
@external
def getPersonDetails(rollNumber: uint256) -> (String[32], uint256):
student: Student = self.studentList[rollNumber]
return student.name, student.age
-
Create an
@external
function namedbattle
which has 1 input paramter:pokemon
of typePokemon
. It returns 4 parameters:bool
,String[32]
,uint256
,uint256
. -
Inside the
battle
function, create auint256
variable namedrandomDNA
. Now call the_generateRandomDNA
function and assign its output torandomDNA
. -
Inside the
battle
function, create aString[32]
variable namedrandomName
. Now to assign a value torandomName
, we have to select a random name frompokemonNames
by passing anyuint256
from0
to19
as key. We can generate a suitable randomuint256
by following operation:randomDNA
modulusNAME_MODULUS
. -
Inside the
battle
function, create auint256
variablerandomHP
. Set its value equal torandomDNA
modulus (%
)HP_LIMIT
.