Exemplo n.º 1
0
        /// <summary>
        /// Validate input string and format
        /// </summary>
        /// <param name="userInput"></param>
        /// <returns></returns>
        private static ConfigStruct ValidateInput(string userInput)
        {
            ConfigStruct config = new ConfigStruct();

            string[] configinstringformat = userInput.Split('#');
            string[] gridconfiguration;

            Exception exception = new Exception(INVALIDINPUTMESSAGE);

            try
            {
                config.pattern = configinstringformat[0];

                if (configinstringformat.Length != 4) throw exception;

                gridconfiguration = configinstringformat[1].Split(',');

                bool result = Int32.TryParse(configinstringformat[2], out config.maxgenerations);

                // Validate null and int type
                if (gridconfiguration.Length != 2 || int.Parse(gridconfiguration[0]) <= 0 || int.Parse(gridconfiguration[1]) <= 0 || (!result) || (string.IsNullOrWhiteSpace(userInput)))
                {
                    throw exception;
                }
                else
                {
                    config.gridrowcount = int.Parse(gridconfiguration[0]);
                    config.gridcolcount = int.Parse(gridconfiguration[1]);
                }

                // Validate coordinate string
                string[] coordinstring = configinstringformat[3].Split('|');
                for (int i = 0; i < coordinstring.Length; i++)
                {
                    string[] coordinatepair = coordinstring[i].Split(',');

                    int rownum;
                    int colnum;
                    if ((!Int32.TryParse(coordinatepair[0], out rownum)) || (!Int32.TryParse(coordinatepair[1], out colnum)))
                    {
                        throw exception;
                    }
                }
                config.CoOrds = coordinstring;
            }
            catch
            {
                // Can do cleanup work here.
                Console.WriteLine(exception.Message);
            }
            finally
            {
                config.pattern = null;
            }

            return config;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Process Simulation
        /// </summary>
        /// <param name="configparams"></param>
        private static void ProcessSimultion(ConfigStruct configparams)
        {
            Game objLifeGame = new Game(configparams.gridrowcount , configparams.gridcolcount);

            objLifeGame.MaxGenerations = configparams.maxgenerations;

            Console.BufferHeight = (Console.BufferHeight > objLifeGame.MaxGenerations * 20) ? Console.BufferHeight : objLifeGame.MaxGenerations * 20;

            Console.Clear();

            Console.WriteLine("Simulating Pattern: " + configparams.pattern + "\n");

            for (int i = 0; i < configparams.CoOrds.Length; i++)
            {
                string[] coordinstring = configparams.CoOrds[i].Split(',');
                objLifeGame.ToggleGridCell(int.Parse(coordinstring[0]), int.Parse(coordinstring[1]));
            }

            objLifeGame.Init();

            Console.WriteLine(RETURNMAINMESSAGE);
            Console.WriteLine(EXITMESSAGE);

            char key = Console.ReadKey(false).KeyChar;
            if (key.ToString().ToUpper() == "B") { Console.Clear(); PrintWelcomeAndInstructions(); return; }
        }