Exemple #1
0
 private State GetState(LongPoint4D point)
 {
     if (_state.TryGetValue(point, out Item item))
     {
         return(item.Previous);
     }
     return(State.Inactive);
 }
Exemple #2
0
        private int Run(int steps, LongPoint incX, LongPoint incY, LongPoint incZ, LongPoint incW)
        {
            IList <LongPoint4D> offsets = new List <LongPoint4D>();

            for (long x = incX.X; x <= incX.Y; x++)
            {
                for (long y = incY.X; y <= incY.Y; y++)
                {
                    for (long z = incZ.X; z <= incZ.Y; z++)
                    {
                        for (long w = incW.X; w <= incW.Y; w++)
                        {
                            LongPoint4D offset = new LongPoint4D(x, y, z, w);
                            if (LongPoint4D.Zero != offset)
                            {
                                offsets.Add(offset);
                            }
                        }
                    }
                }
            }

            // Initial data size
            LongPoint newX = new LongPoint(0, 7);
            LongPoint newY = new LongPoint(0, 7);
            LongPoint newZ = new LongPoint(0, 0);
            LongPoint newW = new LongPoint(0, 0);

            do
            {
                // Increment in each direction
                newX += incX;
                newY += incY;
                newZ += incZ;
                newW += incW;

                // Compute the new state
                Step(offsets, newX, newY, newZ, newW);
            }while (--steps > 0);

            return(_state.Values.Count(i => i.Previous == State.Active));
        }
Exemple #3
0
        private void Update(LongPoint4D point, IEnumerable <LongPoint4D> offsets)
        {
            int activeNeighbors = CountActiveNeighbors(point, offsets);

            if (!_state.TryGetValue(point, out Item item))
            {
                item = new Item()
                {
                    Previous = State.Inactive, Next = State.Inactive
                };
                _state.Add(point, item);
            }

            switch (item.Previous)
            {
            case State.Active:
                if (activeNeighbors == 2 || activeNeighbors == 3)
                {
                    item.Next = State.Active;
                }
                else
                {
                    item.Next = State.Inactive;
                }
                break;

            case State.Inactive:
                if (activeNeighbors == 3)
                {
                    item.Next = State.Active;
                }
                else
                {
                    item.Next = State.Inactive;
                }
                break;
            }
        }
Exemple #4
0
 private int CountActiveNeighbors(LongPoint4D point, IEnumerable <LongPoint4D> offsets)
 {
     return(offsets.Count(o => State.Active == GetState(point + o)));
 }