Esempio n. 1
0
 /**
  * Our complex fighting algorithm!
  *
  * @return BattleResult
  */
 public function battle(Ship $ship1, $ship1Quantity, Ship $ship2, $ship2Quantity)
 {
     $ship1Health = $ship1->getStrength() * $ship1Quantity;
     $ship2Health = $ship2->getStrength() * $ship2Quantity;
     $ship1UsedJediPowers = false;
     $ship2UsedJediPowers = false;
     while ($ship1Health > 0 && $ship2Health > 0) {
         // first, see if we have a rare Jedi hero event!
         if ($this->didJediDestroyShipUsingTheForce($ship1)) {
             $ship2Health = 0;
             $ship1UsedJediPowers = true;
             break;
         }
         if ($this->didJediDestroyShipUsingTheForce($ship2)) {
             $ship1Health = 0;
             $ship2UsedJediPowers = true;
             break;
         }
         // now battle them normally
         $ship1Health = $ship1Health - $ship2->getWeaponPower() * $ship2Quantity;
         $ship2Health = $ship2Health - $ship1->getWeaponPower() * $ship1Quantity;
     }
     $ship1->setStrength($ship1Health);
     $ship2->setStrength($ship2Health);
     if ($ship1Health <= 0 && $ship2Health <= 0) {
         // they destroyed each other
         $winningShip = null;
         $losingShip = null;
         $usedJediPowers = $ship1UsedJediPowers || $ship2UsedJediPowers;
     } elseif ($ship1Health <= 0) {
         $winningShip = $ship2;
         $losingShip = $ship1;
         $usedJediPowers = $ship2UsedJediPowers;
     } else {
         $winningShip = $ship1;
         $losingShip = $ship2;
         $usedJediPowers = $ship1UsedJediPowers;
     }
     return new BattleResult($usedJediPowers, $winningShip, $losingShip);
 }