예제 #1
0
        public void AddLife(int x, int y)
        {
            var newLife = new LifeInstance(x, y);

            if (!AllLife.Add(newLife))
            {
                throw new InvalidOperationException($"Life {newLife} already exists");
            }
        }
예제 #2
0
        public override bool Equals(object obj)
        {
            LifeInstance compareInstance = (LifeInstance)obj;

            if (compareInstance != null && compareInstance.X == X && compareInstance.Y == Y)
            {
                return(true);
            }

            return(false);
        }
예제 #3
0
        public int GetNumberOfNeighbours(LifeInstance life)
        {
            int numberOfNeighbours = 0;

            var neighbouringCells = life.GetNeighbouringCells();

            foreach (var cell in neighbouringCells)
            {
                if (AllLife.Contains(cell))
                {
                    numberOfNeighbours++;
                }
            }

            return(numberOfNeighbours);
        }
예제 #4
0
        public List <LifeInstance> GetNeighbouringCells()
        {
            List <LifeInstance> neighbouringCells = new List <LifeInstance>();

            for (int deltaX = -1; deltaX < 2; deltaX++)
            {
                for (int deltaY = -1; deltaY < 2; deltaY++)
                {
                    var potentialCell = new LifeInstance(X + deltaX, Y + deltaY);
                    if (!potentialCell.Equals(this))
                    {
                        neighbouringCells.Add(potentialCell);
                    }
                }
            }
            return(neighbouringCells);
        }
예제 #5
0
 public void AddLife(LifeInstance life)
 {
     AddLife(life.X, life.Y);
 }