Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

geosot avatar geosot 0 Точки

Programming Fundamentals Mid Exam Retake - 6 August 2019 - 03. Man o War

Някой може ли да ми даде насоки. Стигнах до 80/100

using System;
using System.Linq;

namespace _03._Man_o_war
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] pirateShip = Console.ReadLine().Split('>').Select(double.Parse).ToArray();

            double[] warShip = Console.ReadLine().Split('>').Select(double.Parse).ToArray();

            double health = double.Parse(Console.ReadLine());

            string input = string.Empty;

            int sections = 0;

            bool check = true;

            int pirateshipStats = 0;

            int warshipStats = 0;

            while ((input = Console.ReadLine()) != "Retire")
            {
                string[] splited = input.Split(' ');

                string command = splited[0];

                int index = 0;

                int damage = 0;

                int startIndex = 0;

                int endIndex = 0;

                int repairedHealth = 0;

                switch (command)
                {
                    case "Fire":
                         index = int.Parse(splited[1]);

                         damage = int.Parse(splited[2]);

                        if (ValidIndex(index, warShip))
                        {
                            warShip[index] -= damage;

                            if (warShip[index] <= 0)
                            {
                                Console.WriteLine("You won! The enemy ship has sunken.");
                                check = false;
                                break;
                            }
                        }
                        else
                        {
                            continue;
                        }
                        break;
                    case "Defend":
                         startIndex = int.Parse(splited[1]);

                         endIndex = int.Parse(splited[2]);

                         damage = int.Parse(splited[3]);

                        if (ValidIndex(startIndex, pirateShip) && ValidIndex(endIndex, pirateShip))
                        {
                            for (int i = startIndex; i <= endIndex; i++)
                            {
                                pirateShip[i] -= damage;

                                if (pirateShip[i] <= 0)
                                {
                                    Console.WriteLine("You lost! The pirate ship has sunken.");
                                    check = false;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            continue;
                        }
                        break;
                    case "Repair":
                        index = int.Parse(splited[1]);

                        repairedHealth = int.Parse(splited[2]);

                        if (ValidIndex(index, pirateShip))
                        {
                            if (pirateShip[index] + repairedHealth <= health)
                            {
                                pirateShip[index] += repairedHealth;
                            }
                            else
                            {
                                pirateShip[index] = health;
                            }
                        }
                        else
                        {
                            continue;
                        }
                        break;
                    case "Status":
                        foreach (int section in pirateShip)
                        {
                            if (section < health * 0.2)
                            {
                                sections++;
                            }
                        }

                        Console.WriteLine($"{sections} sections need repair.");
                        break;
                }
            }

            if (check)
            {
                foreach (int section in pirateShip)
                {
                    pirateshipStats += section;
                }

                Console.WriteLine($"Pirate ship status: {pirateshipStats}");

                foreach (int section in warShip)
                {
                    warshipStats += section;
                }

                Console.WriteLine($"Warship status: {warshipStats}");
            }

        }
        private static bool ValidIndex(int index, double[] list)
        {
            return index < list.Length && index >= 0;
        }
    }
}

 

Тагове:
0
Module: C# Advanced
krum_43 avatar krum_43 750 Точки

Дай линк към условието !

                   

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.