Пример #1
0
        private static InfiniteGrid3D RunCycle(InfiniteGrid3D grid, bool useFourthDimension)
        {
            var gridClone = new InfiniteGrid3D(grid);

            var minBounds = grid.GetMinBounds();

            minBounds.X--;
            minBounds.Y--;
            minBounds.Z--;

            var maxBounds = grid.GetMaxBounds();

            maxBounds.X++;
            maxBounds.Y++;
            maxBounds.Z++;

            if (useFourthDimension)
            {
                minBounds.W--;
                maxBounds.W++;
            }

            for (int w = (int)minBounds.W; w <= (int)maxBounds.W; w++)
            {
                for (int z = (int)minBounds.Z; z <= (int)maxBounds.Z; z++)
                {
                    for (int y = (int)minBounds.Y; y <= (int)maxBounds.Y; y++)
                    {
                        for (int x = (int)minBounds.X; x <= (int)maxBounds.X; x++)
                        {
                            int numberOfActiveNeighbours = GetNumberOfActiveNeighbours(grid, x, y, z, w, useFourthDimension);

                            var cubeState = grid.GetPoint(x, y, z, w);

                            if (cubeState == CubeState.Active && (numberOfActiveNeighbours < 2 || numberOfActiveNeighbours > 3))
                            {
                                gridClone.SetPoint(x, y, z, w, CubeState.Inactive);
                            }
                            else if (cubeState == CubeState.Inactive && numberOfActiveNeighbours == 3)
                            {
                                gridClone.SetPoint(x, y, z, w, CubeState.Active);
                            }
                        }
                    }

                    PrintGrid(gridClone, z, w);
                }
            }

            return(gridClone);
        }
Пример #2
0
        private static int GetNumberOfActiveNeighbours(InfiniteGrid3D grid, int x, int y, int z, int w, bool useFourthDimension)
        {
            int activeNeighbours = 0;

            void InnerLoop(int newW)
            {
                for (int newZ = z - 1; newZ <= z + 1; newZ++)
                {
                    for (int newY = y - 1; newY <= y + 1; newY++)
                    {
                        for (int newX = x - 1; newX <= x + 1; newX++)
                        {
                            if (newX == x && newY == y && z == newZ && w == newW)
                            {
                                continue;
                            }

                            //Logger.Debug($"{newX} {newY} {newZ} {newW}");

                            if (grid.GetPoint(newX, newY, newZ, newW) == CubeState.Active)
                            {
                                activeNeighbours++;
                            }
                        }
                    }
                }
            }

            if (useFourthDimension)
            {
                for (int newW = w - 1; newW <= w + 1; newW++)
                {
                    InnerLoop(newW);
                }
            }
            else
            {
                InnerLoop(0);
            }

            return(activeNeighbours);
        }
Пример #3
0
        private static void PrintGrid(InfiniteGrid3D grid, int z, int w)
        {
            Logger.Debug($"z={z}, w={w}");

            var minBounds = grid.GetMinBounds();
            var maxBounds = grid.GetMaxBounds();

            for (int y = (int)minBounds.Y; y <= (int)maxBounds.Y; y++)
            {
                string line = string.Empty;

                for (int x = (int)minBounds.X; x <= (int)maxBounds.X; x++)
                {
                    line += grid.GetPoint(x, y, z, w) == CubeState.Active ? "#" : ".";
                }

                Logger.Debug(line);
            }

            Logger.Debug(string.Empty);
        }