Exemplo n.º 1
0
        static void Main(string[] args)
        {// initial state
            int[][] gridRepresentationArray =
                new int[][] { new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                              new int[] { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
                              new int[] { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
                              new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                              new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                              new int[] { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
                              new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
                              new int[] { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
                              new int[] { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
                              new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };

            // Instantiate arrays
            var size       = new Point(25, 60);
            var cellsLines = new List <List <int> >(size.X);

            //cellsBuffer = new Listt<bool>(360);

            var random = new Random();

            // Initialization of cells
            for (int x = 0; x < size.X; x++)
            {
                cellsLines.Add(new List <int>(size.Y));
                for (int y = 0; y < size.Y; y++)
                {
                    int state = random.Next(100);
                    if (state > 15)
                    {
                        state = 0;
                    }
                    else
                    {
                        state = 1;
                    }
                    cellsLines[x].Add(state); // Save state of each cell
                }
            }

            Grid grid = new Grid(cellsLines);            //new Grid(gridRepresentationArray;

            int i = 1;

            Console.WriteLine("State before evolution");
            grid.Display();

            while (grid.AnyALive())
            {
                Console.WriteLine("State after {0} evolution", i);
                grid.Display();
                grid.Evolve();
                i++;

                Task.Delay(1000).Wait();
            }
            Console.Clear();
            grid.Display();
        }
Exemplo n.º 2
0
        public void TestGrid()
        {
            //Grid Test A: Good data - simple, should always succeed
            Console.WriteLine("Grid Test A");
            try
            {
                Grid testingGridA = new Grid(5, 5);
                testingGridA.PopulateRandom();
                testingGridA.Display();
                testingGridA.PopulateInterim();
                testingGridA.NextGeneration();
                Console.WriteLine();
                testingGridA.Display();
                Console.WriteLine("Grid Test A Successful");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Grid Test A Failed");
            }

            //Grid Test B: Good data - Zero dimensions - shouldn't throw and error but wouldn't display anything
            Console.WriteLine("Grid Test B");
            try
            {
                Grid testingGridB = new Grid(0, 0);
                testingGridB.PopulateRandom();
                testingGridB.Display();
                testingGridB.PopulateInterim();
                testingGridB.NextGeneration();
                testingGridB.Display();
                Console.WriteLine("Grid Test B Successful");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Grid Test B Failed");
            }

            //Grid Test C: Bad data - Negative dimensions - should fail
            Console.WriteLine("Grid Test C");
            try
            {
                Grid testingGridC = new Grid(-2, -4);
                testingGridC.PopulateRandom();
                testingGridC.Display();
                testingGridC.PopulateInterim();
                testingGridC.NextGeneration();
                testingGridC.Display();
                Console.WriteLine("Grid Test C Successful");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Grid Test C Failed");
            }

            //Grid Test D: Good data - large dimensions - should succeed albeit probably will be rather slow
            Console.WriteLine("Grid Test D");
            try
            {
                Grid testingGridD = new Grid(250, 250);
                testingGridD.PopulateRandom();
                testingGridD.Display();
                testingGridD.PopulateInterim();
                testingGridD.NextGeneration();
                testingGridD.Display();
                Console.WriteLine("Grid Test D Successful");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Grid Test D Failed");
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            //Dimensions
            int gridRows    = 0;
            int gridColumns = 0;
            //Boolean used for input while loops
            bool badInput = true;

            //User inputs amount of rows for the grid, while loop + try catch ensure correct input
            while (badInput == true)
            {
                try
                {
                    Console.WriteLine("How many rows would you like the grid to have?");
                    gridRows = Convert.ToInt32(Console.ReadLine());
                    if (gridRows > 0)
                    {
                        badInput = false;
                    }
                    else
                    {
                        Console.WriteLine("Your input was 0 or less. It must be positive.");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine("Input a positive integer.");
                }
            }
            //User inputs amount of columns for the grid, while loop + try catch ensure correct input
            badInput = true;
            while (badInput == true)
            {
                try
                {
                    Console.WriteLine("How many columns would you like the grid to have?");
                    gridColumns = Convert.ToInt32(Console.ReadLine());
                    if (gridColumns > 0)
                    {
                        badInput = false;
                    }
                    else
                    {
                        Console.WriteLine("Your input was 0 or less. It must be positive.");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine("Input a positive integer.");
                }
            }
            //Initialize grid, populate with first generation, and display grid
            Grid mainGrid = new Grid(gridRows, gridColumns);

            mainGrid.PopulateRandom();
            Console.WriteLine("Here is Generation 1:");
            Console.WriteLine();
            mainGrid.Display();
            Console.WriteLine();
            //Get desired amount of simulations, using while loop + try/catch to ensure correct input
            badInput = true;
            Console.WriteLine("Press enter to begin the simulation. Once the simulation begins, press esc to stop.");
            Console.ReadLine();
            Console.WriteLine("Simulating next generation...");
            //While loop to run the simulation until the user presses esc
            while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape))
            {
                mainGrid.PopulateInterim();
                mainGrid.NextGeneration();
                Console.Clear();
                mainGrid.Display();
                Console.WriteLine("");
                Console.WriteLine("Simulating next generation...");
                Thread.Sleep(750);
            }
            Console.WriteLine();
            Console.WriteLine("The simulation has ended. Press enter to exit.");
            Console.ReadLine();
            //Test test = new Test();
        }