public void CombineStartingCellWithNeighbourCells()
 {
     GameBoard board = new GameBoard();
     board.AddCell(0, 1);
     board.AddCell(0, 0);
     board.AddCell(0, -1);
     board.Transition();
 }
 public void TheCellsLocation()
 {
     var expectedXcoord = 0;
     var expectedYcoord = 1;
     GameBoard board = new GameBoard();
     board.AddCell(0, 1);
     Assert.AreEqual(expectedXcoord, board[0].xcoord);
     Assert.AreEqual(expectedYcoord, board[0].ycoord);
 }
コード例 #3
0
ファイル: MainForm.cs プロジェクト: kunalgujar/GameOfLife
        /// <summary>Run a game.</summary>
        private void btnRun_Click(object sender, EventArgs e)
        {
            // If no game is currently running, run one
            if (_cancellation == null)
            {
                // Clear the current image, get the size of the board to use, initialize cancellation,
                // and prepare the form for game running.
                pictureBox1.Image = null;
                int width = pictureBox1.Width, height = pictureBox1.Height;
                _cancellation = new CancellationTokenSource();
                var token = _cancellation.Token;
                lblDensity.Visible = false;
                tbDensity.Visible = false;
                btnRun.Text = "Stop";
                double initialDensity = tbDensity.Value / 1000.0;

                // Initialize the object pool and the game board
                var pool = new ObjectPool<Bitmap>(() => new Bitmap(width, height));
                _game = new GameBoard(width, height, initialDensity, pool, cmbRuleset.SelectedItem.ToString()) { RunParallel = chkParallel.Checked };

                // Run the game on a background thread
                Task.Factory.StartNew(() =>
                {
                    // Run until cancellation is requested
                    var sw = new Stopwatch();
                    while (!token.IsCancellationRequested)
                    {
                        // Move to the next board, timing how long it takes
                        sw.Restart();
                        Bitmap bmp = _game.MoveNext();
                        var framesPerSecond = 1 / sw.Elapsed.TotalSeconds;

                        // Update the UI with the new board image
                        BeginInvoke((Action)(() =>
                        {
                            lblFramesPerSecond.Text = String.Format("Frames / Sec: {0:F2}", framesPerSecond);
                            var old = (Bitmap)pictureBox1.Image;
                            pictureBox1.Image = bmp;
                            if (old != null) pool.PutObject(old);
                        }));
                    }

                    // When the game is done, reset the board.
                }, token).ContinueWith(_ =>
                {
                    _cancellation = null;
                    btnRun.Text = "Start";
                    lblDensity.Visible = true;
                    tbDensity.Visible = true;
                }, TaskScheduler.FromCurrentSynchronizationContext());
            }

                // If a game is currently running, cancel it
            else _cancellation.Cancel();
        }
コード例 #4
0
        static void Main(string[] args)
        {
            List<Cell> cells = new List<Cell>();

            foreach (string arg in args)
            {
                string[] stuff = arg.Split(',');
                if (stuff.Length == 2)
                    cells.Add(new Cell(int.Parse(stuff[0]), int.Parse(stuff[1])));
            }

            GameBoard board = new GameBoard(cells);

            PrintWorld(board);

            while (Console.ReadKey().Key.Equals(ConsoleKey.Spacebar))
            {
                board.Evolve();
                PrintWorld(board);
            }
        }
コード例 #5
0
ファイル: Game.cs プロジェクト: hanscho/GameOfLife
 public Game(GameBoard board, TextBlock generationDisplay, LifeRules rules)
 {
     _board = board;
     _generationDisplay = generationDisplay;
     _rules = rules;
 }
コード例 #6
0
ファイル: Game.cs プロジェクト: hanscho/GameOfLife
 public Game(GameBoard board, TextBlock generationDisplay)
     : this(board, generationDisplay, LifeRules.Normal)
 {
 }
コード例 #7
0
        public static void Main(string[] args)
        {
            int  generations = 0;
            char continuous  = ' ';
            int  steadyCount = 0;
            int  loopCount   = 0;

            try {
                //check for GOLInit file name argument
                if (args.Length < 1)
                {
                    Console.WriteLine("You must enter an GOLInit filename argument. Exiting.");
                }
                else
                {
                    //initialize gameboard
                    char[,] gameBoard = GameBoard.Init(args[0]);
                    //display initial gameboard
                    GameBoard.Display(generations, gameBoard);

                    Console.WriteLine("How many generations would you like to see?");
                    //input value error handling
                    generations = GameBoard.ValidateNumber();
                    while (generations < 0)
                    {
                        Console.WriteLine("Please enter a valid positive integer.");
                        generations = GameBoard.ValidateNumber();
                    }

                    Console.WriteLine("Would you like to see the generations continuously?");
                    continuous = Convert.ToChar(Console.ReadLine().ToUpper());
                    //input value error handling
                    while (continuous != 'Y' && continuous != 'N')
                    {
                        Console.WriteLine("Please enter a Y for yes or N for no.");
                        continuous = Convert.ToChar(Console.ReadLine().ToUpper());
                    }

                    //run the game for n generations
                    for (int i = 1; i <= generations; i++)
                    {
                        gameBoard = GameBoard.NextGeneration(gameBoard);
                        GameBoard.Display(i, gameBoard);
                        //prompt to see next generation
                        if (continuous != 'Y')
                        {
                            Console.WriteLine("Please press any key to see the next generation.");
                            Console.ReadKey();
                        }
                        //if all cels die, terminate early
                        if (GameBoard.CellCount(gameBoard) == 0)
                        {
                            Console.WriteLine("All of the cells on the gameboard are dead.");
                            break;
                        }
                        //if the game reaches a steady state, terminate early
                        if (GameBoard.Equals(gameBoard, GameBoard.NextGeneration(gameBoard)))
                        {
                            if (steadyCount > 0)
                            {
                                Console.WriteLine("The game has reached a steady state.");
                                break;
                            }
                            steadyCount++;
                            continue;
                        }
                        //if the game becomes an infinite loop, terminate early
                        if (GameBoard.Equals(gameBoard, GameBoard.NextGeneration(GameBoard.NextGeneration(gameBoard))) &&
                            GameBoard.Equals(GameBoard.NextGeneration(gameBoard), GameBoard.NextGeneration(GameBoard.NextGeneration(
                                                                                                               GameBoard.NextGeneration(gameBoard)))))
                        {
                            if (loopCount > 2)
                            {
                                Console.WriteLine("The game is in an infinite loop.");
                                break;
                            }
                            loopCount++;
                            continue;
                        }
                    }
                }
                //non numeric or negative GOLInit file data error handling
            } catch (FormatException ex) {
                Console.WriteLine("Corrupt GOLInit file data. Exiting.");
            }
        }
コード例 #8
0
 static void PrintWorld(GameBoard board)
 {
     Console.Out.WriteLine(board.ScreenFriendlyWorld());
     Console.Out.WriteLine("---------");
 }
コード例 #9
0
ファイル: MainForm.cs プロジェクト: presscad/BaseLayer
        /// <summary>Run a game.</summary>
        private void btnRun_Click(object sender, EventArgs e)
        {
            // If no game is currently running, run one
            if (_cancellation == null)
            {
                // Clear the current image, get the size of the board to use, initialize cancellation,
                // and prepare the form for game running.
                pictureBox1.Image = null;
                int width = pictureBox1.Width, height = pictureBox1.Height;
                _cancellation = new CancellationTokenSource();
                var token = _cancellation.Token;
                lblDensity.Visible = false;
                tbDensity.Visible  = false;
                btnRun.Text        = "Stop";
                double initialDensity = tbDensity.Value / 1000.0;

                // Initialize the object pool and the game board
                var pool = new ObjectPool <Bitmap>(() => new Bitmap(width, height));
                _game = new GameBoard(width, height, initialDensity, pool)
                {
                    RunParallel = chkParallel.Checked
                };

                // Run the game on a background thread
                Task.Factory.StartNew(() =>
                {
                    // Run until cancellation is requested
                    var sw = new Stopwatch();
                    while (!token.IsCancellationRequested)
                    {
                        // Move to the next board, timing how long it takes
                        sw.Restart();
                        Bitmap bmp          = _game.MoveNext();
                        var framesPerSecond = 1 / sw.Elapsed.TotalSeconds;

                        // Update the UI with the new board image
                        BeginInvoke((Action)(() =>
                        {
                            lblFramesPerSecond.Text = String.Format("Frames / Sec: {0:F2}", framesPerSecond);
                            var old = (Bitmap)pictureBox1.Image;
                            pictureBox1.Image = bmp;
                            if (old != null)
                            {
                                pool.PutObject(old);
                            }
                        }));
                    }

                    // When the game is done, reset the board.
                }, token).ContinueWith(_ =>
                {
                    _cancellation      = null;
                    btnRun.Text        = "Start";
                    lblDensity.Visible = true;
                    tbDensity.Visible  = true;
                }, TaskScheduler.FromCurrentSynchronizationContext());
            }

            // If a game is currently running, cancel it
            else
            {
                _cancellation.Cancel();
            }
        }
 public void CellAddedToGameBoardIsAlive()
 {
     GameBoard board = new GameBoard();
     board.AddCell(0, 0);
     Assert.IsTrue(board[0].extant);
 }
 public void AddCellMethodAddsACellToGameBoard()
 {
     GameBoard dish = new GameBoard();
     dish.AddCell(0, 0);
     Assert.IsTrue(dish.Count == 1);
 }
コード例 #12
0
ファイル: Game.cs プロジェクト: khellang/GameOfLife
 public Game(GameBoard board, TextBlock generationDisplay)
 {
     _board = board;
     _generationDisplay = generationDisplay;
 }