Esempio n. 1
0
        static void Main(string[] args)
        {
            int    width          = 16;
            int    height         = 16;
            int    ticks          = -1;
            int    seed           = 4;
            string outputFileName = string.Empty;
            bool   helpCalled     = false;

            Parser.Default.ParseArguments <Options>(args)
            .WithParsed <Options>(o =>
            {
                width          = o.Width;
                height         = o.Height;
                ticks          = o.Ticks;
                seed           = o.Seed;
                outputFileName = o.OutputFileName;
            })
            .WithNotParsed <Options>(o =>
            {
                helpCalled = true;
            });

            if (!helpCalled)
            {
                Console.Clear();

                var gameOfLife = new GameOfLife();

                gameOfLife.InitializeMatrixWithPattern(width, height, (InitPattern)seed, MatrixType.HashSet);

                Console.SetCursorPosition(0, height + 3);
                Console.WriteLine("Press ESC to stop");
                Console.WriteLine("To display help screen run 'GameOfLifeConsole --help' ");

                int generation = 0;
                do
                {
                    while (!Console.KeyAvailable && (ticks < 0 || generation < ticks))
                    {
                        gameOfLife.GetNextGeneration();
                        WriteArrayToScreen(gameOfLife.GameOfLifeMatrix);
                        generation++;
                        Thread.Sleep(200);
                    }

                    if (generation >= ticks)
                    {
                        break;
                    }
                } while (Console.ReadKey(true).Key != ConsoleKey.Escape && (ticks < 0 || generation < ticks));

                if (!string.IsNullOrWhiteSpace(outputFileName))
                {
                    gameOfLife.DumpGridState(outputFileName);
                }

                Console.SetCursorPosition(0, height + 5);
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var gol = new GameOfLife(150, 50);

            //gol.Randomize();
            gol.GosperGliderGun();
            while (true)
            {
                Thread.Sleep(75);
                PrintBoard(gol.GetNextGeneration());
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            Console.CursorVisible = false;

            GameOfLife game = GameOfLife.GetInstance(null, 47, 105, 2, false);


            while (true)
            {
                game.Iterate();
                Console.SetCursorPosition(0, 0);
                Console.WriteLine(game);
                Thread.Sleep(50);
            }
        }
Esempio n. 4
0
        public static void Main()
        {
            Console.Title          = "Game of Life";
            Console.OutputEncoding = Encoding.UTF8;
            Console.CursorVisible  = false;

            Console.WriteLine();

            var fps = 15;

            object[][] title =
            {
                new object[] { 0, 1, 1, 0, 0, 2, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0,   0,   0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1 },
                new object[] { 1, 0, 0, 0, 2, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0,   0,   0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0 },
                new object[] { 1, 0, 1, 0, 2, 2, 2, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 'O', 'F', 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0 },
                new object[] { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0,   0,   0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0 },
                new object[] { 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0,   0,   0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1 }
            };

            // FULL BLOCK char
            const string BLOCK = "█";

            // box drawing chars
            const string CORNER_NW = "┏";
            const string CORNER_NE = "┓";
            const string CORNER_SW = "┗";
            const string CORNER_SE = "┛";
            const char   LINE_VERT = '┃';
            const char   LINE_HORZ = '━';

            foreach (var row in title)
            {
                if (OperatingSystem.IsWindows() && Console.WindowWidth < row.Length * 2)
                {
                    Console.SetWindowSize(row.Length * 2, Console.WindowHeight);
                }

                foreach (var ch in row)
                {
                    if (ch is int)
                    {
                        switch (ch)
                        {
                        case 0:
                            Console.Write("  ");

                            break;

                        case 1:
                            Console.Write(BLOCK + BLOCK);

                            break;

                        case 2:
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write(BLOCK + BLOCK);
                            Console.ResetColor();

                            break;
                        }
                    }
                    else if (ch is char)
                    {
                        Console.Write($"{ch} ");
                    }
                }

                Console.WriteLine();
            }

            var size = input.InputInt32("Enter desired board size", 16, 32);

            var gol = new GameOfLife((ushort)size);

            gol.Randomize();

            var paused = false;
            var sb     = new StringBuilder();

            while (true)
            {
                Console.Clear();
                sb.Clear();

                sb.AppendLine(CORNER_NW + $" Gen. {gol.Generation} ({gol.ActiveCells}/{gol.TotalCells}, {fps} SPS) ".PadBoth(gol.Size.Width * 2, LINE_HORZ) + CORNER_NE);
                for (var y = 0; y < gol.Size.Height; y++)
                {
                    sb.Append(LINE_VERT);
                    for (var x = 0; x < gol.Size.Width; x++)
                    {
                        var idx     = y * gol.Size.Width + x;
                        var nextStr = gol.World[idx] ? BLOCK : " ";
                        sb.Append(nextStr + nextStr);
                    }

                    sb.Append(LINE_VERT);
                    sb.AppendLine();
                }

                var pauseBanner = paused ? " (PAUSED) " : "";
                sb.AppendLine(CORNER_SW + pauseBanner.PadBoth((gol.Size.Width) * 2, LINE_HORZ) + CORNER_SE);
                Console.Write(sb);

                if (Console.KeyAvailable || paused)
                {
                    var key = Console.ReadKey(true).Key;
                    if (key == ConsoleKey.Escape || key == ConsoleKey.Q)
                    {
                        // Q = quit
                        break;                         // Breaks outer while loop
                    }

                    if (key == ConsoleKey.E && paused)
                    {
                        // E = edit cell
                        var cellCoord = input.InputPoint("Enter X/Y coordinates to toggle a cell");
                        gol.ToggleCell(cellCoord);

                        continue;
                    }

                    if (key == ConsoleKey.R)
                    {
                        // R = randomize
                        gol.Randomize();

                        continue;
                    }

                    if (key == ConsoleKey.S)
                    {
                        // S = simulation speed
                        string[] speedPromptArr = { "Enter the new desired number of updates per second", "Actual speed may vary. The Windows console is slow" };
                        var      speedPrompt    = string.Join("\n", speedPromptArr);

                        Console.Clear();
                        fps = input.InputInt32(speedPrompt, 1, 30);

                        continue;
                    }

                    if (key == ConsoleKey.C)
                    {
                        // C = clear
                        gol.Reset();
                    }
                    else if (key == ConsoleKey.Spacebar)
                    {
                        // Pause/Resume simulation
                        paused = !paused;
                    }
                }

                gol.UpdateAsync().Wait();

                // Make sure the app runs at FPS steps per second
                Thread.Sleep(1000 / fps);
            }
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            GameOfLife gameOfLife = new GameOfLife();

            gameOfLife.Run();
        }