Пример #1
0
        /// <summary>
        /// Detect pair in a region.
        /// </summary>
        public List <MatchPair> DetectRange(LevelMap map, int xStart, int xEnd, int yStart, int yEnd,
                                            bool returnIfFindOne = false)
        {
            pairList = new List <MatchPair>();

            for (int i = xStart; i < xEnd; ++i)
            {
                GemType prevGemMatchedType = GemType.None;
                int     prevGemCount       = 0;
                int     prevMagicGemCount  = 0;

                for (int j = 0; j < map.Height; ++j)
                {
                    GemType currGemType = map[j, i];

                    // If the current gem is matched with the previous gem
                    GemType matchedGemType = Gem.GetMatchedGemType(currGemType, prevGemMatchedType);
                    if (matchedGemType != GemType.None)
                    {
                        // Update gem type
                        prevGemMatchedType = matchedGemType;

                        // Update gem pair count
                        ++prevGemCount;
                        prevMagicGemCount = (currGemType == GemType.Magic) ? prevMagicGemCount + 1 : 0;

                        continue;
                    }

                    // Add detected pair
                    if (prevGemCount >= 3)
                    {
                        AddNewPair(j - prevGemCount, j, i, prevGemMatchedType);

                        if (returnIfFindOne)
                        {
                            return(pairList);
                        }
                    }

                    // Reset gem stat
                    prevGemMatchedType = currGemType;
                    prevGemCount       = prevMagicGemCount + 1;
                    prevMagicGemCount  = (currGemType == GemType.Magic) ? prevMagicGemCount + 1 : 0;
                }

                // Add detected pair
                if (prevGemCount >= 3)
                {
                    AddNewPair(map.Height - prevGemCount, map.Height, i, prevGemMatchedType);

                    if (returnIfFindOne)
                    {
                        return(pairList);
                    }
                }
            }

            return(pairList);
        }
Пример #2
0
        /// <summary>
        /// Find a gem match in the neighbour gems except the given mate cell direction.
        /// </summary>
        private MatchSwap?DetectGemSwap(LevelMap map, int x, int y, GemType type, Vector2Int mateDir)
        {
            Vector2Int[] neighbourCoords = map.GetNeighbourCoords(new Vector2Int(x, y));
            Vector2Int   mateCoord       = new Vector2Int(x, y) + mateDir;

            foreach (var coord in neighbourCoords)
            {
                if (coord == mateCoord)
                {
                    continue;
                }

                if (Gem.IsMatched(type, map[coord]))
                {
                    return(CreateMatchSwap(x, y, coord.x, coord.y, type));
                }
            }

            return(null);
        }
Пример #3
0
 /// <summary>
 /// Detect matched pairs.
 /// </summary>
 public List <MatchPair> Detect(LevelMap map)
 {
     return(DetectRange(map, 0, map.Width, 0, map.Height));
 }
Пример #4
0
        /// <summary>
        /// Detect possible swaps.
        /// </summary>
        public MatchSwap?DetectSwap(LevelMap map)
        {
            for (int i = 0; i < map.Height; ++i)
            {
                for (int j = 1; j < map.Width - 1; ++j)
                {
                    // Get gem type of neighbour gems.
                    GemType self  = map[i, j];
                    GemType left  = map[i, j - 1];
                    GemType right = map[i, j + 1];
                    GemType up    = (i > 0)              ? map[i - 1, j] : GemType.None;
                    GemType down  = (i < map.Height - 1) ? map[i + 1, j] : GemType.None;

                    bool leftRightMatched = Gem.IsMatched(left, right);
                    bool leftSelfMatched  = Gem.IsMatched(left, self);
                    bool rightSelfMatched = Gem.IsMatched(right, self);

                    // X O X
                    if (leftRightMatched && !leftSelfMatched)
                    {
                        GemType matchedType = Gem.GetMatchedGemType(left, right);

                        if (Gem.IsMatched(matchedType, up))
                        {
                            return(CreateMatchSwap(j, i, j, i - 1, matchedType));
                        }

                        if (Gem.IsMatched(matchedType, down))
                        {
                            return(CreateMatchSwap(j, i, j, i + 1, matchedType));
                        }
                    }

                    // Magic Gem may apply to both cases below
                    // so we have to check both cases separately

                    // X X O
                    if (leftSelfMatched)
                    {
                        GemType   matchedType = Gem.GetMatchedGemType(left, self);
                        MatchSwap?swap        = DetectGemSwap(map, j + 1, i, matchedType, new Vector2Int(-1, 0));
                        if (swap.HasValue)
                        {
                            return(swap);
                        }
                    }

                    // O X X
                    if (rightSelfMatched)
                    {
                        GemType   matchedType = Gem.GetMatchedGemType(right, self);
                        MatchSwap?swap        = DetectGemSwap(map, j - 1, i, matchedType, new Vector2Int(1, 0));
                        if (swap.HasValue)
                        {
                            return(swap);
                        }
                    }
                }
            }

            return(null);
        }
Пример #5
0
        /// <summary>
        /// Detect possible swaps.
        /// </summary>
        public MatchSwap?DetectSwap(LevelMap map)
        {
            for (int i = 1; i < map.Height - 1; ++i)
            {
                for (int j = 0; j < map.Width; ++j)
                {
                    // Get gem type of neighbour gems.
                    GemType self  = map[i, j];
                    GemType left  = (j > 0)             ? map[i, j - 1] : GemType.None;
                    GemType right = (j < map.Width - 1) ? map[i, j + 1] : GemType.None;
                    GemType up    = map[i - 1, j];
                    GemType down  = map[i + 1, j];

                    bool upDownMatched   = Gem.IsMatched(up, down);
                    bool upSelfMatched   = Gem.IsMatched(up, self);
                    bool downSelfMatched = Gem.IsMatched(down, self);

                    // up X O X down
                    if (upDownMatched && !upSelfMatched)
                    {
                        GemType matchedType = Gem.GetMatchedGemType(up, down);
                        if (Gem.IsMatched(matchedType, left))
                        {
                            return(CreateMatchSwap(j, i, j - 1, i, matchedType));
                        }

                        if (Gem.IsMatched(matchedType, right))
                        {
                            return(CreateMatchSwap(j, i, j + 1, i, matchedType));
                        }
                    }

                    // Magic Gem may apply to both cases below
                    // so we have to check both cases separately

                    // up X X O down
                    if (upSelfMatched)
                    {
                        GemType   matchedType = Gem.GetMatchedGemType(up, self);
                        MatchSwap?swap        = DetectGemSwap(map, j, i + 1, matchedType, new Vector2Int(0, -1));
                        if (swap.HasValue)
                        {
                            return(swap);
                        }
                    }

                    // up O X X down
                    if (downSelfMatched)
                    {
                        GemType   matchedType = Gem.GetMatchedGemType(down, self);
                        MatchSwap?swap        = DetectGemSwap(map, j, i - 1, matchedType, new Vector2Int(0, 1));
                        if (swap.HasValue)
                        {
                            return(swap);
                        }
                    }
                }
            }

            return(null);
        }