Advertisement
ralichka

Exam-06.August.2019-03.ManOWar

Oct 29th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03.ManOWar
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<int> pirateShip = Console.ReadLine()
  12.                 .Split(">")
  13.                 .Select(int.Parse)
  14.                 .ToList();
  15.  
  16.             List<int> warShip = Console.ReadLine()
  17.                 .Split(">")
  18.                 .Select(int.Parse)
  19.                 .ToList();
  20.  
  21.             int maxHealthCapacity = int.Parse(Console.ReadLine());
  22.             string[] command = Console.ReadLine().Split();
  23.  
  24.             string word = command[0];
  25.  
  26.  
  27.             while (word != "Retire")
  28.             {
  29.                 if (word == "Fire")
  30.                 {
  31.                     int index = int.Parse(command[1]);
  32.                     int damage = int.Parse(command[2]);
  33.                    
  34.  
  35.                     if (index >= 0 && index <= warShip.Count - 1)
  36.                     {
  37.                         int warShipInitial = warShip[index];
  38.                         warShip.RemoveAt(index);
  39.                         warShip.Insert(index, (warShipInitial - damage));
  40.                     }
  41.                     else
  42.                     {
  43.                         command = Console.ReadLine().Split();
  44.                         word = command[0];
  45.                         continue;
  46.                     }
  47.                     if (warShip[index] <= 0)
  48.                     {
  49.                         Console.WriteLine("You won! The enemy ship has sunken.");
  50.                         return;
  51.                     }
  52.                 }
  53.  
  54.                 if (word == "Defend")
  55.                 {
  56.                     int startIndex = int.Parse(command[1]);
  57.                     int endIndex = int.Parse(command[2]);
  58.                     int damage = int.Parse(command[3]);
  59.  
  60.  
  61.                     if ((startIndex >= 0 && startIndex <= pirateShip.Count - 1) && (endIndex >= 0 && endIndex <= pirateShip.Count - 1))
  62.                     {
  63.                         for (int i = startIndex; i <= endIndex; i++)
  64.                         {
  65.                             int pirateShipInitial = pirateShip[i];
  66.                             pirateShip.RemoveAt(i);
  67.                             pirateShip.Insert(i, (pirateShipInitial - damage));
  68.  
  69.                             if (pirateShip[i] <= 0)
  70.                             {
  71.                                 Console.WriteLine("You lost! The pirate ship has sunken.");
  72.                                 return;
  73.                             }
  74.                         }
  75.                     }
  76.                     else
  77.                     {
  78.                         command = Console.ReadLine().Split();
  79.                         word = command[0];
  80.                         continue;
  81.                     }
  82.                 }
  83.  
  84.                 if (word == "Repair")
  85.                 {
  86.                     int index = int.Parse(command[1]);
  87.                     int health = int.Parse(command[2]);
  88.  
  89.                     if (index >= 0 && index <= pirateShip.Count - 1)
  90.                     {
  91.                         if (pirateShip[index] + health > maxHealthCapacity)
  92.                         {
  93.                             pirateShip[index] = maxHealthCapacity;
  94.                         }
  95.                         else
  96.                         {
  97.                             pirateShip[index] += health;
  98.                         }
  99.                     }
  100.                     else
  101.                     {
  102.                         command = Console.ReadLine().Split();
  103.                         word = command[0];
  104.                         continue;
  105.                     }
  106.                 }
  107.  
  108.                 if(word == "Status")
  109.                 {
  110.                     int counter = 0;
  111.  
  112.                     for (int i = 0; i < pirateShip.Count; i++)
  113.                     {
  114.                         if (pirateShip[i] < maxHealthCapacity / 5)
  115.                         {
  116.                             counter++;
  117.                         }
  118.                     }
  119.                     Console.WriteLine($"{counter} sections need repair.");
  120.                 }
  121.  
  122.  
  123.                 command = Console.ReadLine().Split();
  124.                 word = command[0];
  125.             }
  126.  
  127.             Console.WriteLine($"Pirate ship status: { pirateShip.Sum()}");
  128.             Console.WriteLine($"Warship status: {warShip.Sum()}");
  129.  
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement