Ejemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (working)
            {
                working = false; return;
            }

            button1.Text         = "Stop";
            CellPanel.AllowClick = false;
            var board = IsLoopBox.Checked ? LifeGameBoard.CreateLoopBoard(height, width, (_h, _w) => boardPanel[_h, _w]) : LifeGameBoard.CreateBoard(height, width, (_h, _w) => boardPanel[_h, _w]);
            int h = height, w = width;

            working = true;
            Task.Run(() =>
            {
                var nextFrame = Environment.TickCount;
                while (true)
                {
                    if (!working)
                    {
                        break;
                    }
                    if (nextFrame <= Environment.TickCount)
                    {
                        nextFrame += 100;
                        board.AlternateGeneration();

                        Invoke((MethodInvoker)(() =>
                        {
                            numericUpDown1.Value = ++generation;
                            for (int i = 0; i < h; i++)
                            {
                                for (int k = 0; k < w; k++)
                                {
                                    boardPanel[i, k] = board[i, k];
                                }
                            }
                        }));
                    }
                }
                Invoke((MethodInvoker)(() => button1.Text = "Start"));
                CellPanel.AllowClick = true;
            });
        }
Ejemplo n.º 2
0
        private Task AdvanceGame(int advance = 5)
        {
            var board = IsTorus ?
                        LifeGameBoard.CreateLoopBoard(BoardSize, BoardSize, (_h, _w) => boardPanel[_h, _w]) :
                        LifeGameBoard.CreateBoard(BoardSize, BoardSize, (_h, _w) => boardPanel[_h, _w]);

            CellPanel.AllowClick = false;
            return(Task.Run(() =>
            {
                var nextFrame = Environment.TickCount + 100;
                int count = 0;
                while (true)
                {
                    if (nextFrame <= Environment.TickCount)
                    {
                        nextFrame += 100;
                        board.AlternateGeneration();

                        Invoke((MethodInvoker)(() =>
                        {
                            numericUpDown1.Value += 1;
                            for (int i = 0; i < BoardSize; i++)
                            {
                                for (int k = 0; k < BoardSize; k++)
                                {
                                    boardPanel[i, k] = board[i, k];
                                }
                            }
                        }));
                        if (++count == advance)
                        {
                            break;
                        }
                    }
                }

                CellPanel.AllowClick = true;
            }));
        }