Beispiel #1
0
 public IEnumerable <Cell> Neighbour(GameMatrix matrix)
 {
     for (int x = Point.X - 1; x <= Point.X + 1; x++)
     {
         for (int y = Point.Y - 1; y <= Point.Y + 1; y++)
         {
             if (x >= 0 && y >= 0 && x < matrix.Width && y < matrix.Height && !(x == Point.X && y == Point.Y))
             {
                 yield return(matrix[x, y]);
             }
         }
     }
 }
        public List <Cell> ProcessAllRules(GameMatrix matrix)
        {
            var changes = new List <Cell>(matrix.Width * matrix.Height);

            Cell[] allThatAreOn = matrix.Cells.Where(x => x.IsOn).ToArray();
            foreach (Cell cell in allThatAreOn.Union(allThatAreOn.SelectMany(x => x.Neighbour(matrix))).ToArray())
            {
                int neighbours = cell.Neighbour(matrix).Where(x => x.IsOn).Count();
                foreach (IRule rule in _rules)
                {
                    Cell nCell = cell.Clone();
                    if (rule.Process(nCell, neighbours))
                    {
                        changes.Add(nCell);
                    }
                }
            }
            Console.Out.WriteLine(string.Format("{0} changes", changes.Count));
            matrix.ApplyChanges(changes);
            return(changes);
        }