Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace ManOWar
- {
- class Program
- {
- static bool IsIndexValid(int i, List<int> l)
- {
- return i >= 0 && i < l.Count;
- }
- static void Main(string[] args)
- {
- var pirateShip = Console.ReadLine().Split('>').Select(int.Parse).ToList();
- var warShip = Console.ReadLine().Split('>').Select(int.Parse).ToList();
- int maxSectionHealth = int.Parse(Console.ReadLine());
- string input;
- while ((input = Console.ReadLine()) != "Retire")
- {
- var tokens = input.Split(' ');
- string command = tokens[0];
- if (command == "Fire")
- {
- int index = int.Parse(tokens[1]);
- if (IsIndexValid(index, warShip))
- {
- int damage = int.Parse(tokens[2]);
- warShip[index] -= damage;
- if (warShip[index] <= 0)
- {
- Console.WriteLine("You won! The enemy ship has sunken.");
- warShip.Clear();
- break;
- }
- }
- }
- else if (command == "Defend")
- {
- int startIndex = int.Parse(tokens[1]);
- int finalIndex = int.Parse(tokens[2]);
- if (IsIndexValid(startIndex, pirateShip) && IsIndexValid(finalIndex, pirateShip))
- {
- int damage = int.Parse(tokens[3]);
- for (int i = startIndex; i <= finalIndex; i++)
- {
- pirateShip[i] -= damage;
- if (pirateShip[i] <= 0)
- {
- Console.WriteLine("You lost! The pirate ship has sunken.");
- pirateShip.Clear();
- Environment.Exit(0);
- }
- }
- }
- }
- else if (command == "Repair")
- {
- int index = int.Parse(tokens[1]);
- if (IsIndexValid(index, pirateShip)) {
- int health = int.Parse(tokens[2]);
- pirateShip[index] += health;
- if (pirateShip[index] > maxSectionHealth)
- {
- pirateShip[index] = maxSectionHealth;
- }
- }
- }
- else if (command == "Status")
- {
- int count = pirateShip.Where(x => x < maxSectionHealth / 5).Count();
- Console.WriteLine($"{count} sections need repair.");
- }
- }
- if (pirateShip.Count > 0 && warShip.Count > 0)
- {
- Console.WriteLine($"Pirate ship status: {pirateShip.Sum()}\nWarship status: {warShip.Sum()}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement