Exemplo n.º 1
0
        public Piece Run(PieceGrid currentGen, Point point, Piece piece)
        {
            int aliveNeighbors = PointHelpers.GetAdjacentPointsToroid(point, currentGen, PointHelpers.NeighborhoodOrder.Moore).Count(p => currentGen.PointPieces[p].StateValue > 0);

            switch (piece.StateValue)
            {
            case 0:
                if (aliveNeighbors == 3)
                {
                    return(Piece.Get(1));
                }
                else
                {
                    return(Piece.Get(0));
                }

            case 1:
            default:
                if (aliveNeighbors < 2 || aliveNeighbors > 3)
                {
                    return(Piece.Get(0));
                }
                else
                {
                    return(Piece.Get(1));
                }
            }
        }
Exemplo n.º 2
0
        public Piece Run(PieceGrid currentGen, Point point, Piece piece)
        {
            PointHelpers.NeighborhoodOrder neighborhoodOrder;
            if (Neighborhood == CANeighborhood.Moore)
            {
                neighborhoodOrder = PointHelpers.NeighborhoodOrder.MooreRuleTable;
            }
            else
            {
                neighborhoodOrder = PointHelpers.NeighborhoodOrder.VonNeumannRuleTable;
            }
            IEnumerable <Point> neighborhoodPoints = PointHelpers.GetAdjacentPointsToroid(point, currentGen, neighborhoodOrder);
            List <int>          neighborhood       = new List <int>();

            foreach (Point p in neighborhoodPoints)
            {
                neighborhood.Add(currentGen.PointPieces[p].StateValue);
            }

            List <int> sortedNeighborhood = neighborhood.OrderBy(s => s).ToList();

            sortedNeighborhood.Insert(0, piece.StateValue);

            if (RuleDictionary.TryGetValue(GetKeyString(sortedNeighborhood), out int stateValue))
            {
                return(Piece.Get(stateValue));
            }
            else
            {
                return(piece);
            }
        }
Exemplo n.º 3
0
        public Piece Run(PieceGrid currentGen, Point point, Piece piece)
        {
            int aliveNeighbors = PointHelpers.GetAdjacentPointsToroid(point, currentGen, PointHelpers.NeighborhoodOrder.Moore).Count(p => currentGen.PointPieces[p].StateValue > 0);

            switch (piece.StateValue)
            {
            case 1:
                if (SurvivalNeighborCounts.Contains(aliveNeighbors))
                {
                    return(Piece.Get(1));
                }
                else
                {
                    return(Piece.Get(0));
                }

            case 0:
                if (BirthNeighborCounts.Contains(aliveNeighbors))
                {
                    return(Piece.Get(1));
                }
                else
                {
                    return(Piece.Get(0));
                }

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 4
0
        public Piece Run(PieceGrid currentGen, Point point, Piece piece)
        {
            int aliveNeighbors = PointHelpers.GetAdjacentPointsToroid(point, currentGen, PointHelpers.NeighborhoodOrder.Moore).Sum(p => currentGen.PointPieces[p].StateValue);

            switch (piece.StateValue)
            {
            case 1:
                if (aliveNeighbors < 2 || aliveNeighbors > 3)
                {
                    return(Piece.Get(0));
                }
                else
                {
                    return(Piece.Get(1));
                }

            case 0:
                if (aliveNeighbors == 3 || aliveNeighbors == 6)
                {
                    return(Piece.Get(1));
                }
                else
                {
                    return(Piece.Get(0));
                }

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 5
0
        public virtual void ExecuteGameLoop()
        {
            PieceGrid nextGen = _currentGen.Clone();
            Dictionary <Point, ICARule> nextRulePoints = new Dictionary <Point, ICARule>();

            foreach (var kvp in _currentGen.PointPieces)
            {
                bool         existingRule   = _rulePoints.TryGetValue(kvp.Key, out ICARule rule);
                List <Point> nPoints        = PointHelpers.GetAdjacentPointsToroid(kvp.Key, _currentGen, PointHelpers.NeighborhoodOrder.Moore).ToList();
                bool         aliveNeighbors = nPoints.Any(p => _currentGen.PointPieces[p].StateValue > 0);
                if (!existingRule && !aliveNeighbors)
                {
                    continue;
                }

                if (!existingRule)
                {
                    rule = _ruleChooser.Choose(_random, this, nPoints);
                    nextRulePoints.Add(kvp.Key, rule);
                    _rulePointsAge[kvp.Key] = 1;
                }
                else if (!aliveNeighbors)
                {
                    _rulePointsAge[kvp.Key] = 0;
                }
                else
                {
                    nextRulePoints.Add(kvp.Key, rule);
                    if (_rulePointsAge.ContainsKey(kvp.Key))
                    {
                        _rulePointsAge[kvp.Key]++;
                    }
                    else
                    {
                        _rulePointsAge[kvp.Key] = 1;
                    }
                }
                nextGen.PointPieces[kvp.Key] = rule.Run(_currentGen, kvp.Key);
            }

            _rulePoints = nextRulePoints;
            _currentGen = nextGen;
        }
Exemplo n.º 6
0
        public PieceGrid Run(PieceGrid currentGen)
        {
            tweakPoints1.Clear();
            tweakPoints2.Clear();
            PieceGrid nextGen = currentGen.Clone();

            foreach (var kvp in currentGen.PointPieces)
            {
                nextGen.PointPieces[kvp.Key] = Run(currentGen, kvp.Key, kvp.Value);
            }

            // add live player cells on the board trying to increase at the center
            Point goalPoint = new Point(50, 50);

            if (tweakPoints1.Any() && _xtraCount1 > 0)
            {
                foreach (Point p in tweakPoints1.OrderBy(p => p.Distance(goalPoint)))
                {
                    int aliveNeighbors = PointHelpers.GetAdjacentPointsToroid(p, nextGen, PointHelpers.NeighborhoodOrder.Moore).Sum(n => nextGen.PointPieces[n].StateValue);
                    if (aliveNeighbors == 2)
                    {
                        nextGen.PointPieces[p] = Piece.Get(1, Owner.Player1, PieceAspect.Played);
                        _xtraCount1--;
                        break;
                    }
                }
            }

            if (tweakPoints2.Any() && _xtraCount2 > 0)
            {
                foreach (Point p in tweakPoints2.OrderBy(p => p.Distance(goalPoint)))
                {
                    int aliveNeighbors = PointHelpers.GetAdjacentPointsToroid(p, nextGen, PointHelpers.NeighborhoodOrder.Moore).Sum(n => nextGen.PointPieces[n].StateValue);
                    if (aliveNeighbors == 2)
                    {
                        nextGen.PointPieces[p] = Piece.Get(1, Owner.Player2, PieceAspect.Played);
                        _xtraCount2--;
                        break;
                    }
                }
            }
            return(nextGen);
        }
Exemplo n.º 7
0
        private Piece Run(PieceGrid currentGen, Point point, Piece piece)
        {
            Piece returnPiece;

            int aliveNeighbors = PointHelpers.GetAdjacentPointsToroid(point, currentGen, PointHelpers.NeighborhoodOrder.Moore)
                                 .Sum(p => currentGen.PointPieces[p].StateValue);
            int aliveNeighbors1 = PointHelpers.GetAdjacentPointsToroid(point, currentGen, PointHelpers.NeighborhoodOrder.Moore)
                                  .Where(p => currentGen.PointPieces[p].Owner == Owner.Player1)
                                  .Sum(p => currentGen.PointPieces[p].StateValue);
            int aliveNeighbors2 = PointHelpers.GetAdjacentPointsToroid(point, currentGen, PointHelpers.NeighborhoodOrder.Moore)
                                  .Where(p => currentGen.PointPieces[p].Owner == Owner.Player2)
                                  .Sum(p => currentGen.PointPieces[p].StateValue);

            switch (piece.StateValue)
            {
            case 1:
                if (aliveNeighbors < 2 || aliveNeighbors > 3)
                {
                    returnPiece = Piece.Get(0);
                }
                else
                {
                    returnPiece = Piece.Get(1);
                }
                break;

            case 0:
                bool added = false;
                if (aliveNeighbors == 3)
                {
                    added = true;
                    if (aliveNeighbors1 > aliveNeighbors2)
                    {
                        returnPiece = Piece.Get(1, Owner.Player1);
                    }
                    else if (aliveNeighbors2 > aliveNeighbors1)
                    {
                        returnPiece = Piece.Get(1, Owner.Player2);
                    }
                    else
                    {
                        returnPiece = Piece.Get(1, Owner.None);
                    }
                }
                else
                {
                    returnPiece = Piece.Get(0);
                }

                if (aliveNeighbors1 > 0 && !added)
                {
                    tweakPoints1.Add(point);
                }
                if (aliveNeighbors2 > 0 && !added)
                {
                    tweakPoints2.Add(point);
                }
                break;

            default:
                throw new NotImplementedException();
            }
            return(returnPiece);
        }