Esempio n. 1
0
 private void FillGameField()
 {
     for (var i = 0; i < _rowCount; i++)
     {
         for (var j = 0; j < _colCount; j++)
         {
             _gameField[i, j] = new MineButtonCell
             {
                 Selected = false,
                 Value = MineField.HiddenCell
             };
         }
     }
 }
Esempio n. 2
0
 private void OnMineExplotion(MineButtonCell cell)
 {
     TimeController.Enabled = false;
 }
Esempio n. 3
0
        private void OnCellOpen(MineButtonCell cell)
        {
            if (!_gameStarted)
            {
                TimeController.Enabled = true;
                _gameStarted = true;
            }

            if (!_gameField.AllMinesSweeped()) return;
            _gameField.OpenField();
            TimeController.Enabled = false;
        }
Esempio n. 4
0
 private void OnCellUnPoint(MineButtonCell cell)
 {
     _minesCountLeft++;
     UpdateMineCountLabel();
 }
Esempio n. 5
0
        private void InitializeField(MineButtonCell emptyCell)
        {
            int[] minePositions = new int[MineCount];

            Random rnd = new Random();

            int num = 0;
            while (num < MineCount)
            {
                int position = rnd.Next(0, RowCount * ColumnCount);

                int row = position / ColumnCount;
                int col = position % ColumnCount;

                //if mineCount lower 50% then cells count,
                //then first sel might be empty
                if ((MineCount < RowCount * ColumnCount / 2) &&
                (Math.Abs(emptyCell.RowIndex - row) <= 1) &&
                (Math.Abs(emptyCell.ColumnIndex - col) <= 1))
                    continue;

                MineButtonCell cell = this[row, col];

                if (!cell.Mined)
                {
                    cell.Mined = true;
                    for (int i = -1; i <= 1; i++)
                        for (int j = -1; j <= 1; j++)
                        {
                            if ((i == 0) && (j == 0)) continue;

                            if ((row + i >= 0) && (row + i < RowCount) &&
                                (col + j >= 0) && (col + j < ColumnCount))
                                this[row + i, col + j].NeighborMineCount++;
                        }
                    num++;
                }
            }

            _initializedField = true;
        }
Esempio n. 6
0
        protected void UpEnteredCell(MineButtonCell cell)
        {
            int row = cell.RowIndex;
            int col = cell.ColumnIndex;

            if ((left) && (right))
            {
                int? pointedCellsCount = null;

                if ((EnteredCell != null) && (EnteredCell.CellState == MineCellState.Opened))
                {

                    pointedCellsCount = 0;

                    for (int i = -1; i <= 1; i++)
                        for (int j = -1; j <= 1; j++)
                        {
                            if ((row + i < 0) || (row + i >= RowCount) ||
                            (col + j < 0) || (col + j >= ColumnCount)
                                || (this[row + i, col + j].CellState == MineCellState.Opened))
                                continue;

                            if (this[row + i, col + j].CellState == MineCellState.Pointed)
                                pointedCellsCount++;
                        }
                }

                for (int i = -1; i <= 1; i++)
                    for (int j = -1; j <= 1; j++)
                    {
                        if ((row + i < 0) || (row + i >= RowCount) ||
                        (col + j < 0) || (col + j >= ColumnCount)
                            || (this[row + i, col + j].CellState == MineCellState.Opened)
                            || (this[row + i, col + j].CellState == MineCellState.Pointed))
                            continue;
                        if ((pointedCellsCount != null) && (pointedCellsCount == EnteredCell.NeighborMineCount))
                        {
                            OpenCell(this[row + i, col + j]);
                        }
                        else
                        {
                            this[row + i, col + j].Value = GetCellValueByState(this[row + i, col + j]);
                        }
                    }
            }
            else if (!(right) && (left))
            {
                OpenCell(cell);
            }
        }
Esempio n. 7
0
        public void OpenCell(MineButtonCell cell)
        {
            if (!_initializedField)
                InitializeField(cell);

            if ((cell.CellState != MineCellState.Opened)
                && ((cell.CellState != MineCellState.Pointed)))
            {
                if (cell.Mined)
                {
                    cell.CellState = MineCellState.Opened;
                    cell.Value = ExplodedMine;
                    OnMineExplotion(cell);
                    OpenField();
                }
                else
                {
                    cell.CellState = MineCellState.Opened;
                    cell.Value = GetCellValue(cell);

                    if (cell.NeighborMineCount == 0)
                        for (int i = -1; i <= 1; i++)
                            for (int j = -1; j <= 1; j++)
                            {
                                if (!((i == 0) && (j == 0)) &&
                                    (cell.RowIndex + i >= 0) && (cell.RowIndex + i < RowCount) &&
                                    (cell.ColumnIndex + j >= 0) && (cell.ColumnIndex + j < ColumnCount))
                                {

                                    OpenCell(this[cell.RowIndex + i, cell.ColumnIndex + j]);
                                }
                            }

                }
            }

            OnCellOpen(cell);
        }
Esempio n. 8
0
        public Bitmap GetCellValueByState(MineButtonCell cell)
        {
            Bitmap result = null;

            switch (cell.CellState)
            {
                case MineCellState.Hidden:
                    {
                        result = HiddenCell;
                        break;
                    }
                case MineCellState.Pointed:
                    {
                        result = PointedCell;
                        break;
                    }
                case MineCellState.Unknown:
                    {
                        result = UnknownCell;
                        break;
                    }
                case MineCellState.Opened:
                    {
                        result = GetCellValue(cell);
                        break;
                    }
            }

            return result;
        }
Esempio n. 9
0
        public Bitmap GetCellValue(MineButtonCell cell)
        {
            Bitmap result = null;

            if (cell.Mined)
            {
                result = Mine;
            }
            else
                switch (cell.NeighborMineCount)
                {
                    case 0:
                        {
                            result = EmptyCell;
                            break;
                        }
                    case 1:
                        {
                            result = _1_Mine;
                            break;
                        }
                    case 2:
                        {
                            result = _2_Mine;
                            break;
                        }
                    case 3:
                        {
                            result = _3_Mine;
                            break;
                        }
                    case 4:
                        {
                            result = _4_Mine;
                            break;
                        }
                    case 5:
                        {
                            result = _5_Mine;
                            break;
                        }
                    case 6:
                        {
                            result = _6_Mine;
                            break;
                        }
                    case 7:
                        {
                            result = _7_Mine;
                            break;
                        }
                    case 8:
                        {
                            result = _8_Mine;
                            break;
                        }
                }

            return result;
        }