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;
}
}
}