Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. namespace Man_O_War
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string input = string.Empty;
  13.             int[] pirateShip = Console.ReadLine().Split(">").Select(int.Parse).ToArray();
  14.             int[] warship = Console.ReadLine().Split(">").Select(int.Parse).ToArray();
  15.             int maximumHealth = int.Parse(Console.ReadLine());
  16.  
  17.             while ((input = Console.ReadLine()) != "Retire")
  18.             {
  19.                 string[] tokens = input.Split();
  20.                 string command = tokens[0];
  21.  
  22.                 if (command == "Fire")
  23.                 {
  24.                     int index = int.Parse(tokens[1]);
  25.                     int damage = int.Parse(tokens[2]);
  26.                     if (index >= 0 && index < warship.Length)
  27.                     {
  28.                         warship[index] -= damage;
  29.                         if (warship[index] <= 0)
  30.                         {
  31.                             Console.WriteLine("You won! The enemy ship has sunken.");
  32.                             return;
  33.                         }
  34.                     }
  35.                 }
  36.                 else if (command == "Defend")
  37.                 {
  38.                     int startIndex = int.Parse(tokens[1]);
  39.                     int endIndex = int.Parse(tokens[2]);
  40.                     int damage = int.Parse(tokens[3]);
  41.                     if (startIndex >= 0 && startIndex <= pirateShip.Length)
  42.                     {
  43.                         if (endIndex >= startIndex && endIndex <= pirateShip.Length)
  44.                         {
  45.                             for (int i = startIndex; i <= endIndex; i++) // damage range
  46.                             {
  47.                                 pirateShip[i] -= damage;
  48.                                 if (pirateShip[i] <= 0)
  49.                                 {
  50.                                     Console.WriteLine("You lost! The pirate ship has sunken.");
  51.                                     return;
  52.                                 }
  53.                             }
  54.                         }
  55.                     }
  56.                 }
  57.                 else if (command == "Repair")
  58.                 {
  59.                     int index = int.Parse(tokens[1]);
  60.                     int health = int.Parse(tokens[2]);
  61.                     if (index >= 0 && index < pirateShip.Length)
  62.                     {
  63.                         if (health <= maximumHealth)
  64.                         {
  65.                             pirateShip[index] += health;
  66.                         }
  67.                     }
  68.                 }
  69.                 else if (command == "Status")
  70.                 {
  71.                     int counter = 0;
  72.                     for (int i = 0; i < pirateShip.Length; i++)
  73.                     {
  74.                         if (pirateShip[i] < maximumHealth * 0.2)
  75.                         {
  76.                             counter++;
  77.                         }
  78.                     }
  79.                     Console.WriteLine($"{counter} sections need repair.");
  80.                 }
  81.             }
  82.             int pirateShipSum = pirateShip.Sum();
  83.             int warshipSum = warship.Sum();
  84.             Console.WriteLine($"Pirate ship status: {pirateShipSum}");
  85.             Console.WriteLine($"Warship status: {warshipSum}");
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement