Пример #1
0
        /// <summary>
        /// Initializes a new instance of the Universe class, copying contents from
        /// another Universe.
        /// </summary>
        /// <param name="source">The source universe to copy cells from.</param>
        public Universe(Universe source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(paramName: "source");
            }

            universe = new HashSet<Cell>(source.universe);
        }
Пример #2
0
        /// <summary>
        /// Initializes the game from an integer pattern.
        /// </summary>
        /// <param name="pattern">An integer pattern, where <value>1</value> is a cell and
        /// <value>0</value> is not.</param>
        public override void InitializeFrom(int[][] pattern)
        {
            if (pattern == null)
            {
                throw new ArgumentNullException(paramName: "pattern");
            }

            universe = new Universe(pattern);
        }
Пример #3
0
        /// <summary>
        /// Steps the game one generation into the future.
        /// </summary>
        public override void StepForward()
        {
            var deadCells = new List<Cell>();
            var newCells = new List<Cell>();

            ++generation;
            if (Population > 0)
            {
                var currentState = new Universe(universe); // copy the state of the universe
                List<Cell> cellsToCheck = currentState.AllCells;

                // linq magic, where we check all cells already in the universe
                // and also all of their neighbors. since the speed of light is
                // 1 in game of life, this is sufficient.
                cellsToCheck.AddRange(
                    from cell in currentState.AllCells
                    from neighbor in currentState.ListAllPossibleNeighbors(cell.X, cell.Y)
                    where !cellsToCheck.Contains(neighbor)
                    select neighbor);

                foreach (var cell in cellsToCheck)
                {
                    int numberOfNeighbors = currentState.CountLivingNeighbors(cell.X, cell.Y);
                    bool aliveNow = currentState.HasCell(cell.X, cell.Y);
                    bool aliveNext = Rules.AliveNextGeneration(aliveNow, numberOfNeighbors);

                    if (aliveNow && !aliveNext)
                    {
                        universe.Remove(cell);
                        deadCells.Add(cell);
                    }

                    if (!aliveNow && aliveNext)
                    {
                        universe.Add(cell);
                        newCells.Add(cell);
                    }
                }
            }

            if (GameStepEvent != null)
            {
                var stepInformation = new GameStepEventArgs(deadCells, newCells);
                GameStepEvent(this, stepInformation);
            }
        }
Пример #4
0
 /// <summary>
 /// Initializes a new instance of the LinqGame class.
 /// </summary>
 /// <param name="formatter">The formatter to output state by.</param>
 /// <param name="rules">The rules for running the game.</param>
 public LinqGame(RulesBase rules)
     : base(rules)
 {
     generation = 0;
     universe = new Universe();
 }
Пример #5
0
        public void Constructor_GivenIrregularPattern_ThrowsException()
        {
            // arrange
            var pattern = new int[][]
            {
                new[] { 1, 2 },
                new[] { 2, 3, 5 },
                new[] { 0 }
            };

            // act:ish
            Action action = () => universe = new Universe(pattern);

            // assert
            AssertExtension.Throws<ArgumentException>(action);
        }
Пример #6
0
 public void Setup()
 {
     universe = new Universe();
 }
Пример #7
0
        public void Constructor_GivenZeroWidthPattern_ThrowsException()
        {
            // arrange
            var pattern = new int[][] { };

            // act:ish
            Action action = () => universe = new Universe(pattern);

            // assert
            AssertExtension.Throws<ArgumentException>(action);
        }
Пример #8
0
        public void Constructor_GivenSourceUniverse_PreparesCopy()
        {
            // arrange
            var pattern = new FivePoint();
            var other = new Universe(pattern.GetPattern());

            // act
            var copy = new Universe(other);

            // assert
            Assert.AreEqual(
                expected: 5,
                actual: copy.Population,
                message: "The universe population was not initialized correctly.");

            Assert.IsTrue(
                copy.HasCell(0, 1),
                message: "An expected cell was not found.");

            Assert.IsTrue(
                copy.HasCell(0, 3),
                message: "An expected cell was not found.");

            Assert.IsTrue(
                copy.HasCell(1, 2),
                message: "An expected cell was not found.");

            Assert.IsTrue(
                copy.HasCell(2, 1),
                message: "An expected cell was not found.");

            Assert.IsTrue(
                copy.HasCell(2, 3),
                message: "An expected cell was not found.");
        }
Пример #9
0
        public void Constructor_GivenPattern_PreparesUniverseCorrectly()
        {
            // arrange
            var pattern = new FivePoint();

            // act
            var target = new Universe(pattern.GetPattern());

            // assert
            Assert.AreEqual(
                expected: 5,
                actual: target.Population,
                message: "The universe population was not initialized correctly.");

            Assert.IsTrue(
                target.HasCell(0, 1),
                message: "An expected cell was not found.");

            Assert.IsTrue(
                target.HasCell(0, 3),
                message: "An expected cell was not found.");

            Assert.IsTrue(
                target.HasCell(1, 2),
                message: "An expected cell was not found.");

            Assert.IsTrue(
                target.HasCell(2, 1),
                message: "An expected cell was not found.");

            Assert.IsTrue(
                target.HasCell(2, 3),
                message: "An expected cell was not found.");
        }
Пример #10
0
        public void Constructor_GivenNullSourceUniverse_ThrowsException()
        {
            // act:ish
            Action action = () => universe = new Universe(source: null);

            // assert
            AssertExtension.Throws<ArgumentNullException>(action);
        }