示例#1
0
        /// <summary >
        /// Sets the Survival and birth values, These have identical validation and formatting so I combined them into this method
        /// You can set individual parameters or a range of values using an elipses.
        /// </ summary >
        /// <param name =" args " >String arguments that initialise the Game of Life settings</ param >
        /// <param name =" pos " >The position of the --survival or --birth option within the args array</ param >
        /// <param name =" SB " >survival or birth list address</ param >
        private void readSB(int pos, string[] args, List <int> SB)
        {
            bool invalid = false;

            try
            {
                for (int a = pos + 1; !args[a].Contains("--"); a++)     // for all parameters
                {
                    if (a == pos + 1)
                    {
                        SB.Clear();               //initalise setting new values
                    }
                    if (args[a].Contains("..."))  // set range
                    {
                        string[] range = args[a].Split("...");

                        if (range.Length == 2 && int.Parse(range[0]) < int.Parse(range[1]))
                        {     //valid format?
                            for (int i = int.Parse(range[0]); i <= int.Parse(range[1]); i++)
                            { // set parameters within range
                                if (validate.SB(i, neighbourhoodSize()) && !SB.Contains(i))
                                {
                                    SB.Add(i);
                                }
                                else
                                {
                                    invalid = true;
                                }
                            }
                        }
                        else
                        {
                            validate.invalid(args[a] + " Invalid format");
                        }
                    }
                    else // set birth or survival parameter
                    {
                        if (validate.SB(int.Parse(args[a]), neighbourhoodSize()) && !SB.Contains(int.Parse(args[a])))
                        {
                            SB.Add(int.Parse(args[a]));
                        }
                        else
                        {
                            invalid = true;
                        }
                    }
                }
            }
            catch (Exception)
            {
                //reached the end of arguments
            }
            finally {
                if (invalid)
                {
                    validate.invalid("Invalid [--survival or --birth] value, must be 0 < [value] < " + neighbourhoodSize());
                }
                SB.Sort(); //sort
            }
            return;
        }