Esempio n. 1
0
        /// <summary>
        ///  Game of life logic
        /// </summary>
        private void updateLifeStatus(GOLCell cell)
        {
            int liveNeighbours = cell.LiveNeighboursCount;

            if (cell.IsAlive)
            {
                if (liveNeighbours < 2)
                {
                    cell.IsAlive = false;
                }
                else if (liveNeighbours == 2 || liveNeighbours == 3)
                {
                    cell.IsAlive = true;
                }
                else if (liveNeighbours > 3)
                {
                    cell.IsAlive = false;
                }
            }
            else
            {
                if (liveNeighbours == 3)
                {
                    cell.IsAlive = true;
                }
            }
        }
Esempio n. 2
0
 /// <summary>
 ///  Updating the cel living neighbours
 /// </summary>
 private void updateCellNeighboursCount()
 {
     for (int i = 0; i < _rows; ++i)
     {
         for (int j = 0; j < _columns; ++j)
         {
             GOLCell cell = _gameGrid.GetCell(i, j);
             _neighberhood.UpdateLiveNeighborsCount(cell);
         }
     }
 }
Esempio n. 3
0
 public void GetInfo(GOLCell cell, out int liveCount)
 {
     liveCount = 0;
     foreach (var item in cell.AroundCells)
     {
         if (item.State == CellState.Live)
         {
             liveCount++;
         }
     }
 }
Esempio n. 4
0
        private void init()
        {
            if (_rows > 0 && _columns > 0)
            {
                _gridCells = new GOLCell[_rows, _columns];

                for (int i = 0; i < _rows; ++i)
                {
                    for (int j = 0; j < _columns; ++j)
                    {
                        _gridCells[i, j] = new GOLCell(i, j);
                    }
                }
            }
        }
Esempio n. 5
0
        internal void SetState(GOLCell cell, CellState state)
        {
            if (cell.State != state)
            {
                cell.State = state;
                switch (state)
                {
                case CellState.Live:
                    cell.Rect.Fill = liveBrush;
                    break;

                case CellState.Dead:
                    cell.Rect.Fill = deadBrush;
                    break;

                default:
                    break;
                }
            }
        }
Esempio n. 6
0
 public CellState GetNewState(GOLCell cell)
 {
     if (cell.State == CellState.Live) // calculate state for live cell
     {
         if (survive.Contains(cell.LiveCount))
         {
             return(CellState.Live);
         }
         else
         {
             return(CellState.Dead);
         }
     }
     else // calculate state for dead cell
     {
         if (reborn.Contains(cell.LiveCount))
         {
             return(CellState.Live);
         }
     }
     return(cell.State);
 }
Esempio n. 7
0
        public GOL(Canvas canvas, int width, int height, string pattern, IRules rules, Func <bool> isStopped, Action wasStopped)
        {
            this.isStopped  = isStopped;
            this.wasStopped = wasStopped;
            this.Rules      = rules;
            this.canvas     = canvas;
            this.width      = width;
            this.height     = height;
            canvas.Children.Clear();
            rctWidth  = canvas.ActualWidth / width;
            rctHeight = canvas.ActualHeight / height;

            rctWidth = rctHeight = Math.Min(rctWidth, rctHeight);

            Cells    = new GOLCell[width, height];
            lstCells = new List <GOLCell>();
            rules.SetCells(Cells);
            // create cells
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    var cell = new GOLCell(x, y);
                    lstCells.Add(cell);
                    Cells[x, y] = cell;
                    cell.Left   = x * rctWidth;
                    cell.Top    = y * rctHeight;
                    cell.Rect   = new Rectangle
                    {
                        Width           = rctWidth,
                        Height          = rctHeight,
                        Stroke          = Brushes.LightGray,
                        StrokeThickness = 0.6
                    };
                    //cell.Rect.ToolTip = "0";
                    cell.Rect.Tag = cell;
                    Canvas.SetLeft(cell.Rect, cell.Left);
                    Canvas.SetTop(cell.Rect, cell.Top);
                    canvas.Children.Add(cell.Rect);
                    SetState(cell, CellState.Dead);
                }
            }

            // fill around cells
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    var cell = Cells[x, y];

                    for (int ay = -1; ay <= 1; ay++)
                    {
                        for (int ax = -1; ax <= 1; ax++)
                        {
                            if (ay == 0 && ax == 0)
                            {
                                continue;                     // itself
                            }
                            var ary = y + ay;
                            var arx = x + ax;
                            if (!isValidNeighbour(arx, ary, out var oarx, out var oary))
                            {
                                continue;
                            }
                            cell.AroundCells.Add(Cells[oarx, oary]);
                        }
                    }
                }
            }
            switch (pattern)
            {
            case "Test1":
                AddPatternTest1();
                break;

            case "Test2":
                AddPatternTest2();
                break;

            case "Test3":
                AddPatternTest3();
                break;

            case "Test4":
                AddPatternTest4();
                break;

            default:
                break;
            }
        }
Esempio n. 8
0
        /// <summary>
        ///  Updating the live cell neighbors of the cell (total of 8 neighbours)
        /// </summary>
        public void UpdateLiveNeighborsCount(GOLCell cell)
        {
            int neighborsCount = 0;

            // go left
            if (cell.PositionY > 0)
            {
                if (_grid.GetCell(cell.PositionX, cell.PositionY - 1).IsAlive)
                {
                    ++neighborsCount;
                }
            }

            // go right
            if (cell.PositionY < _grid.Columns - 1)
            {
                if (_grid.GetCell(cell.PositionX, cell.PositionY + 1).IsAlive)
                {
                    ++neighborsCount;
                }
            }

            // go up
            if (cell.PositionX > 0)
            {
                if (_grid.GetCell(cell.PositionX - 1, cell.PositionY).IsAlive)
                {
                    ++neighborsCount;
                }
            }

            // go down
            if (cell.PositionX < _grid.Rows - 1)
            {
                if (_grid.GetCell(cell.PositionX + 1, cell.PositionY).IsAlive)
                {
                    ++neighborsCount;
                }
            }

            // go up left
            if (cell.PositionY > 0 && cell.PositionX > 0)
            {
                if (_grid.GetCell(cell.PositionX - 1, cell.PositionY - 1).IsAlive)
                {
                    ++neighborsCount;
                }
            }

            // go down left
            if (cell.PositionY > 0 && cell.PositionX < _grid.Rows - 1)
            {
                if (_grid.GetCell(cell.PositionX + 1, cell.PositionY - 1).IsAlive)
                {
                    ++neighborsCount;
                }
            }

            // go down right
            if (cell.PositionY < _grid.Columns - 1 && cell.PositionX < _grid.Rows - 1)
            {
                if (_grid.GetCell(cell.PositionX + 1, cell.PositionY + 1).IsAlive)
                {
                    ++neighborsCount;
                }
            }

            // go up right
            if (cell.PositionY < _grid.Columns - 1 && cell.PositionX > 0)
            {
                if (_grid.GetCell(cell.PositionX - 1, cell.PositionY + 1).IsAlive)
                {
                    ++neighborsCount;
                }
            }

            cell.LiveNeighboursCount = neighborsCount;
        }