Example #1
0
        public void Detonate()
        {
            bool success = false;

            var bonusesToDetonate = new List <BonusCell>();

            foreach (var cell in Grid)
            {
                if (cell is BonusCell bonus)
                {
                    if (bonus.IsDetonated)
                    {
                        bonusesToDetonate.Add(bonus);
                    }
                }
            }

            foreach (var bonus in bonusesToDetonate)
            {
                bonus.IsEmpty = true;
                success       = true;
                var bonusMatch = bonus.Action(Grid);
                foreach (var dead in bonusMatch)
                {
                    if (dead != null)
                    {
                        var deadBonus = dead as BonusCell;
                        if (deadBonus == null || deadBonus.IsEmpty || dead.Col == bonus.Col && dead.Row == bonus.Row)
                        {
                            DeleteCell(dead.Col, dead.Row, dead.Points);
                        }
                        else if (!deadBonus.IsDetonated)
                        {
                            deadBonus.IsDetonated = true;
                            ActivatedBonus.Invoke(null, new Tuple <int, int>(deadBonus.Col, deadBonus.Row));
                        }
                    }
                }
            }
            if (!success)
            {
                NoMoreMatches.Invoke(null, new EventArgs());
            }
        }
Example #2
0
        public void DestroyMatches()
        {
            var matches = GetMatches();

            if (!matches.Any())
            {
                if (HaveToDetonate())
                {
                    Detonated.Invoke(null, new EventArgs());
                    return;
                }

                // Check if there is two selected cells
                // it means that there was an unsuccessful swap, so swap back
                if (FirstTouched != null && LastTouched != null)
                {
                    Swap(FirstTouched, LastTouched);
                }
                // Invoke that there is no more matches on the field
                NoMoreMatches.Invoke(null, new EventArgs());

                //Remove cells selection
                ResetSelected();
                return;
            }

            // Consolidate intersected matches into the big one
            Consolidate(matches);

            for (var i = 0; i < matches.Count(); i++)
            {
                // Trying to get bonus cells for this match.
                // There can be more then one bonus for each match, cause bomb appears on
                // every intersection of vertical and horizontal match of the same color.
                var bonusCellsPlaces = GetBonusPlaceCells(matches[i]);
                var bonusCells       = GetBonusCells(matches[i].First().Type, matches[i], bonusCellsPlaces);
                foreach (var bonus in bonusCells)
                {
                    // Don't add bonus to the cell which already has a bonus
                    if (Grid[bonus.Col, bonus.Row] is BonusCell)
                    {
                        continue;
                    }
                    InvokeCellDeleted(bonus.Col, bonus.Row, Grid[bonus.Col, bonus.Row].Points);
                    Grid[bonus.Col, bonus.Row] = bonus;
                    BonusCreated(null, bonus);
                }


                foreach (var cell in matches[i])
                {
                    var col = cell.Col;
                    var row = cell.Row;

                    DeleteCell(col, row, cell.Points);
                }
            }

            // Now all bonuses are old
            ResetNewBonuses();

            // And nothing is selected
            ResetSelected();
        }