I just started https://warriorjs.com/. I'm on level 5 now, I can do this by doing a million if else statements, but I saw people make separate functions, to make it easier to work and read later on.
class Player {
/**
* Plays a warrior turn.
*
* @param {Warrior} warrior The warrior.
*/
playTurn(warrior) {
// Cool code goes here
if (this.health > warrior.health()) {
if (warrior.feel().isEmpty()) {
warrior.walk();
} else {
isCaptive(warrior);
}
} else {
if ((warrior.health() !== warrior.maxHealth()) && warrior.feel().isEmpty()) {
warrior.rest();
} else if (warrior.feel().isEmpty()) {
warrior.walk();
} else {
warrior.attack();
}
}
this.health = warrior.health();
}
isCaptive(warrior) {
if (warrior.feel().getUnit().isEnemy()) {
warrior.attack();
} else if (warrior.feel().getUnit().isBound()) {
warrior.rescue();
}
}
}