Example #1
0
        public ErisMapState Evolve()
        {
            var cellsAdjacentToBugs = GetAdjacentBugCounts(
                out HashSet <GridPoint3D> bugsNotAdjacentToOtherBugs);

            // Update cells based on rules
            var bugCells = BugCells.ToHashSet();

            foreach (var cellAdjacentToBug in cellsAdjacentToBugs)
            {
                var point = cellAdjacentToBug.Key;

                var bugCount = cellAdjacentToBug.Value;
                // Each minute, The bugs live and die based on the number of
                // bugs in the four adjacent tiles:
                // A bug dies(becoming an empty space) unless there is exactly
                // one bug adjacent to it.
                //An empty space becomes infested with a bug if exactly one or
                // two bugs are adjacent to it.
                if (!bugCells.Contains(point) &&
                    (bugCount == 1 ||
                     bugCount == 2))
                {
                    bugCells.Add(point);
                    if (MinZ > point.Z)
                    {
                        MinZ = point.Z;
                    }
                    else if (MaxZ < point.Z)
                    {
                        MaxZ = point.Z;
                    }
                }
                else if (bugCells.Contains(point) &&
                         bugCount != 1)
                {
                    bugCells.Remove(point);
                }
            }
            // Kill all bugs not adjacent to other bugs
            foreach (var bugPoint in bugsNotAdjacentToOtherBugs)
            {
                bugCells.Remove(bugPoint);
            }

            return(new ErisMapState(
                       bugCells: bugCells,
                       width: Width,
                       height: Height,
                       minZ: MinZ,
                       maxZ: MaxZ,
                       isRecursive: IsRecursive));
        }
Example #2
0
        public Dictionary <GridPoint3D, int> GetAdjacentBugCounts(
            out HashSet <GridPoint3D> bugsNotAdjacentToOtherBugs)
        {
            // Build a dictionary of all cells adjacent to bugs
            // and count the number of bugs adjacent to them
            // If the map is recursive, then add new layers if bugs are
            // adjacent to cells in the next inner or outer layer and the
            // layers haven't yet been added to the map
            var cellsAdjacentToBugs = new Dictionary <GridPoint3D, int>();

            bugsNotAdjacentToOtherBugs = BugCells.ToHashSet();
            foreach (var bugCell in BugCells)
            {
                var neighbors = GetNeighbors(bugCell);
                foreach (var neighbor in neighbors)
                {
                    if (GetIsPointOffMap(neighbor))
                    {
                        // We should only get points off the map if the map is
                        // not recursive
                        if (IsRecursive)
                        {
                            throw new Exception("Encountered point off map");
                        }
                        continue;
                    }
                    if (!cellsAdjacentToBugs.ContainsKey(neighbor))
                    {
                        cellsAdjacentToBugs.Add(neighbor, 0);
                    }
                    cellsAdjacentToBugs[neighbor]++;
                    if (bugsNotAdjacentToOtherBugs.Contains(neighbor))
                    {
                        bugsNotAdjacentToOtherBugs.Remove(neighbor);
                    }
                }
            }
            return(cellsAdjacentToBugs);
        }