Advertisement
RGeorgiev97

Untitled

Feb 24th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.14 KB | None | 0 0
  1. <?php
  2. $pirateShip = array_map('intval',explode('>',readline()));
  3. $warShip = array_map('intval',explode('>',readline()));
  4. $maximumHealth = intval(readline());
  5. $input = readline();
  6. while ($input!=='Retire'){
  7.     $args = explode(" ", $input);
  8.     $action = $args[0];
  9.  
  10.     switch ($action){
  11.         case 'Fire':
  12.             $index = $args[1];
  13.             $damage = $args[2];
  14.             if ($index>=0 && $index <count($warShip)){
  15.                 $warShip[$index]-=$damage;
  16.                 if ($warShip[$index]<=0){
  17.                     echo "You won! The enemy ship has sunken." .PHP_EOL;
  18.                     exit();
  19.                 }
  20.             }
  21.             break;
  22.         case 'Defend':
  23.             $startIndex = $args[1];
  24.             $endIndex = $args[2];
  25.             $damage = $args[3];
  26.             if ($startIndex >=0 && $startIndex <count($pirateShip) && $endIndex >=0 && $endIndex <count($pirateShip)){
  27.                 for($i = $startIndex; $i <= $endIndex; $i++) {
  28.                     $pirateShip[$i] -=$damage;
  29.                     if ($pirateShip[$i]<=0){
  30.                         echo "You lost! The pirate ship has sunken.".PHP_EOL;
  31.                         exit();
  32.                     }
  33.                 }
  34.             }
  35.             break;
  36.         case 'Repair':
  37.             $index = $args[1];
  38.             $health = $args[2];
  39.             if ($index>=0 && $index <count($pirateShip)){
  40.                 $pirateShip[$index] +=$health;
  41.                 if ($pirateShip[$index]>$maximumHealth){
  42.                     $pirateShip[$index]=$maximumHealth;
  43.                 }
  44.             }
  45.             break;
  46.         case 'Status':
  47.             $needRepair = 0;
  48.             for($i = 0; $i < count($pirateShip); $i++) {
  49.                 if ($pirateShip[$i]<$maximumHealth*0.2){
  50.                     $needRepair+=1;
  51.                 }
  52.             }
  53.             echo "$needRepair sections need repair." .PHP_EOL;
  54.             break;
  55.     }
  56.  
  57.     $input =readline();
  58. }
  59. $pirateShipStatus = array_sum($pirateShip);
  60. $warShipStatus = array_sum($warShip);
  61. echo "Pirate ship status: $pirateShipStatus" .PHP_EOL;
  62. echo "Warship status: $warShipStatus" .PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement