Exemplo n.º 1
0
        public void UncoverCell(MineCell cell)
        {
            if (cell.HasFlag || cell.IsUncovered)
            {
                return;
            }

            cell.IsUncovered = true;
            if (cell.HasMine)
            {
                GameState = GameState.Defeat;
                _chrono.Stop();
            }
            else
            {
                if (cell.NeighbourhoodMineCount == 0)
                {
                    UncoverNeighbours(cell);
                }

                if (Cells.All(c => c.HasMine ^ c.IsUncovered))
                {
                    GameState = GameState.Victory;
                    _chrono.Stop();
                }
            }
        }
Exemplo n.º 2
0
        private bool PlayEasyMoves(MineCell cell, bool returnOnFirstAction)
        {
            var result = false;
            var neighbours = _mineField.GetNeighbours(cell).ToArray();
            var flaggedNeighbours = neighbours.FilterByVisibleState(MineState.Flagged).ToArray();
            var coveredNeighbours = neighbours.FilterByVisibleState(MineState.Covered).ToArray();

            if (coveredNeighbours.Length > 0 && cell.NeighbourhoodMineCount == (flaggedNeighbours.Length + coveredNeighbours.Length))
            {
                for (var i = 0; i < coveredNeighbours.Length && !(result && returnOnFirstAction); i++)
                {
                    coveredNeighbours[i].HasFlag = true;
                    result = true;
                }
            }
            else if (coveredNeighbours.Length > 0 && cell.NeighbourhoodMineCount == flaggedNeighbours.Length)
            {
                for (var i = 0; i < coveredNeighbours.Length && !(result && returnOnFirstAction); i++)
                {
                    _mineField.UncoverCell(coveredNeighbours[i]);
                    result = true;
                }
            }

            return result;
        }
Exemplo n.º 3
0
        private bool PlayEasyMoves(MineCell cell, bool returnOnFirstAction)
        {
            var result            = false;
            var neighbours        = _mineField.GetNeighbours(cell).ToArray();
            var flaggedNeighbours = neighbours.FilterByVisibleState(MineState.Flagged).ToArray();
            var coveredNeighbours = neighbours.FilterByVisibleState(MineState.Covered).ToArray();

            if (coveredNeighbours.Length > 0 && cell.NeighbourhoodMineCount == (flaggedNeighbours.Length + coveredNeighbours.Length))
            {
                for (var i = 0; i < coveredNeighbours.Length && !(result && returnOnFirstAction); i++)
                {
                    coveredNeighbours[i].HasFlag = true;
                    result = true;
                }
            }
            else if (coveredNeighbours.Length > 0 && cell.NeighbourhoodMineCount == flaggedNeighbours.Length)
            {
                for (var i = 0; i < coveredNeighbours.Length && !(result && returnOnFirstAction); i++)
                {
                    _mineField.UncoverCell(coveredNeighbours[i]);
                    result = true;
                }
            }

            return(result);
        }
        public MineFieldCell(int row, int col, MineCell mineCell, Func<MineFieldCell, Task> leftClick, Func<MineFieldCell, Task> rightClick)
            : this()
        {
            Row = row;
            Col = col;
            MineCell = mineCell;

            _previousState = MineState.Covered;
            _leftClick = leftClick;
            _rightClick = rightClick;
        }
        public MineFieldCell(int row, int col, MineCell mineCell, Func <MineFieldCell, Task> leftClick, Func <MineFieldCell, Task> rightClick)
            : this()
        {
            Row      = row;
            Col      = col;
            MineCell = mineCell;

            _previousState = MineState.Covered;
            _leftClick     = leftClick;
            _rightClick    = rightClick;
        }
Exemplo n.º 6
0
        public void UncoverNeighbours(MineCell cell)
        {
            var neighbours = GetNeighbours(cell);

            if (!cell.HasFlag && neighbours.Count(c => c.HasFlag) == cell.NeighbourhoodMineCount)
            {
                foreach (var neighbour in GetNeighbours(cell))
                {
                    UncoverCell(neighbour);
                }
            }
        }
Exemplo n.º 7
0
        private bool PlayHardMoves(MineCell[] uncoveredCells, bool returnOnFirstAction)
        {
            var minedZones = GetMinedZones(uncoveredCells);

            var hardMoves = from zone in minedZones
                            from other in minedZones
                            where zone.Intersect(other)
                               && PlayHardMoves(zone, other, returnOnFirstAction)
                            select 1;

            return returnOnFirstAction
                        ? hardMoves.Any()
                        : hardMoves.Count() > 0;
        }
Exemplo n.º 8
0
        public MineField(int rows, int cols, int mineCount)
        {
            Rows = rows;
            Cols = cols;
            _mineCount = mineCount;
            GameState = GameState.NotStarted;

            Cells = new MineCell[rows * cols];

            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    Cells[GetCellIndex(row, col)] = new MineCell(row, col);
                }
            }
        }
Exemplo n.º 9
0
        public MineField(int rows, int cols, int mineCount)
        {
            Rows       = rows;
            Cols       = cols;
            _mineCount = mineCount;
            GameState  = GameState.NotStarted;

            Cells = new MineCell[rows * cols];

            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    Cells[GetCellIndex(row, col)] = new MineCell(row, col);
                }
            }
        }
Exemplo n.º 10
0
        private ICollection<MinedZone> GetMinedZones(MineCell[] uncoveredCells)
        {
            var minedZonesById = new Dictionary<string, MinedZone>();
            foreach (var cell in uncoveredCells)
            {
                var neighbours = _mineField.GetNeighbours(cell).ToArray();
                var flaggedNeighbours = neighbours.FilterByVisibleState(MineState.Flagged).ToArray();
                var coveredNeighbours = neighbours.FilterByVisibleState(MineState.Covered).ToArray();
                var remainingCount = cell.NeighbourhoodMineCount - flaggedNeighbours.Length;
                if (remainingCount > 0 && remainingCount < coveredNeighbours.Length)
                {
                    var zone = new MinedZone(remainingCount, coveredNeighbours);
                    minedZonesById[zone.Id] = zone;
                }
            }

            return minedZonesById.Values;
        }
Exemplo n.º 11
0
 public IEnumerable<MineCell> GetNeighbours(MineCell cell) => GetNeighbours(cell.Row, cell.Col);
Exemplo n.º 12
0
 public void UncoverNeighbours(MineCell cell)
 {
     var neighbours = GetNeighbours(cell);
     if (!cell.HasFlag && neighbours.Count(c => c.HasFlag) == cell.NeighbourhoodMineCount)
     {
         foreach (var neighbour in GetNeighbours(cell))
         {
             UncoverCell(neighbour);
         }
     }
 }
Exemplo n.º 13
0
        public void UncoverCell(MineCell cell)
        {
            if (cell.HasFlag || cell.IsUncovered)
            {
                return;
            }

            cell.IsUncovered = true;
            if (cell.HasMine)
            {
                GameState = GameState.Defeat;
                _chrono.Stop();
            }
            else
            {
                if (cell.NeighbourhoodMineCount == 0)
                {
                    UncoverNeighbours(cell);
                }

                if (Cells.All(c => c.HasMine ^ c.IsUncovered))
                {
                    GameState = GameState.Victory;
                    _chrono.Stop();
                }
            }
        }
Exemplo n.º 14
0
 public MinedZone(int mineCount, MineCell[] cells)
 {
     MineCount = mineCount;
     _cells = cells;
     Id = string.Join("|", _cells.Select(c => $"{c.Row},{c.Col}").OrderBy(c => c));
 }
Exemplo n.º 15
0
 public IEnumerable <MineCell> GetNeighbours(MineCell cell) => GetNeighbours(cell.Row, cell.Col);