每5个循环多循环一行.由玩家的速度决定


Loop one extra row every 5 loops. Determined by speed of player

我正试图弄清楚如何让玩家拥有比对手多2倍的速度。每5次攻击一次额外攻击。但是我想不出一个好的方法来计算。

<?php
$monster_speed = 10;
$player_speed = 12;
$turn = mt_rand(0,1);
$hitnum = 1;
while($hitnum <= 15) {
    if ($turn % 2 == 0) {
        echo $hitnum. ". Monster attacked<br>";
        $hitnum++;
        $turn++;
    } else {
        echo $hitnum. ". Player attacked<br>";
        $hitnum++;
        $turn++;
    }
}
?>
输出:

1. Player attacked
2. Monster attacked
3. Player attacked
4. Monster attacked
5. Player attacked
6. Monster attacked
7. Player attacked
8. Monster attacked
9. Player attacked
10. Monster attacked
11. Player attacked
12. Monster attacked
13. Player attacked
14. Monster attacked
15. Player attacked

进球(如果玩家的速度是12,敌人的速度是10):

1. Player attacked
2. Monster attacked
3. Player attacked
4. Monster attacked
5. Player attacked
6. Monster attacked
7. Player attacked
8. Monster attacked
9. Player attacked
10. Player attacked
11. Monster attacked
12. Player attacked
13. Monster attacked
14. Player attacked
15. Monster attacked

看看这样做是否更好。这可能需要更多的计算,但这种方法可能会给你一些启发。如果没有,没问题,我收回它!我要提一下,这是一种非对称算法因为命中会向先命中的人倾斜所以在这种情况下,它可能是13 vs 712 vs 8点结果:

<?php
// this just sees what the mathematical diff is between the p1 and p2
// also taking into consideration your iterations (length)
function offset_hit($pspeed = 0,$mspeed = 0,$length = 0)
    {
        $diff   =   ($pspeed - $mspeed);
        return ($diff != 0)? floor(($length / $diff)) : false;
    }
// This just sees how many more times a player should strike per
// iteration
function disparity($p1 = 0,$p2 = 0)
    {
        // I just floor rounded this but you could make this more 
        // complex to determine value...
        return ($p1 >= $p2)? floor($p1 / $p2) : floor($p2 / $p1);
    }
// Create a function to both output and return the results
// This allows for future hit-point processing...etc.
function FightToTheDeath($p1,$p2,$settings = false) {
        // The length (loops)
        $length =   (!empty($settings['length']) && is_numeric($settings['length']))? $settings['length'] : 10;
        $hitnum =   (!empty($settings['hitnum']) && is_numeric($settings['hitnum']))? $settings['hitnum'] : 1;
        $turn   =   mt_rand(0,1);
        // Percentage difference in overall character vs character
        $disp   =   disparity($p1,$p2);
        // This checks at what interval the disparity will occur
        $offset =   offset_hit($p2,$p1,$length);
        $start  =   $i = 1;
        while($hitnum <= $length) {
                if ($turn % 2 == 0) {
                        // I have the turn taking based off the player so there
                        // are some ifs here for monster
                        if(($offset != 0) || ($start == 1) || !$offset) {
                                $order[]    =   "m";
                                echo $hitnum. ". Monster attacked<br>";
                            }
                    }
                else {
                        $order[]    =   "p";
                        echo $hitnum. ". Player attacked<br>";
                    }
                // Calculate the extra hits here.
                if(($i == str_replace("-","",$offset)) || $disp > 1) {
                        // This is how many extra a turn character can attack
                        // This takes into account they already struck once
                        $disparity  =   (($disp - 1) <= 0)? 1 : $disp-1;
                        // Loop through remaining hits
                        for($a = 1; $a <= $disparity; $a++) {
                                $order[]    =   ($offset < 0)? "m" : "p";
                                echo ($offset < 0)? "+Speed. Monster attacked<br>" : "+Speed. Player attacked<br>";
                            }
                        // Rest iterator counter.   
                        $i  =   0;
                    }
                // You only need these once.
                $hitnum++;
                $turn++;
                $i++;
            }
        return (!empty($order))? $order : array();
    }
?>
使用

>

<?php
// Include the above functions...
// Monster's speed
$mSpeed =   15;
// Player's speed
$pSpeed =   10;
// Write out the value, but also assign scores
$playByplay =   FightToTheDeath($mSpeed,$pSpeed,array("length"=>15,"hitnum"=>1));
// Count how many times each player is hit
// You can now feed this into a new function that tallies up
// total damage and such and such...
$scorecard  =   array_count_values($playByplay);
print_r($scorecard);
?>

1。怪物攻击
2.玩家被攻击
3.怪物攻击
+速度。怪物攻击
4.玩家被攻击
5.怪物攻击
6.玩家被攻击
+速度。怪物攻击
7.怪物攻击
8.玩家被攻击
9.怪物攻击
+速度。怪物攻击
10.玩家被攻击
11.怪物攻击
12.玩家被攻击
+速度。怪物攻击
13.怪物攻击
14.玩家被攻击
15.怪物攻击
+速度。怪物攻击

Array
(
    [m] => 13
    [p] => 7
)

结果不是100%像你看到的那样,但是,你可以通过在循环中使用continue来创建相同的东西。

$monster_speed = 10;
    $player_speed = 12;
    $turn = mt_rand(0,1);
    $monsterHitCount = 0;
    $playerHitCount = 0;
    $hitnum = 1;
    while($hitnum <= 15) {
        if ($turn % 2 == 0) {
            echo $hitnum. ". Monster attacked<br>";
            $hitnum++;
            $turn++;
            $monsterHitCount ++;
            if($monsterHitCount % 5 === 0 && $monster_speed > $player_speed) {
                echo $hitnum. ". Monster attacked<br>";
                $hitnum++;
            }
        } else {
            echo $hitnum. ". Player attacked<br>";
            $hitnum++;
            $turn++;
            $playerHitCount ++;
            if($playerHitCount % 5 === 0 && $monster_speed < $player_speed) {
                echo $hitnum. ". Player attacked<br>";
                $hitnum++;
            }
        }
    }