static void Main(string[] args) { var rng = new Random(); var width = 100; var height = 20; var living_rate = 0.25; var bounds = new Bounds(new Vector <int>(0, 0), new Vector <int>(width, height)); var states = Enumerable .Range(0, width * height) .Select(i => rng.NextDouble() <= living_rate ? GameOfLifeState.Alive() : GameOfLifeState.Dead()); var neighborhood = new MooreNeighborhood(bounds); var world = new World <GameOfLifeState>(states, bounds, neighborhood); var rules = new RuleSet <GameOfLifeState>(new BirthRule(world), new DeathRule(world)); world.SetRules(rules); var running = true; while (running) { Console.WriteLine(AsString(world)); world = world.NextStep(); var key = Console.ReadKey(); running = key.KeyChar != 'q'; } }
/// <summary> /// Checks if the resulting position of the move would be inside /// the grid. /// </summary> /// <param name="tile">The tile from which the move would start.</param> /// <param name="direction">The direction of the move.</param> /// <returns>Answer to the question: is move allowed?</returns> private bool GetIsMoveAllowed(Tile tile, MooreNeighborhood direction) { Coord delta = GetMoveDelta(direction); Coord newPos = tile.Pos + delta; if (newPos.Row < 0 || newPos.Column < 0 || newPos.Row >= Tiles.Length || newPos.Column >= Tiles[0].Length) { return(false); } return(true); }
/// <summary> /// From a given direction, return the respective delta move. /// </summary> /// <param name="direction">The direction of the move.</param> /// <returns><see cref="Coord"/> with the delta to perform /// the move.</returns> private Coord GetMoveDelta(MooreNeighborhood direction) { Coord delta; switch (direction) { case MooreNeighborhood.North: delta = new Coord(-1, 0); break; case MooreNeighborhood.NorthEast: delta = new Coord(-1, 1); break; case MooreNeighborhood.East: delta = new Coord(0, 1); break; case MooreNeighborhood.SouthEast: delta = new Coord(1, 1); break; case MooreNeighborhood.South: delta = new Coord(1, 0); break; case MooreNeighborhood.SouthWest: delta = new Coord(1, -1); break; case MooreNeighborhood.West: delta = new Coord(0, -1); break; case MooreNeighborhood.NorthWest: delta = new Coord(-1, -1); break; default: delta = new Coord(0, 0); break; } return(delta); }