コード例 #1
0
 public void Rule1()
 {
     GameOfLife game = new GameOfLife(3, 3);
     game.grid[1, 1] = true;
     game.Tick();
     Assert.IsFalse(game.grid[1, 1]);
 }
コード例 #2
0
 public void TickTestLoafFormation()
 {
     GameOfLife ATestGrid = new GameOfLife(6, 6);
     ATestGrid[1, 2] = true;
     ATestGrid[1, 3] = true;
     ATestGrid[2, 1] = true;
     ATestGrid[2, 4] = true;
     ATestGrid[3, 2] = true;
     ATestGrid[3, 4] = true;
     ATestGrid[4, 3] = true;
     GameOfLife ATestGridToTick = new GameOfLife(6, 6);
     ATestGridToTick[1, 2] = true;
     ATestGridToTick[1, 3] = true;
     ATestGridToTick[2, 1] = true;
     ATestGridToTick[2, 4] = true;
     ATestGridToTick[3, 2] = true;
     ATestGridToTick[3, 4] = true;
     ATestGridToTick[4, 3] = true;
     ATestGridToTick.Tick();
     CollectionAssert.AreEqual(ATestGrid.ToList()[0], ATestGridToTick.ToList()[0]);
     CollectionAssert.AreEqual(ATestGrid.ToList()[1], ATestGridToTick.ToList()[1]);
     CollectionAssert.AreEqual(ATestGrid.ToList()[2], ATestGridToTick.ToList()[2]);
     CollectionAssert.AreEqual(ATestGrid.ToList()[3], ATestGridToTick.ToList()[3]);
     CollectionAssert.AreEqual(ATestGrid.ToList()[4], ATestGridToTick.ToList()[4]);
     CollectionAssert.AreEqual(ATestGrid.ToList()[5], ATestGridToTick.ToList()[5]);
 }
コード例 #3
0
 public void Blinker()
 {
     GameOfLife game = new GameOfLife(5, 5);
     game.grid[1, 2] = true;
     game.grid[2, 2] = true;
     game.grid[3, 2] = true;
     game.Tick();
     Assert.IsFalse(game.grid[1, 2]);
     Assert.IsFalse(game.grid[3, 2]);
     Assert.IsTrue(game.grid[2, 2]);
 }
コード例 #4
0
        public void When_Game_Ticks_State_Changes()
        {
            var initialBoard = new int[10, 10];

            initialBoard[5, 5] = 1;

            var game = new GameOfLife(initialBoard);

            var newBoard = game.Tick();

            Assert.Equal(0, newBoard[5, 5]);
        }
コード例 #5
0
        public void When_Cell_Has_Two_Living_Neighbour_Cell_Stays_Alive()
        {
            var initialBoard = new int[10, 10];

            initialBoard[5, 5] = 1;
            initialBoard[5, 6] = 1;
            initialBoard[6, 6] = 1;

            var game     = new GameOfLife(initialBoard);
            var newBoard = game.Tick();

            Assert.Equal(1, newBoard[5, 5]);
            Assert.Equal(1, newBoard[5, 6]);
            Assert.Equal(1, newBoard[6, 6]);
            Assert.Equal(3, game.CountAlive());
        }
コード例 #6
0
 public void TickTestBlinkerFormation()
 {
     GameOfLife ATestGrid = new GameOfLife(5, 5);
     ATestGrid[1, 2] = true;
     ATestGrid[2, 2] = true;
     ATestGrid[3, 2] = true;
     GameOfLife ATestGridToTick = new GameOfLife(5, 5);
     ATestGridToTick[2, 1] = true;
     ATestGridToTick[2, 2] = true;
     ATestGridToTick[2, 3] = true;
     ATestGridToTick.Tick();
     CollectionAssert.AreEqual(ATestGrid.ToList()[0], ATestGridToTick.ToList()[0]);
     CollectionAssert.AreEqual(ATestGrid.ToList()[1], ATestGridToTick.ToList()[1]);
     CollectionAssert.AreEqual(ATestGrid.ToList()[2], ATestGridToTick.ToList()[2]);
     CollectionAssert.AreEqual(ATestGrid.ToList()[3], ATestGridToTick.ToList()[3]);
     CollectionAssert.AreEqual(ATestGrid.ToList()[4], ATestGridToTick.ToList()[4]);
 }
コード例 #7
0
 public void Rule1OnLargerBoard()
 {
     GameOfLife game = new GameOfLife(8, 8);
     game.grid[1, 1] = true;
     game.grid[5, 5] = true;
     game.Tick();
     Assert.IsFalse(game.grid[1, 1]);
     Assert.IsFalse(game.grid[5, 5]);
 }
コード例 #8
0
 public void Toad()
 {
     GameOfLife game = new GameOfLife(6, 6);
     game.grid[2, 2] = true;
     game.grid[2, 3] = true;
     game.grid[2, 4] = true;
     game.grid[3, 1] = true;
     game.grid[3, 2] = true;
     game.grid[3, 3] = true;
     game.Tick();
     Assert.IsFalse(game.grid[2, 2]);
     Assert.IsFalse(game.grid[2, 3]);
     Assert.IsFalse(game.grid[3, 2]);
     Assert.IsFalse(game.grid[3, 3]);
     Assert.IsTrue(game.grid[1, 3]);
     Assert.IsTrue(game.grid[2, 1]);
     Assert.IsTrue(game.grid[2, 4]);
     Assert.IsTrue(game.grid[3, 1]);
     Assert.IsTrue(game.grid[3, 4]);
     Assert.IsTrue(game.grid[4, 2]);
 }
コード例 #9
0
 public void TickTestRuleFourDeadCellWithThreeLiveNeighbors()
 {
     GameOfLife ATestGridToTick = new GameOfLife(5, 3);
     ATestGridToTick[2, 1] = true;
     ATestGridToTick[3, 1] = false;
     ATestGridToTick[4, 0] = true;
     ATestGridToTick[4, 1] = true;
     Assert.AreEqual(false, ATestGridToTick[3, 1]);
     ATestGridToTick.Tick();
     Assert.AreEqual(true, ATestGridToTick[3, 1]);
 }
コード例 #10
0
 public void TickTestThreeStepFormationPart2()
 {
     GameOfLife ATestGridTwoTick = new GameOfLife(5, 4);
     GameOfLife ATestGridToTick = new GameOfLife(5, 4);
     ATestGridToTick[1, 2] = true;
     ATestGridToTick[2, 1] = true;
     ATestGridToTick[3, 1] = true;
     ATestGridToTick.Tick();
     ATestGridToTick.Tick();
     CollectionAssert.AreEqual(ATestGridTwoTick.ToList()[0], ATestGridToTick.ToList()[0]);
     CollectionAssert.AreEqual(ATestGridTwoTick.ToList()[1], ATestGridToTick.ToList()[1]);
     CollectionAssert.AreEqual(ATestGridTwoTick.ToList()[2], ATestGridToTick.ToList()[2]);
     CollectionAssert.AreEqual(ATestGridTwoTick.ToList()[3], ATestGridToTick.ToList()[3]);
     CollectionAssert.AreEqual(ATestGridTwoTick.ToList()[4], ATestGridToTick.ToList()[4]);
 }
コード例 #11
0
 public void TickTestRuleTwoTwoLiveNeighbors()
 {
     GameOfLife ATestGridToTick = new GameOfLife(5, 3);
     ATestGridToTick[2, 1] = true;
     ATestGridToTick[3, 1] = true;
     ATestGridToTick[4, 0] = true;
     ATestGridToTick.Tick();
     Assert.AreEqual(true, ATestGridToTick[3, 1]);
 }
コード例 #12
0
 public void TickTestRuleOneZeroLiveNeighbors()
 {
     GameOfLife ATestGridToTick = new GameOfLife(4, 4);
     ATestGridToTick[1, 1] = true;
     ATestGridToTick.Tick();
     Assert.AreEqual(false, ATestGridToTick[1, 1]);
 }
コード例 #13
0
        public static void Main()
        {
            int boardSize, numberOfGenerations;

            Console.WriteLine("!!! Welcome to Conway's Game of Life !!!\n");

            // Get the board size
            Console.WriteLine("Please enter a number for the boardsize. (It will determine the board size, e.g. 5 generates a 5x5 board):");
            var input = Console.ReadLine();

            while (!int.TryParse(input, out boardSize))
            {
                Console.WriteLine("Invalid input. Please enter a number.");
                input = Console.ReadLine();
            }

            // Get the number of generations
            Console.WriteLine("Please enter the number of generations for the simulation:");
            input = Console.ReadLine();
            while (!int.TryParse(input, out numberOfGenerations))
            {
                Console.WriteLine("Invalid input. Please enter a number.");
                input = Console.ReadLine();
            }

            // Pad the board with dead cells so that we don't try iterate out of bounds
            var paddedBoardSize = boardSize + 2;

            // Initialise the board and set the initial state
            var gameOfLife = new GameOfLife(paddedBoardSize, numberOfGenerations);

            gameOfLife.RandomiseCellsToInitialiseBoard();

            // Display the initial state of the board
            Console.WriteLine("\nInitial State:");
            gameOfLife.PrintBoard();

            // Run and display each tick (generation) of the simulation
            var generation = 0;

            while (generation < gameOfLife.NumberOfGenerations)
            {
                // The simulation will end earlier than the specified number of generations provided if all the cells are dead.
                if (gameOfLife.CountLivingCellsOnBoard() == 0)
                {
                    Console.WriteLine("The simulation has ended because all cells have died.");
                    break;
                }
                else
                {
                    gameOfLife.Tick();
                    Console.WriteLine($"\nGeneration: {generation + 1}");
                    gameOfLife.PrintBoard();
                    generation++;
                    Thread.Sleep(1000);
                }
            }

            Console.WriteLine("\nPress any key to quit.");
            Console.ReadKey();
        }