Exemplo n.º 1
0
        public static Grid NextGeneration(Grid currentGrid)
        {
            var nextGrid = new Grid();

            currentGrid.IterateLiveCells((coords, cellState) =>
            {
                if (CurrentlyAliveCellWillStillBeALiveInTheNextGeneration(currentGrid, coords))
                {
                    nextGrid.MarkLiveCellAt(coords);
                }
                else
                {
                    nextGrid.MarkWasPreviouslyAliveCellAt(coords);
                }

                foreach (var neighbour in coords.Neighbours())
                {
                    if (!currentGrid.IsLiveCellAt(neighbour))
                    {
                        if (CurrentlyDeadCellWillBecomeALiveInTheNextGeneration(currentGrid, neighbour))
                        {
                            if (currentGrid.CellWasPreviouslyAliveAt(neighbour))
                            {
                                nextGrid.MarkZombieCellAt(neighbour);
                            }
                            else
                            {
                                nextGrid.MarkLiveCellAt(neighbour);
                            }
                        }
                    }
                }
            });

            return nextGrid;
        }