Exemple #1
0
        public void AnnounceWinners()
        {
            var position = 0;

            Console.WriteLine();
            Console.WriteLine("The winners are ...");
            foreach (var driver in Drivers.OrderByDescending(d => d.Car.Position))
            {
                if (driver.Car.Position == 0)
                {
                    Console.WriteLine($"Oh no! {driver.Name} suffered a breakdown in their {driver.Car.Name} and didn't finish the race!");
                }
                else
                {
                    Console.WriteLine($"{++position}: {driver.Name} in their {driver.Car.Name}");
                }
            }
            Console.WriteLine("Press any key to exit");
            Console.ReadLine();
        }
    private string CalculateOvertaking()
    {
        StringBuilder overtakingSb  = new StringBuilder();
        List <Driver> sortedDrivers = Drivers.OrderByDescending(x => x.TotalTime).ToList();

        for (int i = 0; i < Drivers.Count - 1; i++)
        {
            Driver firstDriver = sortedDrivers[0];

            if (!string.IsNullOrWhiteSpace(firstDriver.FailureReason))
            {
                continue;
            }

            Driver secondDriver = sortedDrivers[1];

            if (firstDriver.GetType() == typeof(AggressiveDriver) && firstDriver.Car.Tyre.GetType() == typeof(UltrasoftTyre))
            {
                if (this.Weather == "Foggy")
                {
                    firstDriver.FailureReason = "Crashed";
                    FailedDrivers.Add(firstDriver);
                    continue;
                }
            }

            if (firstDriver.GetType() == typeof(EnduranceDriver) && firstDriver.Car.Tyre.GetType() == typeof(HardTyre))
            {
                if (this.Weather == "Rainy")
                {
                    firstDriver.FailureReason = "Crashed";
                    FailedDrivers.Add(firstDriver);
                    continue;
                }
            }

            if (Math.Abs(firstDriver.TotalTime - secondDriver.TotalTime) <= 3)
            {
                if (firstDriver.GetType() == typeof(AggressiveDriver) && firstDriver.Car.Tyre.GetType() == typeof(UltrasoftTyre))
                {
                    firstDriver.TotalTime  -= 3;
                    secondDriver.TotalTime += 3;
                    overtakingSb.AppendFormat($"{firstDriver.Name} has overtaken {secondDriver.Name} on lap {this.CurrentLap}");
                    continue;
                }

                if (firstDriver.GetType() == typeof(EnduranceDriver) && firstDriver.Car.Tyre.GetType() == typeof(HardTyre))
                {
                    firstDriver.TotalTime  -= 3;
                    secondDriver.TotalTime += 3;
                    overtakingSb.AppendFormat($"{firstDriver.Name} has overtaken {secondDriver.Name} on lap {this.CurrentLap}");
                    continue;
                }
            }

            if (Math.Abs(firstDriver.TotalTime - secondDriver.TotalTime) <= 2)
            {
                firstDriver.TotalTime  -= 2;
                secondDriver.TotalTime += 2;
                overtakingSb.AppendFormat($"{firstDriver.Name} has overtaken {secondDriver.Name} on lap {this.CurrentLap}");
            }
        }

        Drivers.RemoveAll(x => !string.IsNullOrWhiteSpace(x.FailureReason));
        return(overtakingSb.ToString());
    }