예제 #1
0
 /// <summary>
 /// drive to semaphore or gas station or end of race
 /// </summary>
 /// <param name="secs">How long to drive</param>
 /// <param name="auto">Who is driving</param>
 public void Drive(int secs, Automobile auto)
 {
     for (int i = 0; i < secs; i++)
     {
         Thread.Sleep(1000);
         if (auto.MaxTankVolume <= 0)
         {
             break;
         }
     }
 }
예제 #2
0
 /// <summary>
 /// Method that simulates charginig ar gas station
 /// </summary>
 /// <param name="automobile">Car</param>
 public void ChargingAtGasStation(Automobile automobile)
 {
     //one can charge at time
     gasStation.Wait();
     Console.WriteLine("{0} {1} is charging, current tank volume: {2}l", automobile.Color, automobile.Producer, automobile.MaxTankVolume);
     //charge
     Thread.Sleep(50);
     //charge to max tank value
     automobile.MaxTankVolume = 51;
     Console.WriteLine("{0} {1} finished charging their tank", automobile.Color, automobile.Producer);
     //release gas station so another car can charge
     gasStation.Release();
 }
예제 #3
0
        /// <summary>
        /// create new car
        /// </summary>
        public override void CreateNewVehicle()
        {
            Automobile auto = new Automobile
            {
                MotorVolume        = Math.Round(rng.NextDouble() * 1000, 2),
                Weight             = rng.Next(1000, 2001),
                Category           = allCategory[rng.Next(0, allCategory.Length)],
                MotorType          = allMotorType[rng.Next(0, allMotorType.Length)],
                DoorNumber         = rng.Next(3, 6),
                MaxTankVolume      = 51,
                Color              = allColors[rng.Next(0, allColors.Length)],
                MotorNumber        = rng.Next(50, 101),
                RegistrationNumber = rng.Next(1000, 10000).ToString(),
                TransportType      = allTransportType[rng.Next(0, allTransportType.Length)],
                Producer           = allProducer[rng.Next(0, allProducer.Length)],
                TrafficNumber      = rng.Next(100, 1000)
            };

            //add new car to list
            Program.allAutomobiles.Add(auto);
        }
예제 #4
0
        /// <summary>
        /// Race result based on car's tank volume
        /// </summary>
        /// <param name="automobile"></param>
        public void EndRaceResult(Automobile automobile)
        {
            lock (l)
            {
                carCounter++;
            }

            //increase number of cars that reached the end of race
            if (automobile.MaxTankVolume > 0)
            {
                winners++;
            }

            //print place that car took
            if (winners > 0 && automobile.MaxTankVolume > 0)
            {
                Console.WriteLine("\n{0}. {1} {2}", winners, automobile.Color, automobile.Producer);
            }

            //print red car result
            if (automobile.Color == "Red" && automobile.MaxTankVolume > 0 && !fastestRed)
            {
                fastestRed = true;
                PrintFirstRed(automobile);
            }
            else if (carCounter == 3 && Program.isRedCar == false)
            {
                Console.WriteLine("\n\nThere were no red cars in the race.\n");
            }
            else if (carCounter == 3 && Program.isRedCar == true && fastestRed == false)
            {
                Console.WriteLine("\n\nNo red cars get to the end of race.\n");
            }

            winRace.Set();
        }
예제 #5
0
        static void Main(string[] args)
        {
            Automobile auto    = new Automobile();
            Tractor    tractor = new Tractor();
            Truck      truck   = new Truck();

            Race race = new Race();


            // Creating vehicles
            for (int i = 0; i < 2; i++)
            {
                auto.CreateNewVehicle();
                truck.CreateNewVehicle();
                tractor.CreateNewVehicle();
            }

            //stopwatch 5 seconds
            for (int i = 1; i < 6; i++)
            {
                Console.WriteLine(i);
                Thread.Sleep(1000);
            }

            // Starting cars
            for (int i = 0; i < allAutomobiles.Count; i++)
            {
                int    temp    = i;
                Thread thread1 = new Thread(() => race.Racing(allAutomobiles[temp]));
                thread1.Start();
            }

            // Creating orange golf car
            Random     rng  = new Random();
            Automobile golf = new Automobile
            {
                Color         = "Orange",
                MaxTankVolume = 51,
                Producer      = "Golf"
            };

            allAutomobiles.Add(golf);

            //check if there is red car in cars list
            for (int i = 0; i < allAutomobiles.Count; i++)
            {
                if (allAutomobiles[i].Color == "Red")
                {
                    isRedCar = true;
                    break;
                }
            }

            //Starting orange golf
            Console.WriteLine(golf.Color + " " + golf.Producer + " joined the race\n\n\n");
            Thread threadGolf = new Thread(() => race.Racing(golf));

            threadGolf.Start();

            //Thread that changes the semaphore color for 2 seconds
            Thread thread3 = new Thread(race.ChangeSemaphoreColor)
            {
                IsBackground = true
            };

            thread3.Start();

            // Thread that reduces the car Tank Volume every 1 second
            Thread thread4 = new Thread(race.DecreaseTankVolume)
            {
                IsBackground = true
            };

            thread4.Start();

            Console.ReadKey();
        }
예제 #6
0
        /// <summary>
        /// Car racing
        /// </summary>
        /// <param name="automobile">Car on race</param>
        public void Racing(Automobile automobile)
        {
            //all (3) cars pass
            cnt.Signal();
            cnt.Wait();

            //go car
            automobile.Move();

            //while car have tank volume
            while (automobile.MaxTankVolume > 0)
            {
                //driving to semaphore
                Drive(10, automobile);

                Console.WriteLine("{0} {1} reached the semaphore, currently light is {2}", automobile.Color, automobile.Producer, semaphoreColor);

                //wait until currently light is green
                while (semaphoreColor == "Red")
                {
                    Thread.Sleep(0);
                }

                lock (l)
                {
                    carCounter++;
                }

                //drive to gas stayion
                Drive(3, automobile);

                //if tank values is less than 15 than go to gas station
                if (automobile.MaxTankVolume < 15)
                {
                    ChargingAtGasStation(automobile);
                }
                else
                {
                    Console.WriteLine("{0} {1} passed the gas station with tank valume: {2} l.", automobile.Color, automobile.Producer, automobile.MaxTankVolume);
                }

                //drive 7 seconds to end of race
                Drive(7, automobile);
                break;
            }

            // Checks if the car is out of gas
            if (automobile.MaxTankVolume <= 0)
            {
                //stop the car
                automobile.Stop();
                Console.WriteLine("{0} {1} is out of gas and left the race.", automobile.Color, automobile.Producer);
            }
            else if (automobile.MaxTankVolume > 0)
            {
                Console.WriteLine("{0} {1} car arrived to the end of race", automobile.Color, automobile.Producer);
                automobile.Stop();
            }

            winRace.WaitOne();
            //check race result
            EndRaceResult(automobile);
        }
예제 #7
0
 /// <summary>
 /// print red car result
 /// </summary>
 /// <param name="automobile">Red car</param>
 public void PrintFirstRed(Automobile automobile)
 {
     winRace.Set();
     Console.WriteLine("\n\nFastest red car is: {0} {1}.\n", automobile.Color, automobile.Producer);
 }