/// <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 = yStart; i < yEnd; ++i) { GemType prevGemMatchedType = GemType.None; int prevGemCount = 0; int prevMagicGemCount = 0; for (int j = 0; j < map.Width; ++j) { GemType currGemType = map[i, j]; // 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.Width - prevGemCount, map.Width, i, prevGemMatchedType); if (returnIfFindOne) { return(pairList); } } } return(pairList); }
/// <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); }
/// <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); }