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); } }
public void AddActiveCube(int x, int y, int z) { x += _xOffset; y += _yOffset; z += _zOffset; if (Space[x, y, z].IsActive) { throw new InvalidOperationException($"Cube already exists at point {x}, {y}, {z}"); } Space[x, y, z].IsActive = true; ActiveCubes.Add(Space[x, y, z]); }
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); } }