Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _03_Lists_Man_O_War
- {
- 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 health = int.Parse(Console.ReadLine());
- string command = Console.ReadLine();
- while (command != "Retire")
- {
- string[] commandArr = command.Split();
- string instruction = commandArr[0];
- if (instruction == "Fire")
- {
- int index = int.Parse(commandArr[1]);
- int damage = int.Parse(commandArr[2]);
- if (index >= 0 && index <= warShip.Length - 1)
- {
- warShip[index] -= damage;
- if (warShip[index] <= 0)
- {
- Console.WriteLine("You won! The enemy ship has sunken");
- return;
- }
- }
- }
- else if (instruction == "Defend")
- {
- int startIndex = int.Parse(commandArr[1]);
- int endIndex = int.Parse(commandArr[2]);
- int damage = int.Parse(commandArr[3]);
- if (startIndex >= 0 && startIndex <= endIndex && endIndex <= pirateShip.Length - 1)
- {
- 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 if (instruction == "Repair")
- {
- int index = int.Parse(commandArr[1]);
- int givenHealth = int.Parse(commandArr[2]);
- if (index >= 0 && index <= pirateShip.Length - 1)
- {
- if (pirateShip[index] + givenHealth < health)
- {
- pirateShip[index] += givenHealth;
- }
- else
- {
- pirateShip[index] = health;
- }
- }
- }
- else if (instruction == "Status")
- {
- int count = 0;
- double precent = health * 0.2;
- foreach (var hl in pirateShip)
- {
- if (hl < precent)
- {
- count++;
- }
- }
- Console.WriteLine($"{count} sections need repair.");
- }
- command = Console.ReadLine();
- }
- Console.WriteLine($"Pirate ship status: {pirateShip.Sum()}");
- Console.WriteLine($"Warship status: {warShip.Sum()}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement