Esempio n. 1
0
 /// <summary>
 /// Initialises the generation.
 /// </summary>
 private void Initialise()
 {
     for (int row = 0; row < UniverseSize; row++)
         for (int column = 0; column < UniverseSize; column++)
             universe[row, column] = new Cell(row, column, false);
 }
        /// <summary>
        /// Creates a mouse click binding for a cell so that the cell's
        /// living status can be toggled by the user.
        /// </summary>
        /// <param name="cell">The cell that this binding is for.</param>
        /// <returns>An InputBinding for the cell.</returns>
        private InputBinding CreateMouseClickInputBinding(Cell cell)
        {
            InputBinding cellTextBlockInputBinding = new InputBinding(
                generationViewModel.ToggleCellLifeCommand,
                new MouseGesture(MouseAction.LeftClick)
            );
            cellTextBlockInputBinding.CommandParameter = string.Format("{0},{1}", cell.Row, cell.Column);

            return cellTextBlockInputBinding;
        }
        /// <summary>
        /// Creates a TextBlock that represents a single cell in the game of life.
        /// </summary>
        /// <param name="cell">The cell that the TextBlock will represent.</param>
        /// <returns>A TextBlock representing the cell.</returns>
        private TextBlock CreateCellTextBlock(Cell cell)
        {
            TextBlock cellTextBlock = new TextBlock();
            cellTextBlock.DataContext = cell;
            cellTextBlock.InputBindings.Add(CreateMouseClickInputBinding(cell));
            cellTextBlock.SetBinding(TextBlock.BackgroundProperty, CreateCellAliveBinding());

            return cellTextBlock;
        }
        /// <summary>
        /// Gets the number of neighbor cells that are alive for a particular cell.
        /// </summary>
        /// <param name="generation">The generation.</param>
        /// <param name="cell">The cell whose living neighbour are being counted.</param>
        /// <returns>Number of alive neighbours for the specified cell.</returns>
        private int GetNumberOfAliveNeighbors(Generation generation, Cell cell)
        {
            int numberOfAliveNeighbours = 0;

            List<Cell> neighboringCells = new List<Cell>
            {
                generation.GetCell(cell.Row - 1, cell.Column - 1),
                generation.GetCell(cell.Row - 1, cell.Column + 1),
                generation.GetCell(cell.Row, cell.Column + 1),
                generation.GetCell(cell.Row + 1, cell.Column + 1),
                generation.GetCell(cell.Row + 1, cell.Column),
                generation.GetCell(cell.Row + 1, cell.Column - 1),
                generation.GetCell(cell.Row, cell.Column - 1),
                generation.GetCell(cell.Row - 1, cell.Column)
            };

            neighboringCells.ForEach(
                neighboringCell => numberOfAliveNeighbours +=
                    (neighboringCell != null && neighboringCell.Alive) ? 1 : 0
            );

            return numberOfAliveNeighbours;
        }