コード例 #1
0
 $weaponIndex = array_rand($weapons, 1);
 $player->buy($weapons[$weaponIndex]);
 // buy 0 or 1 armor
 $numArmors = rand(0, 1);
 if ($numArmors == 1) {
     $armorIndex = array_rand($armors, 1);
     $player->buy($armors[$armorIndex]);
 }
 // buy 0, 1 or 2 rings
 $numRings = rand(0, 2);
 for ($i = 0; $i < $numRings; $i++) {
     $ringIndex = array_rand($rings, 1);
     $player->buy($rings[$ringIndex]);
 }
 $playerWins = false;
 while ($boss->getHitpoints() > 0 && $player->getHitpoints() > 0) {
     $player->attack($boss);
     // boss can only attack back if he is alive
     if ($boss->isAlive()) {
         $boss->attack($player);
     }
 }
 if ($player->isAlive()) {
     if (!isset($leastSpent) || $player->getSpentCash() < $leastSpent) {
         $leastSpent = $player->getSpentCash();
         echo 'least spent: ' . $leastSpent . ' for a victory (part 1)' . PHP_EOL;
     }
 } else {
     if (!isset($mostSpent) || $player->getSpentCash() > $mostSpent) {
         $mostSpent = $player->getSpentCash();
         echo 'most spent: ' . $mostSpent . ' for a defeat (part 2)' . PHP_EOL;