Esempio n. 1
0
        public void NumberOfLiveNeighbours_CoordinatesCenter_OneAdjacentCellIsAlive_ReturnsOne()
        {
            var coordinate = new Coordinate {
                X = 1, Y = 1
            };
            var aliveCell = new Cell {
                IsAlive = true
            };
            var deadCell = new Cell {
                IsAlive = false
            };
            var mockBoard = new Mock <IBoard>();

            mockBoard.SetupGet(b => b.Height).Returns(3);
            mockBoard.SetupGet(b => b.Width).Returns(3);
            mockBoard.SetupSequence(b => b.GetCell(It.IsAny <Coordinate>()))
            .Returns(deadCell)
            .Returns(deadCell)
            .Returns(deadCell)
            .Returns(deadCell)
            .Returns(deadCell)
            .Returns(aliveCell)
            .Returns(deadCell)
            .Returns(deadCell)
            .Returns(deadCell);

            var simulation             = new LifeSimulation(mockBoard.Object);
            var numberOfLiveNeighbours = simulation.NumberOfLiveNeighbours(coordinate);

            Assert.AreEqual(1, numberOfLiveNeighbours, "Should return 1 live neighbours when only one adjacent cell is alive.");
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            LifeSimulation sim = new LifeSimulation(10);

            // initialize with a blinker
            sim.ToggleCell(5, 5);
            sim.ToggleCell(5, 6);
            sim.ToggleCell(5, 7);

            sim.BeginGeneration();
            sim.Wait();
            OutputBoard(sim);

            System.Windows.Forms.Timer aTimer = new System.Windows.Forms.Timer();
            aTimer.Interval = 10000;
            aTimer.Tick    += new EventHandler(OnTimedEvent);
            aTimer.Start();

            Console.WriteLine("Press \'q\' to quite");
            while (Console.Read() != 'q')//for (; ; )
            {
                sim.Update();
                sim.Wait();
                //OutputBoard(sim);
                generations = ++generations;
            }

            //sim.Update();
            //sim.Wait();
            //OutputBoard(sim);

            //Console.ReadKey();
        }
 public void Start()
 {
     Simulation = new LifeSimulation(100, 100);
     Logger     = new WebWorldLogger();
     Subscribe(Simulation);
     Simulation.Start(0);
 }
        public void ReturnExpectedState100Times()
        {
            int[,] first =
            {
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
            };

            int[,] second =
            {
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
            };

            int xLength = first.GetLength(1);
            int yLength = first.GetLength(0);

            LifeSimulation lifeSimulation = new LifeSimulation(first, new GridOperator());

            for (int i = 0; i < 100; i++)
            {
                lifeSimulation.NextGeneration();
                if (i % 2 == 0)
                {
                    lifeSimulation.CurrentState.Should().BeEquivalentTo(second);
                }
                else
                {
                    lifeSimulation.CurrentState.Should().BeEquivalentTo(first);
                }
            }
        }
Esempio n. 5
0
        private static void OutputBoard(LifeSimulation sim)
        {
            var line = new String('-', sim.Size);

            Console.WriteLine(line);

            for (int y = 0; y < sim.Size; y++)
            {
                for (int x = 0; x < sim.Size; x++)
                {
                    Console.Write(sim[x, y] ? "1" : "0");
                }

                Console.WriteLine();
            }
        }
Esempio n. 6
0
        public void NumberOfLiveNeighbours_CoordinatesCenter_AllCellsAreAlive_ReturnsEight()
        {
            var coordinate = new Coordinate {
                X = 1, Y = 1
            };
            var cell = new Cell {
                IsAlive = true
            };
            var mockBoard = new Mock <IBoard>();

            mockBoard.SetupGet(b => b.Height).Returns(3);
            mockBoard.SetupGet(b => b.Width).Returns(3);
            mockBoard.Setup(b => b.GetCell(It.IsAny <Coordinate>())).Returns(cell);

            var simulation             = new LifeSimulation(mockBoard.Object);
            var numberOfLiveNeighbours = simulation.NumberOfLiveNeighbours(coordinate);

            Assert.AreEqual(8, numberOfLiveNeighbours, "Should return 8 live neighbours when all cells are live and coordinates are for a cell in the center of the board.");
        }
Esempio n. 7
0
        public void NumberOfLiveNeighbours_CoordinatesAreCornersOfBoard_AllCellsAreAlive_Returns3(int x, int y)
        {
            var coordinate = new Coordinate {
                X = x, Y = y
            };
            var cell = new Cell {
                IsAlive = true
            };
            var mockBoard = new Mock <IBoard>();

            mockBoard.SetupGet(b => b.Height).Returns(3);
            mockBoard.SetupGet(b => b.Width).Returns(3);
            mockBoard.Setup(b => b.GetCell(It.IsAny <Coordinate>())).Returns(cell);

            var simulation             = new LifeSimulation(mockBoard.Object);
            var numberOfLiveNeighbours = simulation.NumberOfLiveNeighbours(coordinate);

            Assert.AreEqual(3, numberOfLiveNeighbours, "Should return 3 live neighbours when all cells are live and coordinates are for corner cell of board.");
        }
Esempio n. 8
0
        public void NumberOfLiveNeighbours_AllCellsAreDead_ReturnsZero(int x, int y)
        {
            var coordinate = new Coordinate {
                X = x, Y = y
            };
            var cell = new Cell {
                IsAlive = false
            };
            var mockBoard = new Mock <IBoard>();

            mockBoard.SetupGet(b => b.Height).Returns(3);
            mockBoard.SetupGet(b => b.Width).Returns(3);
            mockBoard.Setup(b => b.GetCell(It.IsAny <Coordinate>())).Returns(cell);

            var simulation             = new LifeSimulation(mockBoard.Object);
            var numberOfLiveNeighbours = simulation.NumberOfLiveNeighbours(coordinate);

            Assert.AreEqual(0, numberOfLiveNeighbours, "Should return zero live neighbours when all cells are dead.");
        }