In this tutorial we will explain what are algorithms and we will teach you in a matter of 10 minutes algorithmic logic reading. COME IN, WE MADE IT FUN!
Welcome to another new post. Programming is as easy as understanding the flow of any process. To explain the logic of a programming algorithm, we will quote Pokémon!
If you do not know what this game is, we will explain it to you: it is an adventure and action game, in which you put 2 animals to fight, which have special powers that are different from each other, so to speak. But before designing the algorithm for a Pokémon battle, you may wonder ...
What are algorithms?
An algorithm is a sequence of steps to follow; It explains how to do a process step by step, precisely and exactly, and it always has an end, it is never infinite.
Now you know! Now let's continue:
Let's imagine we put "Pikachu" to fight, and another called "Jigglypuff".
In programming, each of these is considered a «Object«. And they all have several things in common, such as the following:
How do we represent this in code? Well, if you saw the definition of what an object is, it would look like this:
Pikachu Life = 100 Pikachu Attack = 55 Jigglypuff Life = 100 Jigglypuff Attack = 45
To give rein to the battle, it is handled in turns: one attacks, then another, then another, and then the other, and so on until ONE LOSES. How do we know which Pokémon's turn is? Let's see:
And how is this represented ?:
turn = 0 turn = 1
When it's Pikachu's turn, the variable turn It will have the value of 0, and when it is Jigglypuff it will be 1.
Mention that algebra is used a lot in programming. DO NOT PANIC!
Do you remember that in school I used to tell you:
x = 1 y = 1 z = x + y
How much was z worth? 3. We will use algebra as simple as this to make our battle algorithm. Let's go!
Ready? To fight!
turn = 0, it's Pikachu's turn, PUUUM !:
Jigglypuff.life = Jigglypuff.life - Pikachu.attack
What does this mean? Jigglypuff's health will decrease according to Pikachu's attack. Remember algebra, and that every object has value. Attribute lifetime = 100, and the attack for each Pokémon varies.
Pumm! Now Jigglypuff's life is 45.
Now? We evaluate if both Pokémon have life.
If Pokachu.life IS GREATER THAN 0 ZERO) & Jigglypuff.life IS GREATER THAN 0 ZERO) THEN GO ON!
Now him shift = 1, therefore it is the turn of Jigglypuff, he does his feat !:
Pikachu.life = Pikachu.life - Jigglypuff.attack
PUMMM! Pikachu now has 65 health.
Now what's next? Evaluate if they are still alive (which is true because Pikachu has 65 health and Jigglypuff has 45) and change the turn and go back to the beginning. This time, Jigglypuff has -10 to live!
Whats Next? Assess if someone is still alive:
If Pikachu.life IS GREATER THAN 0 ZERO) & Jigglypuff.life IS GREATER THAN 0 ZERO)
IT IS NOT FULFILLED! Jigglypuff is -10. Whats Next?:
If Pikachu.life IS LESS OR EQUAL TO 0 ZERO) THEN JIGGLYPUFF WINS!
But If not, PIKACHU WINS! And which one did he enter? IN THE SECOND CONDITION!
That's how simple it is to put together a Pokémon battle algorithm. Do you want to see the explanation of algorithms graphically with Professor Freddy Vega? You got it!: