Пример #1
0
        private static int numberOfLiveNeighbours(Grid grid, Coordinate coordinate)
        {
            var liveCellCount = 0;

            foreach (var coord in neighbouringCoordinates(coordinate))
            {
                grid.GetCellState(coord, () => liveCellCount++, () => { });
            }
            return(liveCellCount);
        }
Пример #2
0
        public Grid NextEvolution(Grid grid)
        {
            var array = new IAmACell[_width * _height];

            for (var y = 0; y < _height; y++)
            {
                for (var x = 0; x < _width; x++)
                {
                    var coordinate = new Coordinate(x, y);
                    grid.GetCellState(coordinate,
                                      () => array[coordinate.X + _width * coordinate.Y] = whenCurrentCellisAlive(coordinate, grid),
                                      () => array[coordinate.X + _width * coordinate.Y] = whenCurrentCellIsDead(coordinate, grid));
                }
            }
            return(new Grid(array));
        }
Пример #3
0
 public void PrintGrid(Grid grid)
 {
     Console.WriteLine($"Iteration {_counter++}\nActive cells {grid.CountActiveCells()}");
     for (int i = 0; i < grid.Size; i++)
     {
         for (int j = 0; j < grid.Size; j++)
         {
             if (grid.GetCellState(i, j) == false)
             {
                 Console.Write("  .  ");
             }
             else
             {
                 Console.Write(" [X] ");
             }
         }
         Console.WriteLine();
     }
 }
Пример #4
0
        private static void Main()
        {
            var grid = new Grid(randomGridCells());
            for (var i = 0; i < NumberOfIterations; i++)
            {
                Console.Clear();
                for (var y = 0; y < GridHeight; y++)
                {
                    Console.WriteLine();
                    for (var x = 0; x < GridWidth; x++)
                    {
                        grid.GetCellState(new Coordinate(x, y), () => Console.Write("X"), () => Console.Write(" "));
                    }
                }
                grid = GridIterator.NextEvolution(grid);
                Thread.Sleep(25);
            }

            Console.WriteLine("\r\n\r\n\r\nPress any key to continue");
            Console.ReadKey();
        }