Click here to Skip to main content
15,885,309 members
Articles / Programming Languages / C#

Revisit the Game of Life while Learning about Extension Methods in C#

Rate me:
Please Sign up or sign in to vote.
4.65/5 (8 votes)
24 Aug 2008CPOL11 min read 55.5K   594   40  
A fun variation of the Game of Life re-factored using extension methods
using System;
using System.Collections.Generic;
using System.Text;

namespace LifeSimulation.Extensions
{
    static class RandomizeLifeModelExtension
    {
        #region Extension methods

        public static void Randomize(this LifeModelBase model)
        {
            Random rnd = new Random(DateTime.Now.Millisecond);
            model.NeighbourDistance = rnd.Next(1, 5);
            int spawn = rnd.Next(1, 7); //Makes 1 to 6

            model.CellDeathIndicators.Clear();
            for (int i = 0; i < model.GetMaximumNeighbours(); i++)
            {
                if (i != spawn)
                {
                    //if (i > 7 || i == 0)
                    //    model.CellDeathIndicators.Add(i);
                    //else if (rnd.Next(100) < 90)
                    //    model.CellDeathIndicators.Add(i);
                    if (rnd.Next(100) < 10)
                        model.CellDeathIndicators.Add(i);
                }

            }
            model.CellSpawnIndicators.Clear();
            model.CellSpawnIndicators.Add(spawn);
            model.MaximumAge = rnd.Next(20, 200);
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions