Advertisement
silvana1303

man-o-war

Jun 15th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Threading;
  6.  
  7. namespace ConsoleApp1
  8. {
  9.     class Exam
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             // fire - warship
  14.             // defend - pirate ship
  15.             // repair - pirate ship
  16.             // status - both
  17.  
  18.             List<int> pirate = Console.ReadLine().Split('>').Select(int.Parse).ToList();
  19.  
  20.             List<int> war = Console.ReadLine().Split('>').Select(int.Parse).ToList();
  21.  
  22.             int maxHealth = int.Parse(Console.ReadLine());
  23.  
  24.             string[] command = Console.ReadLine().Split().ToArray();
  25.  
  26.             while (command[0] != "Retire")
  27.             {
  28.                 if (command[0] == "Fire")
  29.                 {
  30.                     int index = int.Parse(command[1]);
  31.  
  32.                     if (index >= 0 && index < war.Count)
  33.                     {
  34.                         int damage = int.Parse(command[2]);
  35.  
  36.                         war[index] -= damage;
  37.  
  38.                         if (war[index] <= 0)
  39.                         {
  40.                             Console.WriteLine("You won! The enemy ship has sunken.");
  41.                             break;
  42.                         }
  43.                     }
  44.                 }
  45.                 if (command[0] == "Defend")
  46.                 {
  47.                     bool won = false;
  48.                     int startIndex = int.Parse(command[1]);
  49.                     int endIndex = int.Parse(command[2]);
  50.  
  51.                     if ((startIndex >= 0 && startIndex < pirate.Count) && endIndex >= 0 && endIndex < pirate.Count)
  52.                     {
  53.                         int damage = int.Parse(command[3]);
  54.                         for (int i = startIndex; i <= endIndex; i++)
  55.                         {
  56.                             pirate[i] -= damage;
  57.  
  58.                             if (pirate[i] <= 0)
  59.                             {
  60.                                 Console.WriteLine("You lost! The pirate ship has sunken.");
  61.                                 won = true;
  62.                                 break;
  63.                             }
  64.                         }
  65.                         if (won)
  66.                         {
  67.                             break;
  68.                         }
  69.                     }
  70.                 }
  71.                 if (command[0] == "Repair")
  72.                 {
  73.                     int index = int.Parse(command[1]);
  74.  
  75.                     if (index >= 0 && index < pirate.Count)
  76.                     {
  77.                         int health = int.Parse(command[2]);
  78.  
  79.                         pirate[index] += health;
  80.  
  81.                         if (pirate[index] > maxHealth)
  82.                         {
  83.                             pirate[index] = maxHealth;
  84.                         }
  85.                     }
  86.                 }
  87.                 if (command[0] == "Status")
  88.                 {
  89.                     int count = 0;
  90.                     for (int i = 0; i < pirate.Count; i++)
  91.                     {
  92.                         if (pirate[i] < maxHealth * 0.20)
  93.                         {
  94.                             count++;
  95.                         }
  96.                     }
  97.  
  98.                     Console.WriteLine($"{count} sections need repair.");
  99.                 }
  100.  
  101.                 command = Console.ReadLine().Split().ToArray();
  102.             }
  103.  
  104.             if (command[0] == "Retire")
  105.             {
  106.                 Console.WriteLine($"Pirate ship status: {pirate.Sum()}");
  107.                 Console.WriteLine($"Warship status: {war.Sum()}");
  108.  
  109.             }
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement