Esempio n. 1
0
        /// <summary>
        /// Generates the next generation matrix
        /// Any live cell with two or three live neighbors survives.
        /// Any dead cell with three live neighbors becomes a live cell.
        /// All other live cells die in the next generation.Similarly, all other dead cells stay dead./// </summary>
        /// <param name="matrix">matrix with the state of the current generation</param>
        /// <returns>matrix with the state of the next generation</returns>
        public void GetNextGeneration()
        {
            var nextGeneration = CreateGameOfLifeMatrix(this.GameOfLifeMatrix.Width, this.GameOfLifeMatrix.Height, MatrixType.HashSet);

            for (int y = 0; y < this.GameOfLifeMatrix.Height; y++)
            {
                for (int x = 0; x < this.GameOfLifeMatrix.Width; x++)
                {
                    int livingNeighbors = this.GameOfLifeMatrix.CountLivingNeighbors(x, y);



                    if (this.GameOfLifeMatrix.IsCellAlive(x, y) && livingNeighbors == 2 || this.GameOfLifeMatrix.IsCellAlive(x, y) && livingNeighbors == 3)
                    {
                        nextGeneration.SetCellState(x, y, true);
                    }
                    else
                    {
                        if (!this.GameOfLifeMatrix.IsCellAlive(x, y) && livingNeighbors == 3)
                        {
                            nextGeneration.SetCellState(x, y, true);
                        }
                        else
                        {
                            nextGeneration.SetCellState(x, y, false);
                        }
                    }
                }
            }

            this.GameOfLifeMatrix = nextGeneration;
        }
Esempio n. 2
0
        private static void CheckForLivingNeighbors(IGameOfLifeMatrix matrix)
        {
            Assert.AreEqual(3, matrix.CountLivingNeighbors(0, 0));
            Assert.AreEqual(3, matrix.CountLivingNeighbors(4, 3));
            Assert.AreEqual(3, matrix.CountLivingNeighbors(0, 3));
            Assert.AreEqual(3, matrix.CountLivingNeighbors(4, 0));

            for (int i = 1; i < 4; i++)
            {
                Assert.AreEqual(5, matrix.CountLivingNeighbors(i, 0));
                Assert.AreEqual(5, matrix.CountLivingNeighbors(i, 3));
            }

            for (int i = 1; i < 3; i++)
            {
                Assert.AreEqual(5, matrix.CountLivingNeighbors(0, i));
                Assert.AreEqual(5, matrix.CountLivingNeighbors(4, i));
            }

            for (int y = 1; y < 3; y++)
            {
                for (int x = 1; x < 4; x++)
                {
                    Assert.AreEqual(8, matrix.CountLivingNeighbors(x, y));
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes the matrix with pattern data or with random data
        /// </summary>
        /// <param name="destination">Destination matrix</param>
        /// <param name="pattern">Pattern name</param>
        /// <param name="matrixType">The matrix type that shoud be used </param>
        public void InitializeMatrixWithPattern(int width, int height, InitPattern pattern, MatrixType matrixType = MatrixType.MultiArray)
        {
            this.GameOfLifeMatrix = CreateGameOfLifeMatrix(width, height, matrixType);

            switch (pattern)
            {
            case InitPattern.Beacon:
                InitializeGameOfLife(this.GameOfLifeMatrix, beacon);
                break;

            case InitPattern.Blinker:
                InitializeGameOfLife(this.GameOfLifeMatrix, blinker);
                break;

            case InitPattern.Toad:
                InitializeGameOfLife(this.GameOfLifeMatrix, toad);
                break;

            case InitPattern.Glider:
                InitializeGameOfLife(this.GameOfLifeMatrix, glider);
                break;

            default:
                InitializeMatrixWithRandomValues();
                break;
            }
        }
Esempio n. 4
0
 internal static void InitializeGameOfLife(IGameOfLifeMatrix matrix, int[,] pattern)
 {
     for (int y = 0; y < pattern.GetLength(0); y++)
     {
         for (int x = 0; x < pattern.GetLength(1); x++)
         {
             matrix.SetCellState(x, y, pattern[y, x] == 1);
         }
     }
 }
Esempio n. 5
0
        private static void WriteArrayToScreen(IGameOfLifeMatrix matrix)
        {
            for (int y = 0; y < matrix.Height; y++)

            {
                for (int x = 0; x < matrix.Width; x++)
                {
                    Console.SetCursorPosition(x, y);


                    if (matrix.IsCellAlive(x, y))
                    {
                        Console.Write("█");
                    }
                    else
                    {
                        Console.Write(" ");
                    }
                }
            }
        }