예제 #1
0
        private static void Main(string[] args)
        {
            var originalForegroundColor = Console.ForegroundColor;

            Console.Title = String.Format("Game of Lifes Engine v{0}", Assembly.GetExecutingAssembly().GetName().Version);

            var world = new World(Console.WindowWidth - 1, Console.WindowHeight);
            var renderer = new World2StringRenderer();

            world.Randomize(0.1);

            var lastFrameTime = DateTime.Now;
            var lastWindowWidth = Console.WindowWidth;
            var lastWindowHeight = Console.WindowHeight;
            while (true) {
                if (lastWindowWidth != Console.WindowWidth || lastWindowHeight != Console.WindowHeight) {
                    world.Resize(Console.WindowWidth - 1, Console.WindowHeight);
                }

                Console.Clear();
                Console.Write(renderer.Render(world, (char) 30, ' '));

                Console.ForegroundColor = Simulator.Simulate(world) ? originalForegroundColor : ConsoleColor.Red;

                var now = DateTime.Now;
                var diff = DateTime.Now - lastFrameTime;
                if (diff.TotalSeconds < FRAME_TIME) {
                    var sleepTime = (int) (FRAME_TIME * 1000 - diff.TotalMilliseconds);
                    Thread.Sleep(sleepTime);
                }
                lastFrameTime = now;
            }
        }
 public string Render(
     World world,
     int? startX = null, int? startY = null,
     int? width = null, int? height = null)
 {
     return this.Render(world, startX, startY, width, height, '#', ' ');
 }
        public string Render(
            World world,
            int? startX, int? startY,
            int? width, int? height,
            char[] lifeChars,
            char[] deadChars
            )
        {
            var sx = startX ?? 0;
            var sy = startY ?? 0;
            var w = width ?? (world.Width - sx);
            var h = height ?? (world.Height - sy);

            var sw = new StringBuilder();

            for (var y = sy; y < h; y++) {
                for (var x = sx; x < w; x++) {
                    sw.Append(world[x, y] ? lifeChars[this.rand.Next(lifeChars.Length)] : deadChars[this.rand.Next(deadChars.Length)]);
                }
                if (y < h - 1) {
                    sw.AppendLine();
                }
            }

            return sw.ToString();
        }
 public string Render(
     World world,
     int? startX, int? startY,
     int? width, int? height,
     char lifeChar,
     char deadChar
     )
 {
     return Render(world, startX, startY, width, height, new[] {lifeChar}, new[] {deadChar});
 }
예제 #5
0
 public static bool Simulate(World world, SimulationRuleFunction simulationRule)
 {
     var result = new bool[world.Width, world.Height];
     var hasChanged = false;
     for (var x = 0; x < world.Width; x++) {
         for (var y = 0; y < world.Height; y++) {
             var state = simulationRule(world[x, y], world.Neighbours[x, y]);
             result[x, y] = state;
             if (result[x, y] != world[x, y]) {
                 hasChanged = true;
             }
         }
     }
     world.Fill(result);
     return hasChanged;
 }
 public string Render(World world, char[] lifeChars, char[] deadChars)
 {
     return Render(world, null, null, null, null, lifeChars, deadChars);
 }
 public string Render(World world, char lifeChar, char deadChar)
 {
     return Render(world, new[] {lifeChar}, new[] {deadChar});
 }
예제 #8
0
 public static bool Simulate(World world, SimulationRule simulationRule)
 {
     return Simulate(world, simulationRule.RuleFunction);
 }
예제 #9
0
 public static bool Simulate(World world)
 {
     return Simulate(world, StdRules.Conway);
 }
예제 #10
0
 public NeighboursIndexer(World world)
 {
     this._w = world;
 }