I know you said that there is no other way to do it. I hope you don’t mind that I took the liberty to do so, anyway. If you structure the code somehow like below, you will make future-you much happier:
I assume that you have the following variables: $shipLife
is the life amount of the player, $shipDamage
is the damage dealt by the planer, $enemyLife
is the life amount of the enemy and $enemyDamage
is the damage dealt by the enemy.
//advance time $t with a for loop at most until minute 15
for($t = 1; $t <= 15; ++$t)
{
//perform enemy attack
$shipLife -= $enemyDamage;
//check if the ship survived the attack
if($shipLife <= 0)
{
echo "You got destroyed in minute " . $t;
break;
}
//perform ship attack
$enemyLife -= $shipDamage;
//check if the enemy survived the attack
if($enemyLife <= 0)
{
echo "You destroyed the enemy in minute " . $t;
break;
}
}
//Check if both players are still alive
if($shipLife > 0 && $enemyLife > 0)
echo "Both ship and enemy are still alive after " . ($t - 1) . " minutes";
2
solved Simple mathematic solution doesnt work [closed]