Ejemplo n.º 1
0
        public void Cycle()
        {
            var activeCubesWithNeighbours = ActiveCubes
                                            .Select(c => new { Cube = c, Neighbours = GetNeighbours(c).ToArray() })
                                            .ToArray();

            var inactiveNeighbours = activeCubesWithNeighbours
                                     .Select(c => c.Neighbours)
                                     .SelectMany(n => n)
                                     .Distinct(new Coordinate3DComparer())
                                     .Where(c => !activeCubesWithNeighbours.Any(ac => ac.Cube.Z == c.Z &&
                                                                                ac.Cube.Y == c.Y &&
                                                                                ac.Cube.X == c.X))
                                     .Select(c => new {
                Cube = c,
                ActiveNeighboursCount = GetNeighbours(c)
                                        .Where(n => activeCubesWithNeighbours.Any(ac => ac.Cube.Z == n.Z &&
                                                                                  ac.Cube.Y == n.Y &&
                                                                                  ac.Cube.X == n.X))
                                        .Count()
            })
                                     .ToArray();

            var activeCubes = activeCubesWithNeighbours
                              .Select(c => new
            {
                Cube = c.Cube,
                ActiveNeighboursCount = c.Neighbours
                                        .Where(n => activeCubesWithNeighbours.Any(ac => ac.Cube.Z == n.Z &&
                                                                                  ac.Cube.Y == n.Y &&
                                                                                  ac.Cube.X == n.X))
                                        .Count()
            })
                              .ToArray();

            var cubesToDeactivate = activeCubes
                                    .Where(c => c.ActiveNeighboursCount < 2 || c.ActiveNeighboursCount > 3)
                                    .Select(c => c.Cube);

            var cubesToActivate = inactiveNeighbours
                                  .Where(c => c.ActiveNeighboursCount == 3)
                                  .Select(c => c.Cube);

            foreach (var cube in cubesToDeactivate)
            {
                RemoveCube(cube);
            }

            foreach (var cube in cubesToActivate)
            {
                AddCube(cube);
            }
        }
Ejemplo n.º 2
0
        public void Cycle()
        {
            var activeCubesWithNeighbours = ActiveCubes
                                            .Select(c => new { Cube = c, Neighbours = GetNeighbours(c).ToArray() })
                                            .ToArray();

            var inactiveNeighbours = activeCubesWithNeighbours
                                     .Select(c => c.Neighbours)
                                     .SelectMany(n => n)
                                     .Distinct(_coordinateComparer)
                                     .Where(c => !activeCubesWithNeighbours.Any(ac => _coordinateComparer.Equals(ac.Cube, c)))
                                     .Select(c => new {
                Cube = c,
                ActiveNeighboursCount = GetNeighbours(c)
                                        .Where(n => activeCubesWithNeighbours.Any(ac => _coordinateComparer.Equals(ac.Cube, n)))
                                        .Count()
            })
                                     .ToArray();

            var activeCubes = activeCubesWithNeighbours
                              .Select(c => new
            {
                Cube = c.Cube,
                ActiveNeighboursCount = c.Neighbours
                                        .Where(n => activeCubesWithNeighbours.Any(ac => _coordinateComparer.Equals(ac.Cube, n)))
                                        .Count()
            })
                              .ToArray();

            var cubesToDeactivate = activeCubes
                                    .Where(c => c.ActiveNeighboursCount < 2 || c.ActiveNeighboursCount > 3)
                                    .Select(c => c.Cube);

            var cubesToActivate = inactiveNeighbours
                                  .Where(c => c.ActiveNeighboursCount == 3)
                                  .Select(c => c.Cube);

            foreach (var cube in cubesToDeactivate)
            {
                RemoveCube(cube);
            }

            foreach (var cube in cubesToActivate)
            {
                AddCube(cube);
            }
        }