Beispiel #1
0
        /// <summary>
        /// Обработчик события изменения грида.
        /// </summary>
        /// <param name="grid">Грид поколения.</param>
        private void SingleGenerationModelChanged(SingleGenerationGrid <bool> grid)
        {
            if (this.generationCanvas == null)
            {
                return;
            }

            Guid lifeId;

            if (grid != null)
            {
                lifeId = grid.LifeId;
            }
            else
            {
                return;
            }

            if (Dispatcher.CheckAccess())
            {
                this.RenderGrid(lifeId);
            }
            else
            {
                Dispatcher.Invoke(new Action(() => this.RenderGrid(lifeId)));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Является ли ячейка живой.
        /// </summary>
        /// <param name="x">Абсцисса точки в гриде.</param>
        /// <param name="y">Ордината точки в гриде.</param>
        /// <param name="grid">Грид.</param>
        /// <returns>Признак того жива ячейка или нет.</returns>
        private static bool IsCellAlive(int x, int y, SingleGenerationGrid <bool> grid)
        {
            if ((x >= 0 && x < grid.Width) && (y >= 0 && y < grid.Height))
            {
                return(grid[x, y]);
            }

            return(false);
        }
Beispiel #3
0
        private void NewUniverseWithThreeLifeInLineCreatedAndAppearedNewGeeneration()
        {
            var universeGrid = new bool[DefaultFieldWidth, DefaultFieldHeight]
            {
                { false, false, false },
                { true, true, true },
                { false, false, false }
            };
            var grid = new SingleGenerationGrid <bool>(universeGrid, Guid.NewGuid());

            this.game = new ClassicGameOfLife(grid);
            this.game.RewindGenerationForward();
        }
Beispiel #4
0
        protected GameTemplate(SingleGenerationGrid <bool> current)
        {
            var timer = new Timer
            {
                Enabled   = false,
                AutoReset = true,
                Interval  = this.СhangeGenerationInterval
            };

            timer.Elapsed += this.OnChangeGenerationTimerElapsed;
            this.changeGenerationTimer = timer;
            this.SetNextGeneration(current);
        }
Beispiel #5
0
 /// <summary>
 /// Задать следующее поколение.
 /// </summary>
 /// <param name="grid">Грид поколения.</param>
 protected void SetNextGeneration(SingleGenerationGrid <bool> grid)
 {
     if (grid == null)
     {
         this.StopGame();
     }
     else
     {
         if (this.currentGeneration != null)
         {
             this.PreviousGenerations.Add(this.currentGeneration);
             this.CurrentGenerationIndex = this.PreviousGenerations.Count;
         }
         else
         {
             this.CurrentGenerationIndex = 0;
         }
         this.CurrentGeneration = grid;
     }
 }
Beispiel #6
0
 /// <summary>
 /// Получить количество "живых соседей".
 /// </summary>
 /// <param name="x">Абсцисса точки в гриде.</param>
 /// <param name="y">Ордината точки в гриде.</param>
 /// <param name="grid">Грид.</param>
 /// <returns>Количество "живых" соседей.</returns>
 private int GetNeighborCellsCount(int x, int y, SingleGenerationGrid <bool> grid)
 {
     return(offsets.Sum(offset => IsCellAlive(x + offset.Dx, y + offset.Dy, grid) ? 1 : 0));
 }
Beispiel #7
0
 public ClassicGameOfLife(SingleGenerationGrid <bool> current) : base(current)
 {
 }
Beispiel #8
0
        /// <summary>
        /// Cохдать вселенную.
        /// </summary>
        private void CreateUniverse(object obj)
        {
            var grid = new SingleGenerationGrid <bool>(new bool[this.defaultFieldWidth, this.defaultFieldHeight], Guid.NewGuid());

            this.Game = new ClassicGameOfLife(grid);
        }