示例#1
0
 private void SetNeightboursOfBacteria(List <Bacteria> bacterias, Bacteria curBacteria, int width, int height)
 {
     for (int yPos = curBacteria.Y - 1; yPos <= curBacteria.Y + 1; yPos++)
     {
         if (yPos >= 0 && yPos <= height - 1)
         {
             for (int xPos = curBacteria.X - 1; xPos <= curBacteria.X + 1; xPos++)
             {
                 if (xPos >= 0 && xPos <= width - 1)
                 {
                     var neighbourBacteria = bacterias.FirstOrDefault(x => x.X == xPos && x.Y == yPos);
                     if (neighbourBacteria != null)
                     {
                         curBacteria.Neighbours.Add(neighbourBacteria);
                         neighbourBacteria.Neighbours.Add(curBacteria);
                     }
                 }
             }
         }
     }
 }
示例#2
0
        /// <summary>
        /// Generates a list of bacterias. Amounts depends on the width and height parameter.
        /// </summary>
        /// <param name="width">Cell count on the x axis</param>
        /// <param name="height">Cell count on the y axis</param>
        /// <returns>A list of bacetrias</returns>
        public List <Bacteria> GenerateBacterias(int width, int height)
        {
            _generation = 0;
            var ret = new List <Bacteria>();

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    var newBacteria = new Bacteria
                    {
                        X          = x,
                        Y          = y,
                        IsAlive    = GenerateBacteriaAliveStatus(),
                        Neighbours = new List <Bacteria>()
                    };
                    SetNeightboursOfBacteria(ret, newBacteria, width, height);
                    ret.Add(newBacteria);
                }
            }
            return(ret);
        }