Exemplo n.º 1
0
        private static void SeedWithBlinker(Universe universe)
        {
            // http://en.wikipedia.org/wiki/File:Game_of_life_blinker.gif
            // XXX

            universe.AddSeedCellAt(Coords.Create(0, 0));
            universe.AddSeedCellAt(Coords.Create(1, 0));
            universe.AddSeedCellAt(Coords.Create(2, 0));
        }
Exemplo n.º 2
0
 private static void SeedWithGlider(Universe universe)
 {
     // http://en.wikipedia.org/wiki/File:Game_of_life_animated_glider.gif
     // X
     //  XX
     // XX
     universe.AddSeedCellAt(Coords.Create(-5, 5));
     universe.AddSeedCellAt(Coords.Create(-4, 4));
     universe.AddSeedCellAt(Coords.Create(-3, 4));
     universe.AddSeedCellAt(Coords.Create(-5, 3));
     universe.AddSeedCellAt(Coords.Create(-4, 3));
 }
Exemplo n.º 3
0
        private static void SeedWithBeacon(Universe universe)
        {
            // http://en.wikipedia.org/wiki/File:Game_of_life_beacon.gif
            // XX
            // XX
            //   XX
            //   XX

            universe.AddSeedCellAt(Coords.Create(-1, 1));
            universe.AddSeedCellAt(Coords.Create(-2, 1));
            universe.AddSeedCellAt(Coords.Create(-1, 0));
            universe.AddSeedCellAt(Coords.Create(-2, 0));
            universe.AddSeedCellAt(Coords.Create(0, -1));
            universe.AddSeedCellAt(Coords.Create(1, -1));
            universe.AddSeedCellAt(Coords.Create(0, -2));
            universe.AddSeedCellAt(Coords.Create(1, -2));
        }
Exemplo n.º 4
0
        public void TheExpectedPatternShouldBeGeneratedWhenTheSeedIsBeacon()
        {
            // http://en.wikipedia.org/wiki/File:Game_of_life_beacon.gif

            // XX
            // XX
            //   XX
            //   XX
            var setOfCoords1 = new[]
                                   {
                                       Coords.Create(-1, 1),
                                       Coords.Create(-2, 1),
                                       Coords.Create(-1, 0),
                                       Coords.Create(-2, 0),
                                       Coords.Create(0, -1),
                                       Coords.Create(1, -1),
                                       Coords.Create(0, -2),
                                       Coords.Create(1, -2)
                                   };

            // XX
            // X
            //    X
            //   XX
            var setOfCoords2 = new[]
                                  {
                                       Coords.Create(-1, 1),
                                       Coords.Create(-2, 1),
                                       Coords.Create(-2, 0),
                                       Coords.Create(1, -1),
                                       Coords.Create(0, -2),
                                       Coords.Create(1, -2)
                                  };

            var universe = new Universe();
            MarkLiveCells(universe, setOfCoords1);
            AssertThatGridContainsExpectedLiveCellsAfterTick(universe, setOfCoords2);
            AssertThatGridContainsExpectedLiveCellsAfterTick(universe, setOfCoords1);
            AssertThatGridContainsExpectedLiveCellsAfterTick(universe, setOfCoords2);
            AssertThatGridContainsExpectedLiveCellsAfterTick(universe, setOfCoords1);
            AssertThatGridContainsExpectedLiveCellsAfterTick(universe, setOfCoords2);
            AssertThatGridContainsExpectedLiveCellsAfterTick(universe, setOfCoords1);
            AssertThatGridContainsExpectedLiveCellsAfterTick(universe, setOfCoords2);
            AssertThatGridContainsExpectedLiveCellsAfterTick(universe, setOfCoords1);
        }
Exemplo n.º 5
0
        public static void SeedTheUniverse(Universe universe, int seedChoice)
        {
            switch (seedChoice)
            {
                case 1:
                    SeedWithBlinker(universe);
                    break;

                case 2:
                    SeedWithToad(universe);
                    break;

                case 3:
                    SeedWithBeacon(universe);
                    break;

                case 4:
                    SeedWithPulsar(universe);
                    break;

                case 5:
                    SeedWithGlider(universe);
                    break;

                case 6:
                    SeedWithLightWeightSpaceShip(universe);
                    break;

                case 7:
                    SeedWithRPentomino(universe);
                    break;

                case 8:
                    SeedWithGun(universe);
                    break;

                default:
                    SeedWithPulsar(universe);
                    break;
            }
        }
Exemplo n.º 6
0
        private static bool SomeLiveCellsAreWithinBounds(Universe universe)
        {
            var result = false;

            universe.IterateLiveCells((coords, cellState) =>
                                          {
                                              if (GridCoordsAreWithinBounds(coords))
                                              {
                                                  result = true;
                                              }
                                          });

            return result;
        }
Exemplo n.º 7
0
        private static ConsoleCtrlHandler.RunLoopResult RunLoop(Universe universe, int tickSleepInterval)
        {
            if (!DrawBoundedUniverse(universe))
            {
                return ConsoleCtrlHandler.RunLoopResult.Quit;
            }

            Thread.Sleep(tickSleepInterval);
            universe.Tick();

            return ConsoleCtrlHandler.RunLoopResult.KeepGoing;
        }
Exemplo n.º 8
0
 public UnderpopulationRule(Universe grid)
 {
     _grid = grid;
 }
Exemplo n.º 9
0
        private static void SeedWithGun(Universe universe)
        {
            // http://en.wikipedia.org/wiki/File:Game_of_life_glider_gun.svg
            const int originX = -45;
            const int originY = 25;

            universe.AddSeedCellAt(Coords.Create(originX + 0, originY + 0));
            universe.AddSeedCellAt(Coords.Create(originX + 0, originY + 1));
            universe.AddSeedCellAt(Coords.Create(originX + 1, originY + 0));
            universe.AddSeedCellAt(Coords.Create(originX + 1, originY + 1));

            universe.AddSeedCellAt(Coords.Create(originX + 10, originY + 0));
            universe.AddSeedCellAt(Coords.Create(originX + 10, originY + 1));
            universe.AddSeedCellAt(Coords.Create(originX + 10, originY + -1));
            universe.AddSeedCellAt(Coords.Create(originX + 11, originY + 2));
            universe.AddSeedCellAt(Coords.Create(originX + 11, originY + -2));
            universe.AddSeedCellAt(Coords.Create(originX + 12, originY + 3));
            universe.AddSeedCellAt(Coords.Create(originX + 13, originY + 3));
            universe.AddSeedCellAt(Coords.Create(originX + 12, originY + -3));
            universe.AddSeedCellAt(Coords.Create(originX + 13, originY + -3));
            universe.AddSeedCellAt(Coords.Create(originX + 14, originY + 0));
            universe.AddSeedCellAt(Coords.Create(originX + 15, originY + 2));
            universe.AddSeedCellAt(Coords.Create(originX + 15, originY + -2));
            universe.AddSeedCellAt(Coords.Create(originX + 16, originY + 0));
            universe.AddSeedCellAt(Coords.Create(originX + 16, originY + 1));
            universe.AddSeedCellAt(Coords.Create(originX + 16, originY + -1));
            universe.AddSeedCellAt(Coords.Create(originX + 17, originY + 0));

            universe.AddSeedCellAt(Coords.Create(originX + 20, originY + 1));
            universe.AddSeedCellAt(Coords.Create(originX + 20, originY + 2));
            universe.AddSeedCellAt(Coords.Create(originX + 20, originY + 3));
            universe.AddSeedCellAt(Coords.Create(originX + 21, originY + 1));
            universe.AddSeedCellAt(Coords.Create(originX + 21, originY + 2));
            universe.AddSeedCellAt(Coords.Create(originX + 21, originY + 3));
            universe.AddSeedCellAt(Coords.Create(originX + 22, originY + 4));
            universe.AddSeedCellAt(Coords.Create(originX + 22, originY + 0));
            universe.AddSeedCellAt(Coords.Create(originX + 24, originY + 4));
            universe.AddSeedCellAt(Coords.Create(originX + 24, originY + 5));
            universe.AddSeedCellAt(Coords.Create(originX + 24, originY + 0));
            universe.AddSeedCellAt(Coords.Create(originX + 24, originY + -1));

            universe.AddSeedCellAt(Coords.Create(originX + 34, originY + 2));
            universe.AddSeedCellAt(Coords.Create(originX + 34, originY + 3));
            universe.AddSeedCellAt(Coords.Create(originX + 35, originY + 2));
            universe.AddSeedCellAt(Coords.Create(originX + 35, originY + 3));
        }
Exemplo n.º 10
0
 private void MarkLiveCells(Universe universe, params Coords[] setOfcoords)
 {
     foreach (var coords in setOfcoords)
     {
         universe.AddSeedCellAt(coords);
     }
 }
Exemplo n.º 11
0
        private static void SeedWithToad(Universe universe)
        {
            // http://en.wikipedia.org/wiki/File:Game_of_life_toad.gif
            //  XXX
            // XXX

            universe.AddSeedCellAt(Coords.Create(0, 0));
            universe.AddSeedCellAt(Coords.Create(1, 0));
            universe.AddSeedCellAt(Coords.Create(2, 0));
            universe.AddSeedCellAt(Coords.Create(1, 1));
            universe.AddSeedCellAt(Coords.Create(2, 1));
            universe.AddSeedCellAt(Coords.Create(3, 1));
        }
Exemplo n.º 12
0
 private static void SeedWithRPentomino(Universe universe)
 {
     // http://en.wikipedia.org/wiki/File:Game_of_life_fpento.svg
     //  XX
     // XX
     //  X
     universe.AddSeedCellAt(Coords.Create(15, 10));
     universe.AddSeedCellAt(Coords.Create(15, 11));
     universe.AddSeedCellAt(Coords.Create(15, 12));
     universe.AddSeedCellAt(Coords.Create(16, 12));
     universe.AddSeedCellAt(Coords.Create(14, 11));
 }
Exemplo n.º 13
0
        private static void SeedWithPulsar(Universe universe)
        {
            // http://en.wikipedia.org/wiki/File:Game_of_life_pulsar.gif
            //   XXX   XXX
            //
            // x    x x    x
            // x    x x    x
            // x    x x    x
            //   XXX   XXX
            //
            //   XXX   XXX
            // x    x x    x
            // x    x x    x
            // x    x x    x
            //
            //   XXX   XXX

            universe.AddSeedCellAt(Coords.Create(2, 1));
            universe.AddSeedCellAt(Coords.Create(3, 1));
            universe.AddSeedCellAt(Coords.Create(4, 1));
            universe.AddSeedCellAt(Coords.Create(2, 6));
            universe.AddSeedCellAt(Coords.Create(3, 6));
            universe.AddSeedCellAt(Coords.Create(4, 6));
            universe.AddSeedCellAt(Coords.Create(1, 2));
            universe.AddSeedCellAt(Coords.Create(1, 3));
            universe.AddSeedCellAt(Coords.Create(1, 4));
            universe.AddSeedCellAt(Coords.Create(6, 2));
            universe.AddSeedCellAt(Coords.Create(6, 3));
            universe.AddSeedCellAt(Coords.Create(6, 4));

            universe.AddSeedCellAt(Coords.Create(-2, 1));
            universe.AddSeedCellAt(Coords.Create(-3, 1));
            universe.AddSeedCellAt(Coords.Create(-4, 1));
            universe.AddSeedCellAt(Coords.Create(-2, 6));
            universe.AddSeedCellAt(Coords.Create(-3, 6));
            universe.AddSeedCellAt(Coords.Create(-4, 6));
            universe.AddSeedCellAt(Coords.Create(-1, 2));
            universe.AddSeedCellAt(Coords.Create(-1, 3));
            universe.AddSeedCellAt(Coords.Create(-1, 4));
            universe.AddSeedCellAt(Coords.Create(-6, 2));
            universe.AddSeedCellAt(Coords.Create(-6, 3));
            universe.AddSeedCellAt(Coords.Create(-6, 4));

            universe.AddSeedCellAt(Coords.Create(2, -1));
            universe.AddSeedCellAt(Coords.Create(3, -1));
            universe.AddSeedCellAt(Coords.Create(4, -1));
            universe.AddSeedCellAt(Coords.Create(2, -6));
            universe.AddSeedCellAt(Coords.Create(3, -6));
            universe.AddSeedCellAt(Coords.Create(4, -6));
            universe.AddSeedCellAt(Coords.Create(1, -2));
            universe.AddSeedCellAt(Coords.Create(1, -3));
            universe.AddSeedCellAt(Coords.Create(1, -4));
            universe.AddSeedCellAt(Coords.Create(6, -2));
            universe.AddSeedCellAt(Coords.Create(6, -3));
            universe.AddSeedCellAt(Coords.Create(6, -4));

            universe.AddSeedCellAt(Coords.Create(-2, -1));
            universe.AddSeedCellAt(Coords.Create(-3, -1));
            universe.AddSeedCellAt(Coords.Create(-4, -1));
            universe.AddSeedCellAt(Coords.Create(-2, -6));
            universe.AddSeedCellAt(Coords.Create(-3, -6));
            universe.AddSeedCellAt(Coords.Create(-4, -6));
            universe.AddSeedCellAt(Coords.Create(-1, -2));
            universe.AddSeedCellAt(Coords.Create(-1, -3));
            universe.AddSeedCellAt(Coords.Create(-1, -4));
            universe.AddSeedCellAt(Coords.Create(-6, -2));
            universe.AddSeedCellAt(Coords.Create(-6, -3));
            universe.AddSeedCellAt(Coords.Create(-6, -4));
        }
Exemplo n.º 14
0
 private static void SeedWithLightWeightSpaceShip(Universe universe)
 {
     // http://en.wikipedia.org/wiki/File:Game_of_life_animated_LWSS.gif
     //   XX
     // XX XX
     // XXXX
     //  XX
     universe.AddSeedCellAt(Coords.Create(-7, 3));
     universe.AddSeedCellAt(Coords.Create(-6, 3));
     universe.AddSeedCellAt(Coords.Create(-9, 2));
     universe.AddSeedCellAt(Coords.Create(-8, 2));
     universe.AddSeedCellAt(Coords.Create(-6, 2));
     universe.AddSeedCellAt(Coords.Create(-5, 2));
     universe.AddSeedCellAt(Coords.Create(-9, 1));
     universe.AddSeedCellAt(Coords.Create(-8, 1));
     universe.AddSeedCellAt(Coords.Create(-7, 1));
     universe.AddSeedCellAt(Coords.Create(-6, 1));
     universe.AddSeedCellAt(Coords.Create(-8, 0));
     universe.AddSeedCellAt(Coords.Create(-7, 0));
 }
Exemplo n.º 15
0
 private static bool DrawBoundedUniverse(Universe universe)
 {
     universe.IterateCellsThatHaveComeAlive(DrawCell);
     universe.IterateCellsThatHaveDied(EraseCell);
     return SomeLiveCellsAreWithinBounds(universe);
 }
Exemplo n.º 16
0
        private static void Main(string[] args)
        {
            var seedChoice = DefaultSeedChoice;
            var tickSleepInterval = DefaultTickSleepInterval;

            if (args.Length >= 1)
            {
                if (!int.TryParse(args[0], out seedChoice))
                {
                    seedChoice = DefaultSeedChoice;
                }
            }

            if (args.Length == 2)
            {
                if (!int.TryParse(args[1], out tickSleepInterval))
                {
                    tickSleepInterval = DefaultTickSleepInterval;
                }
            }

            var universe = new Universe();
            SeedPatterns.SeedTheUniverse(universe, seedChoice);

            InitialiseConsole();

            ConsoleCtrlHandler.RunUntilCtrlC(() => RunLoop(universe, tickSleepInterval));

            RestoreConsole();
        }
Exemplo n.º 17
0
        private void AssertThatGridContainsExpectedLiveCellsAfterTick(Universe universe, Coords[] setOfExpectedCoords)
        {
            universe.Tick();

            var coordsOfLiveCells = new List<Coords>();
            universe.IterateLiveCells((coords, cellState) => coordsOfLiveCells.Add(coords));

            Assert.That(setOfExpectedCoords, Is.Unique);
            Assert.That(coordsOfLiveCells, Is.Unique);

            Assert.That(coordsOfLiveCells.Count(), Is.EqualTo(setOfExpectedCoords.Count()), "Found the wrong number of live cells");

            foreach (var expectedCoords in setOfExpectedCoords)
            {
                var match = coordsOfLiveCells.FirstOrDefault(coords => coords.Equals(expectedCoords));
                var message = string.Format("Expected to find a live cell at ({0}, {1})", expectedCoords.X, expectedCoords.Y);
                Assert.That(match, Is.Not.Null, message);
            }
        }