Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.1 kB
2
Indexable
Never
<?php
// Enter your code here, enjoy!
$player1 = array
(
    "health"=>rand(70, 100), 
    "strength"=>rand(70, 80), 
    "defence"=>rand(45, 55), 
    "speed"=>rand(40, 50), 
    "luck"=>rand(10, 30), 
    "type" => 'Orderus',
);

$player2 = array
(
    "health"=>rand(60, 90), 
    "strength"=>rand(60, 90), 
    "defence"=>rand(40, 60),
    "speed"=>rand(40, 60), 
    "luck"=>rand(25, 40), 
    "type" => 'Beast',
);

$noOfTurns = 1;

echo 'Orderus initial stats: ' . PHP_EOL;
foreach ($player1 as $key => $value) {
    echo $key . ': ' . $value . PHP_EOL;
}
echo PHP_EOL;
echo 'Beast initial stats: ' . PHP_EOL;
foreach ($player2 as $key => $value) {
    echo $key . ': ' . $value . PHP_EOL;
}
echo PHP_EOL;

// function swithRoles($player1, $player2) {
//     $player3 = $player1;
//     $player1 = $player2;
//     $player2 = $player3;
// }

if ($player1['speed'] < $player2['speed']) {
    $player3 = $player1;
    $player1 = $player2;
    $player2 = $player3;
} elseif 
(
    $player1['speed'] === $player2['speed'] &&
    $player1['luck'] < $player2['luck']
) {
    $player3 = $player1;
    $player1 = $player2;
    $player2 = $player3;
}

while ($noOfTurns <= 20 && ($player1['health'] > 0 || $player2['health'] > 0)) {
// for ($noOfTurns = 1; $noOfTurns <= 20; $noOfTurns++) {
    echo 'Round: ' . $noOfTurns . PHP_EOL;
    echo 'Attacking player: ' . $player1['type'] . ' health: ' . $player1['health'] . PHP_EOL;
    echo 'Defending player: ' . $player2['type'] . ' health: ' . $player2['health'] . PHP_EOL;
    echo 'Attacking player: ' . $player1['type'] . ' luck: ' . $player1['luck'] . PHP_EOL;
    echo 'Defending player: ' . $player2['type'] . ' luck: ' . $player2['luck'] . PHP_EOL;

    if ($player1['type'] === 'Orderus') {
        //if Orderus is the attacker
        if ($player1['luck'] < $player2['luck']) {
            echo $player1['type'] . ' missed because ' . $player2['type'] . ' got lucky this turn.' . PHP_EOL;
        } else {
            //calculate Orderus rapid strike chance
            $noOfAttacks = 1;
            $useRapidStrike = false;
            $luck = rand(1, 10);
            if ($luck > 9) {
                $useRapidStrike = true;
            }
            if ($useRapidStrike) {
                echo 'Orderus use rapid strike.' . PHP_EOL;
                $noOfAttacks = 2;
            }
            for ($i = 1; $i <= $noOfAttacks; $i++) {
                $damage = $player1['strength'] - $player2['defence'];
                echo 'Damage taken by ' . $player2['type'] . ': ' . $damage . PHP_EOL;
                $player2['health'] -= $damage;
            }
        }
    } else {
        //if Beast is the attacker
        if ($player1['luck'] < $player2['luck']) {
            echo $player1['type'] . ' missed because ' . $player2['type'] . ' got lucky this turn.' . PHP_EOL;
        } else {
            $damage = $player1['strength'] - $player2['defence'];
            //calculate Orderus magic shield chance
            $useMagicShield = false;
            $luck = rand(1, 10);
            if ($luck > 8) {
                $useMagicShield = true;
            }
            if ($useMagicShield) {
                echo 'Orderus use magic shield.' . PHP_EOL;
                $damage /= 2;
            }
            $player2['health'] -= $damage;
            echo 'Damage taken by ' . $player2['type'] . ': ' . $damage . PHP_EOL;
        }
    }
    // echo 'before switch roles player1 type: ' . $player1['type'] . PHP_EOL; 
    //switch roles
    $player3 = $player1;
    $player1 = $player2;
    $player2 = $player3;
    // echo 'after switch roles player1 type: ' . $player1['type'] . PHP_EOL; 

    if ($player1['type'] === 'Orderus') {
        $player1['luck'] = rand(10, 30);
        $player2['luck'] = rand(25, 40);
    } else {
        $player1['luck'] = rand(25, 40);
        $player2['luck'] = rand(10, 30);
    }
    echo $noOfTurns . PHP_EOL;
    $noOfTurns++;
    echo PHP_EOL;
}