예제 #1
0
        // ReSharper disable once ReturnTypeCanBeEnumerable.Global
        public IAction[] Swap(Point src, Point dest)
        {
            var actions = new List <IAction>
            {
                new SwapAction
                {
                    Src  = new ItemPos(src, Items[src.X, src.Y]),
                    Dest = new ItemPos(dest, Items[dest.X, dest.Y]),
                }
            };

            _board.Swap(src, dest);

            MatchRes match = _board.CalcMatches();

            if (match.IsEmpty)
            {
                actions.Add(new SwapAction
                {
                    Src  = new ItemPos(src, Items[src.X, src.Y]),
                    Dest = new ItemPos(dest, Items[dest.X, dest.Y]),
                });
                _board.Swap(src, dest); // swap back
            }
            else
            {
                while (!match.IsEmpty)
                {
                    IEnumerable <IAction> processed =
                        ProcessMatch(src, dest, match);

                    actions.AddRange(processed);

                    match = _board.CalcMatches();
                }
            }

            return(actions.ToArray());
        }
예제 #2
0
        private IEnumerable <IAction> ProcessMatch(Point src,
                                                   Point dest,
                                                   MatchRes match)
        {
            var actions = new List <IAction>();
            //
            var regularDestroy = new HashSet <Point>();

            match.MatchLines.ForEach(l => regularDestroy.UnionWith(l));
            match.MatchCrosses.ForEach(cr =>
            {
                (Point[] line1, Point[] line2) = cr;
                regularDestroy.UnionWith(line1);
                regularDestroy.UnionWith(line2);
            });
            ItemPos[] regularDestroyPos = regularDestroy
                                          .Select(p => new ItemPos(p, Items[p.X, p.Y]))
                                          .ToArray();
            //
            var bonuses = new List <ItemPos>();

            match.MatchLines.ForEach(line =>
            {
                ItemPos bonus = CalcBonusSpawn(src, dest, line);
                if (bonus != null && bonuses.All(b => b.Pos != bonus.Pos))
                {
                    bonuses.Add(bonus);
                }
            });
            match.MatchCrosses.ForEach(cross =>
            {
                ItemPos bonus = CalcBonusSpawn(src, dest, cross);
                if (bonus != null && bonuses.All(b => b.Pos != bonus.Pos))
                {
                    bonuses.Add(bonus);
                }
            });

            var except      = new HashSet <Point>(regularDestroy); // New modified set
            var destroyedBy = new Dictionary <ItemPos, ItemPos[]>();

            regularDestroyPos
            .Where(p => !p.Item.IsRegularShape).ToList()
            .ForEach(p => CollectBonusDestroy(p, destroyedBy, except));
            //
            UpdateBoardOnDestroy(regularDestroyPos, destroyedBy, bonuses);
            //
            actions.Add(new DestroyAction
            {
                MatchDestroyedPos = regularDestroyPos,
                SpawnBonuses      = bonuses.ToArray(),
                DestroyedBy       = destroyedBy,
            });

            FallDownPos[] fallen = _board.CalcFallDownPositions().ToArray();
            actions.Add(new FallDownAction {
                Positions = fallen
            });

            ItemPos[] spawned = _board.SpawnItems().ToArray();
            actions.Add(new SpawnAction {
                Positions = spawned
            });

            return(actions);
        }