コード例 #1
0
        public ConwayController(IEnumerable <string> input, int dimensions = DefaultDimensions)
        {
            //< Hold onto the raw input as a list
            this.Source = input.ToList();
            //< Retain the dimensionality
            this.Dimensions = dimensions;

            this.CubeMap = new Dictionary <string, ConwayCube>();
            //< Iterate over each row (y)
            foreach (int y in Enumerable.Range(0, Source.Count))
            {
                //< Iterate over each available (x) position
                foreach (int x in Enumerable.Range(0, Source[y].Length))
                {
                    char state = Source[y][x];
                    //< If this cube is active -> add to the map
                    if (state == ConwayCube.Active)
                    {
                        //< Parse the starting position
                        var pos = GetStartingPosition(x, y, Dimensions);
                        //< Generate the new ConwayCube object and add to the dimension map
                        var cube = new ConwayCube(pos, state);
                        CubeMap.Add(cube.Index, cube);
                    }
                }
            }
        }
コード例 #2
0
        private char GetNewState(IEnumerable <string> neighs, char state)
        {
            //< Count active neighbour cubes
            int numActiveNeighs = neighs.Count(x => CubeMap.ContainsKey(x));

            //< Get the resulting state and return it
            return(ConwayCube.GetNewState(state, numActiveNeighs));
        }
コード例 #3
0
        public static IEnumerable <string> GetNeighbours(string index, int dim)
        {
            //< Get the position array for this index
            var pos = ConwayCube.GetPosition(index);
            //< Get the neighbouring positions (removing the position at the given index)
            var neighs = GetOffsets(dim).Select(offset => GetPosition(pos, offset))
                         .Select(p => GetPositionString(p))
                         .Where(idx => idx != index);

            return(neighs);
        }
コード例 #4
0
        public void SimulateSteps(int steps)
        {
            //< Each cube gets a state change simultaneously
            foreach (int step in Enumerable.Range(0, steps))
            {
                //< Instantiate map of any changes to existing cubes
                var changeMap = new Dictionary <string, char>();
                //< Instantiate map of any 'new' neighbours to be added after this turn
                var neighsToCheck = new HashSet <string>();

                //< Iterate over each currently active cube in the current iteration of the map
                foreach (var kvp in CubeMap)
                {
                    //< Get all the neighbour positions/indices
                    var neighs = ConwayCube.GetNeighbours(kvp.Key, Dimensions);
                    //< Check if we need to change this cube's state
                    var newState = GetNewState(neighs, kvp.Value.State);
                    if (newState != kvp.Value.State)
                    {
                        changeMap.Add(kvp.Key, newState);
                    }
                    //< Add all newly encountered (not in the map) neighbours to be considered
                    foreach (var neigh in neighs.Where(n => !CubeMap.ContainsKey(n)))
                    {
                        if (!neighsToCheck.Contains(neigh))
                        {
                            neighsToCheck.Add(neigh);
                        }
                    }
                }

                //< Add any newly-encountered neighbours to the map between turns
                foreach (var neigh in neighsToCheck)
                {
                    //< Get all the neighbour positions/indices
                    var neighs = ConwayCube.GetNeighbours(neigh, Dimensions);
                    //< Check if we need to change this cube's state
                    var newState = GetNewState(neighs, ConwayCube.Inactive);
                    if (newState == ConwayCube.Active)
                    {
                        changeMap.Add(neigh, newState);
                    }
                }

                //< Apply changes
                ApplyChanges(changeMap);
            }
        }