Beispiel #1
0
        public int NumberOfSurroundingLivingCells(Map grid)
        {
            Coordinate[] surroundingCoordinates = SurroundingCoordinates();
            int          number = 0;

            foreach (Coordinate coordinate in surroundingCoordinates)
            {
                LivingCell testCell = grid.FindCellAtCoordinate(coordinate);
                if (testCell != null)
                {
                    number++;
                }
            }
            return(number);
        }
        var ParseSpawnRule(int ruleNumber)
        {
            if (ruleNumber < 1 || ruleNumber > 8)
            {
                throw new ArgumentOutOfRangeException();
            }
            var rawCellsToSpawn = new List <Coordinate>();

            foreach (LivingCell cell in _map.LivingCells.Values)
            {
                foreach (Coordinate candidate in cell.Coordinate.SurroundingCoordinates())
                {
                    int surroundingNumber = candidate.NumberOfSurroundingLivingCells(_map);

                    bool candidateIsLivingCell = _map.FindCellAtCoordinate(candidate) != null;
                    if (surroundingNumber == ruleNumber && !candidateIsLivingCell)      //If the number matches, but also if the candidate doesn't exist already
                    {
                        rawCellsToSpawn.Add(candidate);                                 //we desire to only check the neighbours of the living cells from this state,
                    }                                                                   //not the neighbors of living cells being added in the foreach loop, hence the new list
                }
            }

            return(rawCellsToSpawn);
        }