Пример #1
0
 private void Random_Button_Click(object sender, RoutedEventArgs e)
 {
     Stop();
     for (int i = 0; i < Grid.Count; i++)
     {
         for (int j = 0; j < Grid.ElementAt(0).Count; j++)
         {
             Grid.ElementAt(i).ElementAt(j).IsAlive = (gen.Next(3) == 0);
         }
     }
 }
Пример #2
0
 public void Clear_Button_Click(object sender, RoutedEventArgs e)
 {
     Stop();
     for (int i = 0; i < Grid.Count; i++)
     {
         for (int j = 0; j < Grid.ElementAt(0).Count; j++)
         {
             Grid.ElementAt(i).ElementAt(j).IsAlive = false;
         }
     }
 }
Пример #3
0
        public void GenerateGlider()
        {
            int         x           = gen.Next(_rowMax - 6) + 3;
            int         y           = gen.Next(_colMax - 6) + 3;
            List <Cell> gliderCells = new List <Cell>();

            gliderCells.Add(Grid.ElementAt(x).ElementAt(y));

            int direction = gen.Next(4);

            switch (direction)
            {
            case 0:
                gliderCells.Add(Grid.ElementAt(x).ElementAt(y + 1));
                gliderCells.Add(Grid.ElementAt(x).ElementAt(y - 1));
                gliderCells.Add(Grid.ElementAt(x + 1).ElementAt(y + 1));
                gliderCells.Add(Grid.ElementAt(x + 2).ElementAt(y));
                break;

            case 1:
                gliderCells.Add(Grid.ElementAt(x - 1).ElementAt(y));
                gliderCells.Add(Grid.ElementAt(x + 1).ElementAt(y));
                gliderCells.Add(Grid.ElementAt(x + 1).ElementAt(y + 1));
                gliderCells.Add(Grid.ElementAt(x).ElementAt(y + 2));
                break;

            case 2:
                gliderCells.Add(Grid.ElementAt(x - 1).ElementAt(y));
                gliderCells.Add(Grid.ElementAt(x + 1).ElementAt(y));
                gliderCells.Add(Grid.ElementAt(x - 1).ElementAt(y + 1));
                gliderCells.Add(Grid.ElementAt(x).ElementAt(y + 2));
                break;

            default:
                gliderCells.Add(Grid.ElementAt(x).ElementAt(y + 1));
                gliderCells.Add(Grid.ElementAt(x).ElementAt(y - 1));
                gliderCells.Add(Grid.ElementAt(x - 1).ElementAt(y + 1));
                gliderCells.Add(Grid.ElementAt(x - 2).ElementAt(y));
                break;
            }



            foreach (Cell c in gliderCells)
            {
                c.IsAlive = true;
            }
        }
Пример #4
0
        public void Tick()
        {
            for (int i = 0; i < Grid.Count; i++)
            {
                for (int j = 0; j < Grid.ElementAt(0).Count; j++)
                {
                    _tempCopy[i, j] = Grid.ElementAt(i).ElementAt(j).IsAlive;
                }
            }

            int x, y;

            for (int i = 0; i < Grid.Count; i++)
            {
                for (int j = 0; j < Grid.ElementAt(0).Count; j++)
                {
                    Cell currentCell = Grid.ElementAt(i).ElementAt(j);
                    x = i;
                    y = j;
                    int count = 0;
                    foreach (Cell c in currentCell.Neighbors)
                    {
                        if (c != null && _tempCopy[c.X, c.Y])
                        {
                            count++;
                        }
                    }
                    if (currentCell.IsAlive)
                    {
                        if (count < 2 || count > 3)
                        {
                            currentCell.IsAlive = false;
                        }
                    }
                    else
                    {
                        if (count == 3)
                        {
                            currentCell.IsAlive = true;
                        }
                        if (HighLifeMode && count == 6)
                        {
                            currentCell.IsAlive = true;
                        }
                    }
                }
            }
        }
Пример #5
0
        public void GenerateRPentomino()
        {
            int         x         = gen.Next(_rowMax - 2) + 1;
            int         y         = gen.Next(_colMax - 2) + 1;
            List <Cell> pentCells = new List <Cell>();

            pentCells.Add(Grid.ElementAt(x).ElementAt(y));
            pentCells.Add(Grid.ElementAt(x).ElementAt(y + 1));
            pentCells.Add(Grid.ElementAt(x + 1).ElementAt(y + 1));
            pentCells.Add(Grid.ElementAt(x).ElementAt(y - 1));
            pentCells.Add(Grid.ElementAt(x - 1).ElementAt(y));

            foreach (Cell c in pentCells)
            {
                c.IsAlive = true;
            }
        }
Пример #6
0
        private void BuildNeighborList(Cell c, int x, int y)
        {
            Cell[] neighbors = new Cell[8];
            int    colMax    = Grid.Count;
            int    rowMax    = Grid.ElementAt(0).Count;

            if (x > 0 && x < colMax - 1 && y > 0 && y < rowMax - 1)
            {
                neighbors[0] = Grid.ElementAt(Math.Abs(x - 1) % colMax).ElementAt(Math.Abs(y) % rowMax);
                neighbors[1] = Grid.ElementAt(Math.Abs(x + 1) % colMax).ElementAt(Math.Abs(y) % rowMax);
                neighbors[2] = Grid.ElementAt(Math.Abs(x) % colMax).ElementAt(Math.Abs(y - 1) % rowMax);
                neighbors[3] = Grid.ElementAt(Math.Abs(x) % colMax).ElementAt(Math.Abs(y + 1) % rowMax);
                neighbors[4] = Grid.ElementAt(Math.Abs(x + 1) % colMax).ElementAt(Math.Abs(y + 1) % rowMax);
                neighbors[5] = Grid.ElementAt(Math.Abs(x + 1) % colMax).ElementAt(Math.Abs(y - 1) % rowMax);
                neighbors[6] = Grid.ElementAt(Math.Abs(x - 1) % colMax).ElementAt(Math.Abs(y - 1) % rowMax);
                neighbors[7] = Grid.ElementAt(Math.Abs(x - 1) % colMax).ElementAt(Math.Abs(y + 1) % rowMax);
            }

            c.Neighbors = neighbors;
        }