Beispiel #1
0
        /// <summary>
        /// Read the size and initailize games with randomly dead or alive cells.
        /// </summary>
        /// <returns>New game states of the user specified size and quantity.</returns>
        private void CreateGame()
        {
            // Read input to start the game
            int rows    = Ask("Enter the number of rows (max: 45000): ");
            int columns = Ask("Enter the number of columns (max: 45000): ");
            int count   = AskGamesQuantity("Enter the number of games you want to generate (max: 1000): ");

            // Initailize games with randomly dead or alive cells
            for (int i = 0; i < count; i++)
            {
                Game game = new Game(rows, columns);
                game.Randomize();
                games.Add(game);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Ask user for game paramters and initialize application with new games.
        /// Games will have randomly assigned dead or alive cells.
        /// </summary>
        private void CreateGame()
        {
            // Read input to start the game
            int rows    = view.AskNumber("Enter the number of rows", 1, 20);
            int columns = view.AskNumber("Enter the number of columns", 1, 20);

            activeCount = view.AskNumber("Enter the number of games you want to generate", 1, 1000);
            AskDisplayGames(activeCount);

            // Initailize games with randomly dead or alive cells
            for (int i = 0; i < activeCount; i++)
            {
                Game game = new Game(rows, columns);
                game.Randomize(random);
                games.Add(game);
                totalAlive += game.CountAlive;
            }
        }
Beispiel #3
0
        private void UpdateLoop()
        {
            if (!game.paused)
            {
                game.Iterate();
            }

            var focused = false;

            Invoke((MethodInvoker)(() => { focused = Focused; }));
            if (focused)
            {
                if (Keyboard.IsKeyDown(Keyboard.Key.D))
                {
                    game.SetBorderType(Grid.BorderType.DeadBorders);
                    canvas.DrawBorders(colors["gray 24"]);
                }

                if (Keyboard.IsKeyDown(Keyboard.Key.L))
                {
                    game.SetBorderType(Grid.BorderType.LiveBorders);
                    canvas.DrawBorders(colors["white"]);
                }

                if (Keyboard.IsKeyDown(Keyboard.Key.W))
                {
                    game.SetBorderType(Grid.BorderType.WrapBorders);
                    canvas.DrawBorders(colors["gray 64"]);
                }

                if (Keyboard.IsKeyDown(Keyboard.Key.R))
                {
                    game.Randomize(D);
                }

                if (Keyboard.IsKeyDown(Keyboard.Key.C))
                {
                    game.Clear();
                }

                if (Keyboard.IsKeyDown(Keyboard.Key.RIGHT))
                {
                    if (game.paused)
                    {
                        game.Iterate();
                    }
                    else
                    {
                        game.paused = true;
                    }
                }

                if (Keyboard.IsKeyDown(Keyboard.Key.LEFT))
                {
                    if (game.paused)
                    {
                        game.Undo();
                    }
                    else
                    {
                        game.paused = true;
                    }
                }

                if (Keyboard.IsKeyDown(Keyboard.Key.SPACE))
                {
                    game.paused = !game.paused;
                }
            }
        }
Beispiel #4
0
        public FormGame()
        {
            InitializeComponent();

            AssemblyName asmName = Assembly.GetExecutingAssembly().GetName();

            title = $"{asmName.Name} - {asmName.Version}";

            void drawGame()
            {
                panelGame.Refresh();
                System.Threading.Thread.Sleep(500);
            }

            Load += (s, e) =>
            {
                Text = title;
                nudGeneration.Value  = 10;
                nudWidth.Value       = 100;
                nudHeight.Value      = 100;
                nudProbability.Value = 5;
            };

            nudWidth.ValueChanged += (s, e) =>
            {
                CurrentGame = new Game((int)nudWidth.Value, (int)nudHeight.Value);
            };

            nudHeight.ValueChanged += (s, e) =>
            {
                CurrentGame = new Game((int)nudWidth.Value, (int)nudHeight.Value);
            };

            buttonRandomize.Click += (s, e) =>
            {
                CurrentGame = new Game((int)nudWidth.Value, (int)nudHeight.Value);
                CurrentGame.Randomize((int)nudProbability.Value);
                drawGame();
            };

            buttonRun.Click += (s, e) =>
            {
                panelControls.Enabled = !buttonRun.Enabled;

                if (CurrentGame == null)
                {
                    CurrentGame = new Game((int)nudWidth.Value, (int)nudHeight.Value);
                    CurrentGame.Line();
                    drawGame();
                }

                for (int i = 0; i < nudGeneration.Value; i++)
                {
                    Text = $"{title} - Generation #{i + 1}";
                    CurrentGame.UpdateGame();
                    drawGame();
                }

                panelControls.Enabled = !buttonRun.Enabled;
            };

            panelGame.Paint += (s, e) =>
            {
                int cellSize = 4;

                if (CurrentGame != null)
                {
                    e.Graphics.SmoothingMode = SmoothingMode.HighSpeed;

                    using (var backColorBrush = new SolidBrush(panelGame.BackColor))
                    {
                        for (int y = 0; y < CurrentGame.Height; y++)
                        {
                            for (int x = 0; x < CurrentGame.Width; x++)
                            {
                                Brush brush = backColorBrush;

                                if (CurrentGame.CurrentCells[x, y].Alive)
                                {
                                    brush = Brushes.Lime;
                                }

                                e.Graphics.FillRectangle(brush, new Rectangle(x * cellSize, y * cellSize, cellSize, cellSize));
                            }
                        }
                    }
                }
            };
        }