예제 #1
0
        private static void AssertOsscillatorRepeatsInTwoGenerations(HashSet <Cell> initialState, IGenerationStrategy strategyUnderTest)
        {
            var gen1 = strategyUnderTest.AdvanceGeneration(initialState);

            gen1.Should().NotBeEquivalentTo(initialState);

            var gen2 = strategyUnderTest.AdvanceGeneration(gen1);

            gen2.Should().BeEquivalentTo(initialState);
        }
예제 #2
0
        public void AdvanceGenerationDeadCellWithFourLiveNeighborsShouldRemainDead(IGenerationStrategy strategyUnderTest)
        {
            var nextGen = strategyUnderTest.AdvanceGeneration(
                GenerateLivingTestNeighborhood(this.testCell, coreCellLives: false, liveNeighborCount: 4));

            nextGen.Should().NotContain(this.testCell);
        }
예제 #3
0
        public void AdvanceGenerationDeadCellWithThreeLiveNeighborsShouldBecomeAlive(IGenerationStrategy strategyUnderTest)
        {
            var nextGen = strategyUnderTest.AdvanceGeneration(
                GenerateLivingTestNeighborhood(this.testCell, coreCellLives: false, liveNeighborCount: 3));

            nextGen.Should().Contain(this.testCell);
        }
예제 #4
0
        public void AdvanceGenerationLiveCellWithTwoLiveNeighborsShouldSurvive(IGenerationStrategy strategyUnderTest)
        {
            var nextGen = strategyUnderTest.AdvanceGeneration(
                GenerateLivingTestNeighborhood(this.testCell, coreCellLives: true, liveNeighborCount: 2));

            nextGen.Should().Contain(this.testCell);
        }
예제 #5
0
파일: Program.cs 프로젝트: seaf/gameOfLife
        internal static async Task Main(string[] args)
        {
            IGameInputSource gameInputSource = null;

            Parser.Default.ParseArguments <CommandLineOptions>(args)
            .WithParsed(
                (options) =>
            {
                if (options.InputFile != null)
                {
                    gameInputSource = new FileGameInputSource(options.InputFile, CellParser);
                }
                else
                {
                    gameInputSource = new ConsoleGameInputSource(CellParser);
                }
            })
            .WithNotParsed(errors => Environment.Exit(0));

            try
            {
                var liveCells = await gameInputSource.GetInitialGameState();

                if (liveCells.Count == 0)
                {
                    System.Console.WriteLine("No input found! Exiting simulation.");
                    PauseBeforeExit();
                }

                var generation = 1;
                while (true)
                {
                    System.Console.WriteLine($"Generation: {generation++}");

                    foreach (var cell in liveCells)
                    {
                        System.Console.WriteLine(cell);
                    }

                    System.Console.WriteLine(
                        "Hit ENTER to simulate the next generation (or " +
                        $"\"{InputConstants.InputEnd}\" to exit).");

                    var input = System.Console.ReadLine().Trim().ToLowerInvariant();
                    if (input == InputConstants.InputEnd)
                    {
                        break;
                    }

                    liveCells = GenerationStrategy.AdvanceGeneration(liveCells);
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex);
                PauseBeforeExit();
            }
        }
예제 #6
0
        public void AdvanceGenerationBlockStillLifeShouldRemainUnchanged(IGenerationStrategy strategyUnderTest)
        {
            var block = new HashSet <Cell> {
                new Cell(0, 0),
                new Cell(0, 1),
                new Cell(1, 0),
                new Cell(1, 1)
            };

            var nextGen = strategyUnderTest.AdvanceGeneration(block);

            nextGen.Should().BeEquivalentTo(block);
        }
예제 #7
0
        public void AdvanceGenerationTubStillLifeShouldRemainUnchanged(IGenerationStrategy strategyUnderTest)
        {
            var tub = new HashSet <Cell> {
                new Cell(0, 1),
                new Cell(1, 0),
                new Cell(1, 2),
                new Cell(2, 1)
            };

            var nextGen = strategyUnderTest.AdvanceGeneration(tub);

            nextGen.Should().BeEquivalentTo(tub);
        }
예제 #8
0
        public void AdvanceGenerationFromTwoLiveCellsWithOverlappingNeighborhoodsShouldIncludeAllNeighborsWithoutDuplicates(IGenerationStrategy strategyUnderTest)
        {
            MockGameRules.Setup(x => x.ShouldCellLive(It.IsAny <bool>(), It.IsAny <int>())).Returns(true);

            // Neighbors in column 2 overlap
            var cell1 = new Cell(1, 1);
            var cell2 = new Cell(1, 3);

            var nextGen = strategyUnderTest.AdvanceGeneration(new HashSet <Cell> {
                cell1, cell2
            });

            nextGen.Should().BeEquivalentTo(cell1.FindValidNeighbors().Union(cell2.FindValidNeighbors()));
        }
예제 #9
0
        public void AdvanceGenerationFromTwoLiveCellsWithDisparateNeighborhoodsShouldIncludeAllNeighbors(IGenerationStrategy strategyUnderTest)
        {
            MockGameRules.Setup(x => x.ShouldCellLive(It.IsAny <bool>(), It.IsAny <int>())).Returns(true);

            // Not adjacent
            var cell1 = new Cell(10, 20);
            var cell2 = new Cell(50, 70);

            var nextGen = strategyUnderTest.AdvanceGeneration(new HashSet <Cell> {
                cell1, cell2
            });

            nextGen.Should().BeEquivalentTo(new HashSet <Cell>(cell1.FindValidNeighbors().Union(cell2.FindValidNeighbors())));
        }
예제 #10
0
        public void AdvanceGenerationBeehiveStillLifeShouldRemainUnchanged(IGenerationStrategy strategyUnderTest)
        {
            var beehive = new HashSet <Cell> {
                new Cell(0, 1),
                new Cell(0, 2),
                new Cell(1, 0),
                new Cell(1, 3),
                new Cell(2, 1),
                new Cell(2, 2)
            };

            var nextGen = strategyUnderTest.AdvanceGeneration(beehive);

            nextGen.Should().BeEquivalentTo(beehive);
        }
예제 #11
0
 public void AdvanceGenerationFromSingleLivingCellShouldResultInNoLivingCellsWhenNoneShouldLive(IGenerationStrategy strategyUnderTest)
 {
     var nextGen = strategyUnderTest.AdvanceGeneration(new HashSet <Cell> {
         new Cell(0, 0)
     }).Should().BeEmpty();
 }
예제 #12
0
 public void AdvanceGenerationFromNoLivingCellsShouldResultInNoLivingCells(IGenerationStrategy strategyUnderTest)
 {
     strategyUnderTest.AdvanceGeneration(new HashSet <Cell>()).Should().BeEmpty();
 }