Пример #1
0
 private static void EraseCell(Coords gridCoords, CellState cellState)
 {
     if (GridCoordsAreWithinBounds(gridCoords))
     {
         DrawCharacter(gridCoords, ' ');
     }
 }
Пример #2
0
        // Any live cell with fewer than two live neighbours dies, as if caused by under-population.
        // Any live cell with two or three live neighbours lives on to the next generation.
        // Any live cell with more than three live neighbours dies, as if by overcrowding.
        // Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
        internal static bool CurrentlyAliveCellWillStillBeALiveInTheNextGeneration(Grid currentGrid, Coords coords)
        {
            var numLiveNeighbours = GetNumLiveNeighbours(currentGrid, coords);

            // Rules 1, 2 and 3.
            return (numLiveNeighbours == 2 || numLiveNeighbours == 3);
        }
Пример #3
0
        internal static bool CurrentlyDeadCellWillBecomeALiveInTheNextGeneration(Grid currentGrid, Coords coords)
        {
            var numLiveNeighbours = GetNumLiveNeighbours(currentGrid, coords);

            // Rule 4.
            return (numLiveNeighbours == 3);
        }
Пример #4
0
        private bool CellWasDeadInPreviousGrid(Coords coords)
        {
            var cellHasComeAlive = true;

            if (_previousGrid != null)
            {
                cellHasComeAlive = !_previousGrid.IsLiveCellAt(coords);
            }

            return cellHasComeAlive;
        }
Пример #5
0
        private static void DrawCell(Coords gridCoords, CellState cellState)
        {
            if (GridCoordsAreWithinBounds(gridCoords))
            {
                var savedForegroundColor = Console.ForegroundColor;

                Console.ForegroundColor = cellState.IsZombie ? ConsoleColor.Magenta : ConsoleColor.Cyan;
                DrawCharacter(gridCoords, 'X');

                Console.ForegroundColor = savedForegroundColor;
            }
        }
Пример #6
0
 private static void DrawCharacter(Coords gridCoords, char character)
 {
     var consoleCoords = GridCoordsToConsoleCoords(gridCoords);
     Console.SetCursorPosition(consoleCoords.X, consoleCoords.Y);
     Console.Write(character);
 }
 public void SetUp()
 {
     _coords = new Coords(12, 13);
 }
Пример #8
0
 public void MarkZombieCellAt(Coords coords)
 {
     _cells[coords] = CellState.ZombieCellState();
 }
Пример #9
0
 public void MarkWasPreviouslyAliveCellAt(Coords coords)
 {
     _cells[coords] = CellState.WasPreviouslyAliveCellState();
 }
Пример #10
0
 public bool IsLiveCellAt(Coords coords)
 {
     return _cells.ContainsKey(coords) && _cells[coords].IsAlive;
 }
Пример #11
0
 public bool CellWasPreviouslyAliveAt(Coords coords)
 {
     return _cells.ContainsKey(coords) && _cells[coords].WasPreviouslyAlive;
 }
Пример #12
0
 private bool CellIsDeadInCurrentGrid(Coords coords)
 {
     return !_currentGrid.IsLiveCellAt(coords);
 }
Пример #13
0
 public void AddSeedCellAt(Coords coords)
 {
     _currentGrid.MarkLiveCellAt(coords);
 }
Пример #14
0
 private static int GetNumLiveNeighbours(Grid currentGrid, Coords coords)
 {
     var coordsOfNeighbours = coords.Neighbours();
     return coordsOfNeighbours.Count(currentGrid.IsLiveCellAt);
 }
Пример #15
0
 private static bool GridCoordsAreWithinBounds(Coords gridCoords)
 {
     return Math.Abs(gridCoords.X) <= MaxOffsetFromOriginX &&
            Math.Abs(gridCoords.Y) <= MaxOffsetFromOriginY;
 }
Пример #16
0
 public void MarkLiveCellAt(Coords coords)
 {
     _cells[coords] = CellState.AliveCellState();
 }
Пример #17
0
 private static Coords GridCoordsToConsoleCoords(Coords coords)
 {
     return Coords.Create(
         MaxOffsetFromOriginX + coords.X,
         OverallBoardSizeY - MaxOffsetFromOriginY - coords.Y - 1);
 }
Пример #18
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);
            }
        }