Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $pirateShip = array_map('intval',explode('>',readline()));
- $warShip = array_map('intval',explode('>',readline()));
- $maximumHealth = intval(readline());
- $input = readline();
- while ($input!=='Retire'){
- $args = explode(" ", $input);
- $action = $args[0];
- switch ($action){
- case 'Fire':
- $index = $args[1];
- $damage = $args[2];
- if ($index>=0 && $index <count($warShip)){
- $warShip[$index]-=$damage;
- if ($warShip[$index]<=0){
- echo "You won! The enemy ship has sunken." .PHP_EOL;
- exit();
- }
- }
- break;
- case 'Defend':
- $startIndex = $args[1];
- $endIndex = $args[2];
- $damage = $args[3];
- if ($startIndex >=0 && $startIndex <count($pirateShip) && $endIndex >=0 && $endIndex <count($pirateShip)){
- for($i = $startIndex; $i <= $endIndex; $i++) {
- $pirateShip[$i] -=$damage;
- if ($pirateShip[$i]<=0){
- echo "You lost! The pirate ship has sunken.".PHP_EOL;
- exit();
- }
- }
- }
- break;
- case 'Repair':
- $index = $args[1];
- $health = $args[2];
- if ($index>=0 && $index <count($pirateShip)){
- $pirateShip[$index] +=$health;
- if ($pirateShip[$index]>$maximumHealth){
- $pirateShip[$index]=$maximumHealth;
- }
- }
- break;
- case 'Status':
- $needRepair = 0;
- for($i = 0; $i < count($pirateShip); $i++) {
- if ($pirateShip[$i]<$maximumHealth*0.2){
- $needRepair+=1;
- }
- }
- echo "$needRepair sections need repair." .PHP_EOL;
- break;
- }
- $input =readline();
- }
- $pirateShipStatus = array_sum($pirateShip);
- $warShipStatus = array_sum($warShip);
- echo "Pirate ship status: $pirateShipStatus" .PHP_EOL;
- echo "Warship status: $warShipStatus" .PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement