Beispiel #1
0
        public void CopyTo(LifeTorus dest)
        {
            // Copies this torus to another usually differently sized torus
            int width  = Math.Min(this._width, dest._width);
            int height = Math.Min(this._height, dest._height);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    dest[x, y] = this[x, y];
                }
            }
        }
        /// <summary>
        /// Advances the game to the next generation.
        /// Returns true if changes have occurred, i.e. any cell was born or has died.
        /// This is typically used to halt any automation once no further changes occur.
        /// In actual fact the game will often enter into sequence where ti alternates between two states, e.g.
        /// the 'blinker'. At this stage we don't detect such repeating sequences.
        /// </summary>
        /// <returns></returns>
        public bool Next()
        {
            LifeTorus next    = new LifeTorus(_dim);
            bool      changed = false;

            for (int x = 0; x < _dim.Width; x++)
            {
                for (int y = 0; y < _dim.Height; y++)
                {
                    // Calculate the number of alive neighbours to this cell.
                    // The grid is a torus with left edge adjacent to the right and top edge adjacent to the
                    // bottom edge. This wrap around torus function is implemented in the LifeTorus class so
                    // no special coding is required here.
                    int neighbours = 0;

                    // Top row
                    if (_current[x - 1, y - 1])
                    {
                        neighbours++;
                    }
                    if (_current[x, y - 1])
                    {
                        neighbours++;
                    }
                    if (_current[x + 1, y - 1])
                    {
                        neighbours++;
                    }

                    // Bottom row
                    if (_current[x - 1, y + 1])
                    {
                        neighbours++;
                    }
                    if (_current[x, y + 1])
                    {
                        neighbours++;
                    }
                    if (_current[x + 1, y + 1])
                    {
                        neighbours++;
                    }

                    // Either side
                    if (_current[x - 1, y])
                    {
                        neighbours++;
                    }
                    if (_current[x + 1, y])
                    {
                        neighbours++;
                    }

                    if (_current[x, y])
                    {
                        // Its alive now
                        if (neighbours == 2 || neighbours == 3)
                        {
                            next[x, y] = true;
                        }
                        else
                        {
                            changed = true;
                        }
                    }
                    else
                    {
                        // Its dead but will be born if ==3
                        if (neighbours == 3)
                        {
                            next[x, y] = true;
                            changed    = true;
                        }
                    }
                }
            }
            _current = next;
            return(changed);
        }
 public LifeModel(Dimensions dim)
 {
     _dim     = dim;
     _current = new LifeTorus(dim);
 }