Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _06._09._2019___Mid_Exam
- {
- class ManOWar
- {
- static void Main()
- {
- List<int> pirateShip = Console.ReadLine()
- .Split('>')
- .Select(int.Parse)
- .ToList();
- List<int> warShip = Console.ReadLine()
- .Split('>')
- .Select(int.Parse)
- .ToList();
- int maximumHealth = int.Parse(Console.ReadLine());
- string command = Console.ReadLine();
- while (command != "Retire")
- {
- string[] currentCommand = command.Split();
- string instruction = currentCommand[0];
- if (instruction == "Fire")
- {
- int index = int.Parse(currentCommand[1]);
- int dmg = int.Parse(currentCommand[2]);
- if (0 <= index && index < warShip.Count)
- {
- warShip[index] -= dmg;
- if (warShip[index] <= 0)
- {
- Console.WriteLine("You won! The enemy ship has sunken.");
- return;
- }
- }
- }
- else if (instruction == "Defend")
- {
- int startIndex = int.Parse(currentCommand[1]);
- int endIndex = int.Parse(currentCommand[2]);
- int dmg = int.Parse(currentCommand[3]);
- bool isStartValid = 0 <= startIndex && startIndex < endIndex;
- bool isEndValid = startIndex < endIndex && endIndex < pirateShip.Count;
- if (isStartValid && isEndValid)
- {
- for (int i = startIndex; i <= endIndex; i++)
- {
- pirateShip[i] -= dmg;
- if (pirateShip[i] <= 0)
- {
- Console.WriteLine("You lost! The pirate ship has sunken.");
- return;
- }
- }
- }
- }
- else if (instruction == "Repair")
- {
- int index = int.Parse(currentCommand[1]);
- int health = int.Parse(currentCommand[2]);
- bool isIndexValid = 0 <= index && index < pirateShip.Count;
- if (isIndexValid)
- {
- pirateShip[index] += health;
- if (pirateShip[index] > maximumHealth)
- {
- pirateShip[index] = maximumHealth;
- }
- }
- }
- else if (instruction == "Status")
- {
- int count = 0;
- double needRepair = maximumHealth * 0.20;
- for (int i = 0; i < pirateShip.Count; i++)
- {
- if (pirateShip[i] < needRepair)
- {
- count += 1;
- }
- }
- Console.WriteLine($"{count} sections need repair.");
- }
- command = Console.ReadLine();
- }
- bool isStalemate = pirateShip.Count > 0 && warShip.Count > 0;
- if (isStalemate)
- {
- Console.WriteLine($"Pirate ship status: {pirateShip.Sum()}");
- Console.WriteLine($"Warship status: {warShip.Sum()}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment