Пример #1
0
        private List <Boat> CreateRandomBoats(int boats)
        {
            List <Boat> incoming = new List <Boat>(); // List to return.

            for (int i = 0; i < boats; i++)
            {
                // 0-RowBoat, 1-MotorBoat, 2-SailsBoat, 3-CargoBoat, 4-Katamaran
                TypeBoat t = (TypeBoat)Enum.GetValues(typeof(TypeBoat)) // This returns an array of the values in TypeBoat
                             .GetValue((new Random()).Next(0, 5));      // GetValue(i) retrieves by an index (much like [i]).
                                                                        // In this case "Random".

                switch (t)
                {
                case TypeBoat.Rowboat:
                {
                    RowBoat r = new RowBoat(true);
                    r.Type = TypeBoat.Rowboat;
                    incoming.Add(r);
                    break;
                }

                case TypeBoat.Motorboat:
                {
                    MotorBoat m = new MotorBoat(true);
                    m.Type = TypeBoat.Motorboat;
                    incoming.Add(m);
                    break;
                }

                case TypeBoat.Sailsboat:
                {
                    SailsBoat s = new SailsBoat(true);
                    s.Type = TypeBoat.Sailsboat;
                    incoming.Add(s);
                    break;
                }

                case TypeBoat.Cargoboat:
                {
                    CargoBoat c = new CargoBoat(true);
                    c.Type = TypeBoat.Cargoboat;
                    incoming.Add(c);
                    break;
                }

                case TypeBoat.Katamaran:
                {
                    Katamaran k = new Katamaran(true);
                    k.Type = TypeBoat.Katamaran;
                    incoming.Add(k);
                    break;
                }
                }
            }

            return(incoming);
        }
Пример #2
0
        /*
         * I decided to use regular expressions here and to make things a little bit easier,
         * I chose a LABEL/TAG format to save the original file.
         * That way I will only look for tags or labels and take their content.
         */
        private bool LoadStatusFromFile(string fileName)
        {
            bool fileLocated = false;

            if (File.Exists(fileName))
            {
                fileLocated = true;

                string file = File.ReadAllText(fileName);

                // We recover the day.
                Match m = Regex.Match(file, @"<day>(.*)</day>");
                Day = int.Parse(m.Groups[1].Value);

                // Now let's fill the dock's codes. North and South.
                m = Regex.Match(file, @"<docksNorth>(.*)</docksNorth>");
                port.docksNorth = m.Groups[1].Value.ToCharArray(); // and make them into char[]
                m = Regex.Match(file, @"<docksSouth>(.*)</docksSouth>");
                port.docksSouth = m.Groups[1].Value.ToCharArray();

                // Let's now recover every boat in the port. Multiple matches!
                MatchCollection mBoats = Regex.Matches(file, @"<boat>(.+?)</boat>", RegexOptions.Singleline);

                for (int i = 0; i < mBoats.Count; i++)
                {
                    string boat = mBoats[i].Groups[1].Value;

                    string  id                 = Regex.Match(boat, @"<ID>(.*)</ID>").Groups[1].Value;
                    string  parkingPort        = Regex.Match(boat, @"<ParkingPort>(.*)</ParkingPort>").Groups[1].Value;
                    int     parkingPlace       = int.Parse(Regex.Match(boat, @"<ParkingPlace>(.*)</ParkingPlace>").Groups[1].Value);
                    string  boatSpacesRequired = Regex.Match(boat, @"<BoatSpacesRequired>(.*)</BoatSpacesRequired>").Groups[1].Value;
                    int     maxSpeedNots       = int.Parse(Regex.Match(boat, @"<MaxSpeedNots>(.*)</MaxSpeedNots>").Groups[1].Value);
                    decimal weight             = decimal.Parse(Regex.Match(boat, @"<Weight>(.*)</Weight>").Groups[1].Value);
                    int     daysRemaining      = int.Parse(Regex.Match(boat, @"<DaysRemaining>(.*)</DaysRemaining>").Groups[1].Value);
                    string  type               = Regex.Match(boat, @"<Type>(.*)</Type>").Groups[1].Value;
                    int     special            = int.Parse(Regex.Match(boat, @"</Type>\W*<.*>(.*)</.*>").Groups[1].Value);

                    Boat b = new RowBoat();
                    switch (type)
                    {
                    case "Rowboat":
                    {
                        b.Type = TypeBoat.Rowboat;
                        ((RowBoat)b).Passengers = special;
                        break;
                    }

                    case "Motorboat":
                    {
                        b      = new MotorBoat();
                        b.Type = TypeBoat.Motorboat;
                        ((MotorBoat)b).HorsePower = special;
                        break;
                    }

                    case "Sailsboat":
                    {
                        b      = new SailsBoat();
                        b.Type = TypeBoat.Sailsboat;
                        ((SailsBoat)b).BoatLenght = special;
                        break;
                    }

                    case "Cargoboat":
                    {
                        b      = new CargoBoat();
                        b.Type = TypeBoat.Cargoboat;
                        ((CargoBoat)b).NumberContainers = special;
                        break;
                    }

                    case "Katamaran":
                    {
                        b      = new Katamaran();
                        b.Type = TypeBoat.Katamaran;
                        ((Katamaran)b).AmountBeds = special;
                        break;
                    }
                    }

                    switch (parkingPort)
                    {
                    case "North":
                    {
                        b.ParkingPort = PortSide.North;
                        break;
                    }

                    case "South":
                    {
                        b.ParkingPort = PortSide.South;
                        break;
                    }
                    }

                    b.ID                 = id;
                    b.ParkingPlace       = parkingPlace - 1;
                    b.BoatSpacesRequired = boatSpacesRequired;
                    b.MaxSpeedNots       = maxSpeedNots;
                    b.Weight             = weight;
                    b.DaysRemaining      = daysRemaining;

                    port.boats.Add(b);
                }

                MatchCollection mc = Regex.Matches(file, @"<rejected>(.*)</rejected>");
                for (int i = 0; i < mc.Count; i++)
                {
                    rejectedBoats.Add(mc[i].Groups[1].Value);
                }

                Print();
            }

            return(fileLocated);
        }