private static void EraseCell(Coords gridCoords, CellState cellState) { if (GridCoordsAreWithinBounds(gridCoords)) { DrawCharacter(gridCoords, ' '); } }
// 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); }
internal static bool CurrentlyDeadCellWillBecomeALiveInTheNextGeneration(Grid currentGrid, Coords coords) { var numLiveNeighbours = GetNumLiveNeighbours(currentGrid, coords); // Rule 4. return (numLiveNeighbours == 3); }
private bool CellWasDeadInPreviousGrid(Coords coords) { var cellHasComeAlive = true; if (_previousGrid != null) { cellHasComeAlive = !_previousGrid.IsLiveCellAt(coords); } return cellHasComeAlive; }
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; } }
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); }
public void MarkZombieCellAt(Coords coords) { _cells[coords] = CellState.ZombieCellState(); }
public void MarkWasPreviouslyAliveCellAt(Coords coords) { _cells[coords] = CellState.WasPreviouslyAliveCellState(); }
public bool IsLiveCellAt(Coords coords) { return _cells.ContainsKey(coords) && _cells[coords].IsAlive; }
public bool CellWasPreviouslyAliveAt(Coords coords) { return _cells.ContainsKey(coords) && _cells[coords].WasPreviouslyAlive; }
private bool CellIsDeadInCurrentGrid(Coords coords) { return !_currentGrid.IsLiveCellAt(coords); }
public void AddSeedCellAt(Coords coords) { _currentGrid.MarkLiveCellAt(coords); }
private static int GetNumLiveNeighbours(Grid currentGrid, Coords coords) { var coordsOfNeighbours = coords.Neighbours(); return coordsOfNeighbours.Count(currentGrid.IsLiveCellAt); }
private static bool GridCoordsAreWithinBounds(Coords gridCoords) { return Math.Abs(gridCoords.X) <= MaxOffsetFromOriginX && Math.Abs(gridCoords.Y) <= MaxOffsetFromOriginY; }
public void MarkLiveCellAt(Coords coords) { _cells[coords] = CellState.AliveCellState(); }
private static Coords GridCoordsToConsoleCoords(Coords coords) { return Coords.Create( MaxOffsetFromOriginX + coords.X, OverallBoardSizeY - MaxOffsetFromOriginY - coords.Y - 1); }
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); } }