I need to create a player with 3 properties:
- Life (must be a random integer from 1 to 100);
- Attack (must be a random integer from 1 to 100);
- Defense (must be a random integer from 1 to 100); But the total sum of all properties must be 200 (Life + Attack + Defense = 200). No more or less. I’ve tried to make something like this:
public function initPlayer(){
$life = random_int(1, 100);
$attack = random_int(1, 100);
$lifeAttack = $life + $attack;
if($lifeAttack >= 100) {
$defense = 200 - $lifeAttack;
} else {
$defense = random_int(1, 100);
}
echo $life . '-' . $attack . '-' . $defense;
}
But it works correctly only if the sum of two params is equal or more than 100.