示例#1
0
        public void InsertPattern(CAPattern pattern, Point insertPoint, int rank = -1)
        {
            if (!_allRules.Contains(pattern.Rule))
            {
                _allRules.Add(pattern.Rule);
            }
            if (!_ruleChooser.TryGetRuleRank(pattern.Rule, out int existingRank))
            {
                if (rank >= 0)
                {
                    _ruleChooser.SetRuleRank(pattern.Rule, rank);
                }
                else
                {
                    _ruleChooser.SetRuleRank(pattern.Rule, _allRules.IndexOf(pattern.Rule));
                }
            }

            if (_currentGen.IsOutOfBounds(insertPoint))
            {
                throw new ArgumentException($"Insert point {insertPoint} is outside given PieceGrid (size {_currentGen.Size})");
            }
            if (pattern.Pattern.Size > _currentGen.Size)
            {
                throw new ArgumentException($"Pattern too big for given PieceGrid: {_currentGen.Size} < {pattern.Pattern.Size}");
            }
            foreach (var kvp in pattern.Pattern.PointPieces)
            {
                IEnumerable <Point> nPoints = kvp.Key.GetAdjacentPointsNotOutOfBounds(pattern.Pattern, PointHelpers.NeighborhoodOrder.Moore);
                bool aliveNeighbors         = nPoints.Any(pt => pattern.Pattern.PointPieces[pt].StateValue > 0);
                if (aliveNeighbors)
                {
                    Point p = insertPoint.AddPointsToroid(kvp.Key, _currentGen);
                    _currentGen.PointPieces[p] = kvp.Value;
                    _rulePoints[p]             = pattern.Rule;
                }
            }
        }