In the previous chapter, we created the battle function which created a random pokemon.
We need to compare the wild pokemon and the trainer pokemon to return the result of the battle. For this purpose we need comparison operators.
Different types support different comparison operators. For our use-case we will compare 2 uint256.
Here is a list of comparison operators supported by uint256 type.
| Operator | Description |
|---|---|
| x < y | Less than |
| x <= y | Less than or equal to |
| x == y | Equals |
| x != y | Does not equal |
| x >= y | Greater than or equal to |
| x > y | Greater than |
The if statement is a control flow construct used for conditional execution:
if CONDITION:
# do somethingCONDITION is a boolean or boolean operation. The boolean is evaluated left-to-right, one expression at a time, until the condition is found to be true or false. If true, the logic in the body of the if statement is executed.
Note that unlike Python, Vyper does not allow implicit conversion from non-boolean types within the condition of an if statement. if 1: pass will fail to compile with a type mismatch.
You can also include elif and else statements, to add more conditional statements and a body that executes when the conditionals are false:
if CONDITION:
# do something 1
elif OTHER_CONDITION:
# do something 2
else:
# do something 3We will compare the HP of the pokemons to decide the winner. If the trainer pokemon wins, the trainer gets the new pokemon.
-
Inside the
battlefunction, increment thebattleCountstate variable by 1. -
Create an
ifstatement which compares the HP of the trainerpokemonparameter and therandomHPof the wild pokemon. If the HP of trainerpokemonis greater thanrandomHPthen the trainerpokemonwins. -
In the body of the
ifstatement, return the following arguments:- Battle result:
True - Wild pokemon name:
randomName - Wild pokemon DNA:
randomDNA - Wild pokemon HP:
randomHP
- Battle result: