while循环不能正常工作

时间:2014-10-18 10:19:14

标签: php loops if-statement while-loop

结果是这样的:

  1. 你击中了敌人,造成8点伤害,敌人的伤害为92马力
  2. 敌人击中了你的伤害,你有86马力的伤害
  3. 你击中了敌人,造成5点伤害,敌人还有81马力
  4. 敌人击中你,造成11点伤害,你还有72马力
  5. 你击中敌人造成12点伤害,敌人的伤害为67马力
  6. 敌人为你造成7点伤害,你有60马力的伤害
  7. 你击中了敌人,造成13点伤害,敌人还剩下40马力
  8. 敌人打你11点伤害你还有32马力
  9. 你击中敌人造成2点伤害,敌人还有33马力
  10. 敌人击中了你的15点伤害,你还有16马力
  11. 你击中了敌人15点伤害敌人有2马力
  12. 敌人击中了你还有-11马力的17点伤害
  13. 你死了:-11马力敌人赢了!

    Enemy hp是:-8你赢了!你获得50 exp!

    两者都有100马力,正如你在第2行所看到的那样,当敌人击中8点伤害并且剩下86马力时,有一个“循环”丢失。

    当其中一个死亡时,如何停止循环,以便两个同时死亡?因此,如果一个人死了,那么循环就会停止并确定胜利者。

    有没有更好的方法来确定从随机开始?

    <?php
    
    $p_hp = 100;
    $p_atk = 20;
    $p_defence = 5;
    
    $m_hp = 100;
    $m_atk = 20;
    $m_defence = 5;
    
    if ($p_hp > 0) {
    
        $turn = rand(0,1);
        $hitnr = 0;
    
        while (($p_hp >= 0) && ($m_hp >= 0)) {
    
            $turn--;
            $hitnr++;
    
            $enemydef = $p_atk - $m_defence / 2;
            $my_damage = rand(1, $enemydef);
    
            $playerdef = $m_atk - $p_defence / 2;
            $enemy_damage = rand(1, $playerdef);
    
            $p_hp = $p_hp - $enemy_damage;
            $m_hp = $m_hp - $my_damage;
    
            // Player turn
            if ($turn % 2 == 0) {
                echo "<b>".$hitnr.".</b> Enemy hit you for  <font color='red'><b>".$enemy_damage . "</b></font> damage you have <b>".$p_hp." hp</b> left<br>";    
    
            // Monster turn    
            } else {
                echo "<b>".$hitnr.".</b> You hit enemy for  <font color='green'><b>".$my_damage . "</b></font> damage enemy has <b>".$m_hp." hp</b> left<br>";
            }
    
            if ($p_hp <= 0) {
                echo "<br>You died: ".$p_hp." hp<br><font color='red'><b>Enemy wins!</b></font>";
             }
    
            if ($m_hp <= 0) {
                echo "<br>Enemy hp is: ".$m_hp."<br><font color='green'><b>You win! You gain 50 exp!</b></font>";
    
            }
        }
    } else {
        echo "You are dead. <a href='hospital.php'>Go to the hospital!</a>";
    }
    
    ?>
    

1 个答案:

答案 0 :(得分:0)

除非怪物(或玩家)能够反击,否则只有目标公司的HP会减少:

$p_hp = 100;
$p_atk = 20;
$p_defence = 5;

$m_hp = 100;
$m_atk = 20;
$m_defence = 5;

if ($p_hp >= 0) {

    $turn = rand(0,1);
    $hitnr = 0;

    while (($p_hp >= 0) && ($m_hp >= 0)) {

        $turn--;
        $hitnr++;

        $enemydef = $p_atk - $m_defence / 2;
        $my_damage = rand(1, $enemydef);

        $playerdef = $m_atk - $p_defence / 2;
        $enemy_damage = rand(1, $playerdef);

        // Monster turn
        if ($turn % 2 == 0) {
            $p_hp = $p_hp - $enemy_damage;
            echo "<b>".$hitnr.".</b> Enemy hit you for  <font color='red'><b>".$enemy_damage . "</b></font> damage you have <b>".$p_hp." hp</b> left<br>";    

        // Player turn    
        } else {
            $m_hp = $m_hp - $my_damage;
            echo "<b>".$hitnr.".</b> You hit enemy for  <font color='green'><b>".$my_damage . "</b></font> damage enemy has <b>".$m_hp." hp</b> left<br>";
        }

        if ($p_hp <= 0) {
            echo "<br>You died: ".$p_hp." hp<br><font color='red'><b>Enemy wins!</b></font>";
         }

        if ($m_hp <= 0) {
            echo "<br>Enemy hp is: ".$m_hp."<br><font color='green'><b>You win! You gain 50 exp!</b></font>";

        }
    }
} else {
    echo "You are dead. <a href='hospital.php'>Go to the hospital!</a>";
}

?>