Advertisement
Guest User

Untitled

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