Man O War - Mid Exam Retake - 6 August 2019
I apologize, but can somebody please tell me why my code is not working? I have watched video lecture about this exercise https://softuni.bg/trainings/resources/video/44085/video-30-october-2019-slavi-kapsalov-csharp-fundamentals-september-2019/2438 , debugging and looked through all the code of my colleagues and result is 80/100. I can't understand how I scored only 80/100 for this exercise only if tests in Judge have been changed.
Man O War: https://judge.softuni.bg/Contests/Practice/Index/1773#2
using System;
using System.Collections.Generic;
using System.Linq;
namespace midExam
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            List<int> PirateShip = Console.ReadLine().Split('>').Select(int.Parse).ToList();
            List<int> WarShip = Console.ReadLine().Split('>').Select(int.Parse).ToList();
            int maxHealthCapacity = int.Parse(Console.ReadLine());
            string command = string.Empty;
            while ((command = Console.ReadLine()) != "Retire")
            {
                string[] commandArray = command.Split();
                if (commandArray[0] == "Fire")
                {
                    int index = int.Parse(commandArray[1]);
                    int damage = int.Parse(commandArray[2]);
                    if (index >= 0 && index <= PirateShip.Count - 1 && index <= WarShip.Count - 1)
                    {
                        WarShip[index] = WarShip[index] - damage;
                        if (WarShip[index] <= 0)
                        {
                            Console.WriteLine("You won! The enemy ship has sunken");
                            return;
                        }
                    }
                }
                else if (commandArray[0] == "Defend")
                {
                    int startIndex = int.Parse(commandArray[1]);
                    int endIndex = int.Parse(commandArray[2]);
                    int damage = int.Parse(commandArray[3]);
                    if (startIndex >= 0 && startIndex<=PirateShip.Count-1 && endIndex>=0 && endIndex <= PirateShip.Count - 1)
                    {
                        for (int i = startIndex; i <= endIndex; i++)
                        {
                            PirateShip[i] = PirateShip[i] - damage;
                            if (PirateShip[i] <= 0)
                            {
                                Console.WriteLine("You lost! The pirate ship has sunken.");
                                return;
                            }
                        }
                    }
                }
                else if (commandArray[0] == "Repair")
                {
                    int index = int.Parse(commandArray[1]);
                    int health = int.Parse(commandArray[2]);
                    if (index >= 0 && index <= PirateShip.Count - 1)
                    {
                        PirateShip[index] = PirateShip[index] + health;
                        if (PirateShip[index] > maxHealthCapacity)
                        {
                            PirateShip[index] = maxHealthCapacity;
                        }
                    }
                }
                else if (commandArray[0] == "Status")
                {
                    var filteredList = PirateShip.Where(x => (double) x / maxHealthCapacity * 100 < 20);
                    Console.WriteLine($"{filteredList.Count()} sections need repair.");
                }
            }
            Console.WriteLine($"Pirate ship status: {PirateShip.Sum()}");
            Console.WriteLine($"Warship status: {WarShip.Sum()}");
        }
    }
}
 
Thanks for your feedback!
I haven't done this, because according the requirement I have to print the status of both ships if a stalemate occurs, which is the sum. If same ship win I have to print the winer and stop the program.
Tomorrow I would be trying your solution. Thanks again.