Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Numerics;
- namespace Lists
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] pirateShip = Console.ReadLine().Split(">").Select(int.Parse).ToArray();
- int[] warShip = Console.ReadLine().Split(">").Select(int.Parse).ToArray();
- int maxHealth = int.Parse(Console.ReadLine());
- string command = "";
- while (command != "Retire")
- {
- string[] commands = command.Split();
- if (commands[0] == "Fire")
- {
- int index = int.Parse(commands[1]);
- int damage = int.Parse(commands[2]);
- if (index >= 0 && index < warShip.Length)
- {
- warShip[index] -= damage;
- if (warShip[index] <= 0)
- {
- Console.WriteLine("You won! The enemy ship has sunken.");
- return;
- }
- }
- }
- else if (commands[0] == "Defend")
- {
- int startIndex = int.Parse(commands[1]);
- int endIndex = int.Parse(commands[2]);
- int damage = int.Parse(commands[3]);
- if (startIndex >= 0 && endIndex >= 0 &&
- startIndex < pirateShip.Length && endIndex < pirateShip.Length)
- {
- if (startIndex <= endIndex)
- {
- for (int i = startIndex; i <= endIndex; i++)
- {
- pirateShip[i] -= damage;
- if (pirateShip[i] < 0)
- {
- Console.WriteLine("You lost! The pirate ship has sunken.");
- return;
- }
- }
- }
- else
- {
- for (int i = endIndex; i <= startIndex; i++)
- {
- pirateShip[i] -= damage;
- if (pirateShip[i] < 0)
- {
- Console.WriteLine("You lost! The pirate ship has sunken.");
- return;
- }
- }
- }
- }
- }
- else if (commands[0] == "Repair")
- {
- int index = int.Parse(commands[1]);
- int health = int.Parse(commands[2]);
- Repair(index, health, pirateShip, maxHealth);
- }
- else if (commands[0] == "Status")
- {
- Status(pirateShip, maxHealth);
- }
- command = Console.ReadLine();
- }
- Console.WriteLine($"Pirate ship status: {pirateShip.Sum()}");
- Console.WriteLine($"Warship status: {warShip.Sum()}");
- }
- private static void Status(int[] pirateShip, int maxHealth)
- {
- int count = 0;
- foreach (int section in pirateShip)
- {
- if(section<0.2*maxHealth)
- {
- count++;
- }
- }
- Console.WriteLine($"{count} sections need repair.");
- }
- private static void Repair(int index, int health, int[] pirateShip, int maxHealth)
- {
- if (index>=0 && index<pirateShip.Length)
- {
- if (health + pirateShip[index] <= maxHealth)
- pirateShip[index] += health;
- else
- pirateShip[index] = maxHealth;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement