Example #1
0
        /// <summary>
        /// Checks whether or not the cell should be alive or dead.
        /// </summary>
        /// <param name="f"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void CheckConditions(Field f, int x, int y)
        {
            int aliveNeighbors = CountAliveNeighbors(f,x,y);
            if (aliveNeighbors > 0)
            {
                //Console.WriteLine(aliveNeighbors.ToString());
            }

            if (Alive)
            {
                if (aliveNeighbors != 2 && aliveNeighbors != 3)
                {
                    f.AddCellToChanged(this);
                }
            }
            else
            {
                if (aliveNeighbors == 3)
                {
                    f.AddCellToChanged(this);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Counts all the alive neighbors of the cell.
        /// </summary>
        /// <param name="f"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        private int CountAliveNeighbors(Field f, int x, int y)
        {
            Cell[,] cells = f.Cells;
            int width = f.WidthInCells;
            int height = f.HeightInCells;
            int count = 0;

            Point left_top = FormatPoint(new Point(x - 1, y - 1), f);
            Point mid_top = FormatPoint(new Point(x, y - 1), f);
            Point right_top = FormatPoint(new Point(x + 1, y - 1), f);
            Point left_mid = FormatPoint(new Point(x - 1, y), f);
            Point right_mid = FormatPoint(new Point(x + 1, y), f);
            Point left_bot = FormatPoint(new Point(x - 1, y + 1), f);
            Point mid_bot = FormatPoint(new Point(x, y + 1), f);
            Point right_bot = FormatPoint(new Point(x + 1, y + 1), f);

            if (cells[left_top.X, left_top.Y].Alive)
            {
                count++;
            }
            if (cells[mid_top.X, mid_top.Y].Alive)
            {
                count++;
            }
            if (cells[right_top.X, right_top.Y].Alive)
            {
                count++;
            }
            if (cells[left_mid.X, left_mid.Y].Alive)
            {
                count++;
            }
            if (cells[right_mid.X, right_mid.Y].Alive)
            {
                count++;
            }
            if (cells[left_bot.X, left_bot.Y].Alive)
            {
                count++;
            }
            if (cells[mid_bot.X, mid_bot.Y].Alive)
            {
                count++;
            }
            if (cells[right_bot.X, right_bot.Y].Alive)
            {
                count++;
            }

            return count;
        }
Example #3
0
        /// <summary>
        /// Formats the point if it is not contained in the field.
        /// </summary>
        /// <param name="p"></param>
        private Point FormatPoint(Point p, Field f)
        {
            int width = f.WidthInCells;
            int height = f.HeightInCells;
            Rectangle cellBounds = new Rectangle(0, 0, width, height);

            if (!cellBounds.Contains(p))
            {
                int newX = p.X;
                int newY = p.Y;
                if (p.X < 0)
                {
                    newX = width - 1;
                }
                if (p.X >= width)
                {
                    newX = 0;
                }
                if (p.Y < 0)
                {
                    newY = height - 1;
                }
                if (p.Y >= height)
                {
                    newY = 0;
                }
                p = new Point(newX, newY);
            }
            return p;
        }
Example #4
0
 /// <summary>
 /// LoadContent will be called once per game and is the place to load
 /// all of your content.
 /// </summary>
 protected override void LoadContent()
 {
     spriteBatch = new SpriteBatch(GraphicsDevice);
     cell = new Texture2D(GraphicsDevice, 1, 1);
     cell.SetData(new Color[] { Color.White });
     font = Content.Load<SpriteFont>("Verdana");
     field = new Field(field_W, field_H, cellSize, cell, buffer, interval, this);
     // TODO: use this.Content to load your game content here
 }