コード例 #1
0
        public static Grid CreateGameGrid(GameController controller, Cell[,] game)
        {
            Grid grid = new Grid();
            int createdColumns = 0;

            for (int row = 0; row < game.GetLength(0); row++)
            {
                grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(25, GridUnitType.Pixel) });

                for (int column = 0; column < game.GetLength(1); column++)
                {
                    // Create columns only once
                    if (createdColumns < game.GetLength(1))
                    {
                        grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(25, GridUnitType.Pixel) });
                        createdColumns++;
                    }

                    // Create cell controls
                    CellControl cell = new CellControl(controller, game[row, column]);

                    Grid.SetRow(cell, row);
                    Grid.SetColumn(cell, column);
                    grid.Children.Add(cell);
                }
            }

            return grid;
        }
コード例 #2
0
        public void MarkCell(Cell cell)
        {
            if (cell.IsToggled) { return; }

            cell.IsMarked = !cell.IsMarked;

            if (cell.IsMarked)
            {
                NumberOfMarkedCells++;
            }
            else
            {
                NumberOfMarkedCells--;
            }
        }
コード例 #3
0
        public static List<Cell> GetNeighbours(this Cell[,] cells, Cell cell)
        {
            List<Cell> neighbours = new List<Cell>();
            int lowerRow = cell.Row - 1;
            int upperRow = cell.Row + 1;
            int lowerColumn = cell.Column - 1;
            int upperColumn = cell.Column + 1;

            for (int r = lowerRow; r <= upperRow; r++)
            {
                if (r >= cells.GetLength(0)) { continue; }
                for (int c = lowerColumn; c <= upperColumn; c++)
                {
                    if (r < 0 || c < 0 || (r == cell.Row && c == cell.Column) || c >= cells.GetLength(1)) { continue; }

                    neighbours.Add(cells[r, c]);
                }
            }

            return neighbours;
        }
コード例 #4
0
        public void OpenNeighbours(Cell cell)
        {
            if (!cell.IsToggled)
            {
                OpenCell(cell);
            }
            else
            {
                // Open cells and all depending cells
                var neighbours = Cells.GetNeighbours(cell);
                var markedNeighbours = neighbours.Count(n => n.IsMarked);

                if (markedNeighbours == cell.Number)
                {
                    neighbours.ForEach(ToggleCore);
                    CheckGameWon();
                }
            }
        }
コード例 #5
0
 public void OpenCell(Cell cell)
 {
     ToggleCore(cell);
     CheckGameWon();
 }
コード例 #6
0
        private void ToggleCore(Cell cell)
        {
            if (cell.IsToggled || cell.IsMarked) { return; }

            if (isFirstStep) { OnFirstStep(); }

            // Open cells and all depending cells
            cell.IsToggled = true;

            if (cell.Number == 0)
            {
                Cells.GetNeighbours(cell).ForEach(ToggleCore);
            }

            if (cell.Type == CellType.Mine)
            {
                OnGameOver();
                isGameOver = true;

                ToggleAllCells();
            }

            isFirstStep = false;
        }
コード例 #7
0
        private static Cell[,] CreateCells(int rows, int columns)
        {
            Cell[,] cells = new Cell[rows, columns];

            for (int row = 0; row < rows; row++)
            {
                for (int column = 0; column < columns; column++)
                {
                    cells[row, column] = new Cell(row, column);
                }
            }

            return cells;
        }