Beispiel #1
0
        private int parse(List<String> data)
        {
            planets = new List<Planet>();
            fleets = new List<Fleet>();

            CultureInfo en = CultureInfo.GetCultureInfo("en-US"); // Doubles are written with a . separator

            foreach (String str in data) {
                if (str.Trim().Length != 0) {
                    String[] line = str.Split(' ');
                    if (line.Length != 0) {
                        if (line[0].Equals("M")) {
                            if (line.Length != 5) {
                                System.Console.Error.WriteLine("error line 0: " + str);
                                return 1;
                            }
                            double x = Double.Parse(line[1], en);
                            double y = Double.Parse(line[2], en);
                            int owner = int.Parse(line[3]);
                            int numShips = int.Parse(line[4]);

                            MilitaryPlanet p = new MilitaryPlanet(planets.Count(),
                                    owner, numShips, x, y);
                            planets.Add(p);
                        } else if (line[0].Equals("E")) {
                            if (line.Length != 6) {
                                System.Console.Error.WriteLine("error line 1: " + str);
                                return 1;
                            }
                            double x = Double.Parse(line[1], en);
                            double y = Double.Parse(line[2], en);
                            int owner = int.Parse(line[3]);
                            int numShips = int.Parse(line[4]);
                            int economicValue = int.Parse(line[5]);

                            EconomicPlanet p = new EconomicPlanet(planets.Count(),
                                    owner, numShips, economicValue, x, y);
                            planets.Add(p);
                        } else if (line[0].Equals("F") || line[0].Equals("R")) {
                            if (line.Length != 7) {
                                System.Console.Error.WriteLine("error line 2: " + str);
                                return 1;
                            }
                            int owner = int.Parse(line[1]);
                            int numShips = int.Parse(line[2]);
                            int sourceDept = int.Parse(line[3]);
                            int destDept = int.Parse(line[4]);
                            int tripLength = int.Parse(line[5]);
                            int turnsRemaining = int.Parse(line[6]);
                            Fleet fleet = new Fleet(owner, numShips, sourceDept,
                                    destDept, tripLength, turnsRemaining, line[0].Equals("F"));
                            this.fleets.Add(fleet);
                        } else if (line[0].Equals("turn")) {
                            // New turn
                        }
                    }
                }
            }
            return 0;
        }
Beispiel #2
0
        private int parse(List <String> data)
        {
            planets = new List <Planet>();
            fleets  = new List <Fleet>();

            CultureInfo en = CultureInfo.GetCultureInfo("en-US"); // Doubles are written with a . separator

            foreach (String str in data)
            {
                if (str.Trim().Length != 0)
                {
                    String[] line = str.Split(' ');
                    if (line.Length != 0)
                    {
                        if (line[0].Equals("M"))
                        {
                            if (line.Length != 5)
                            {
                                System.Console.Error.WriteLine("error line 0: " + str);
                                return(1);
                            }
                            double x        = Double.Parse(line[1], en);
                            double y        = Double.Parse(line[2], en);
                            int    owner    = int.Parse(line[3]);
                            int    numShips = int.Parse(line[4]);

                            MilitaryPlanet p = new MilitaryPlanet(planets.Count(),
                                                                  owner, numShips, x, y);
                            planets.Add(p);
                        }
                        else if (line[0].Equals("E"))
                        {
                            if (line.Length != 6)
                            {
                                System.Console.Error.WriteLine("error line 1: " + str);
                                return(1);
                            }
                            double x             = Double.Parse(line[1], en);
                            double y             = Double.Parse(line[2], en);
                            int    owner         = int.Parse(line[3]);
                            int    numShips      = int.Parse(line[4]);
                            int    economicValue = int.Parse(line[5]);

                            EconomicPlanet p = new EconomicPlanet(planets.Count(),
                                                                  owner, numShips, economicValue, x, y);
                            planets.Add(p);
                        }
                        else if (line[0].Equals("F") || line[0].Equals("R"))
                        {
                            if (line.Length != 7)
                            {
                                System.Console.Error.WriteLine("error line 2: " + str);
                                return(1);
                            }
                            int   owner          = int.Parse(line[1]);
                            int   numShips       = int.Parse(line[2]);
                            int   sourceDept     = int.Parse(line[3]);
                            int   destDept       = int.Parse(line[4]);
                            int   tripLength     = int.Parse(line[5]);
                            int   turnsRemaining = int.Parse(line[6]);
                            Fleet fleet          = new Fleet(owner, numShips, sourceDept,
                                                             destDept, tripLength, turnsRemaining, line[0].Equals("F"));
                            this.fleets.Add(fleet);
                        }
                        else if (line[0].Equals("turn"))
                        {
                            // New turn
                        }
                    }
                }
            }
            return(0);
        }