internal void CheckForPossibleMove() { if (checkedForPossibleMove) { return; } checkedForPossibleMove = true; Dictionary <GemType, Dictionary <Line, List <GemController> > > matchedNeighbors = new Dictionary <GemType, Dictionary <Line, List <GemController> > >(); Direction[] directions = Enum.GetValues(typeof(Direction)).Cast <Direction>().Where(v => v != Direction.None).ToArray(); List <GemController> availableNeighbors = new List <GemController>(); foreach (Direction dir in directions) { GemController neighbor = GetNeighbor(dir); if (neighbor != null && neighbor.CurrentGemType != CurrentGemType && neighbor.IsActive) { availableNeighbors.Add(neighbor); } } if (availableNeighbors.Count <= 1) { needToCheckPossibleMove = false; return; } for (int i = 0; i < availableNeighbors.Count; i++) { for (int j = i + 1; j < availableNeighbors.Count; j++) { if (availableNeighbors[i].CurrentGemType == availableNeighbors[j].CurrentGemType) { if (!matchedNeighbors.ContainsKey(availableNeighbors[i].CurrentGemType)) { matchedNeighbors[availableNeighbors[i].CurrentGemType] = new Dictionary <Line, List <GemController> >(); } Line gemLine = DirectionHelper.GetLineByDirection(GetDirectionByNeighbor(availableNeighbors[i])); if (!matchedNeighbors[availableNeighbors[i].CurrentGemType].ContainsKey(gemLine)) { matchedNeighbors[availableNeighbors[i].CurrentGemType][gemLine] = new List <GemController>(); } if (!matchedNeighbors[availableNeighbors[i].CurrentGemType][gemLine].Contains(availableNeighbors[i])) { matchedNeighbors[availableNeighbors[i].CurrentGemType][gemLine].Add(availableNeighbors[i]); } Line gemgemLine = DirectionHelper.GetLineByDirection(GetDirectionByNeighbor(availableNeighbors[j])); if (!matchedNeighbors[availableNeighbors[j].CurrentGemType].ContainsKey(gemgemLine)) { matchedNeighbors[availableNeighbors[j].CurrentGemType][gemgemLine] = new List <GemController>(); } if (!matchedNeighbors[availableNeighbors[j].CurrentGemType][gemgemLine].Contains(availableNeighbors[j])) { matchedNeighbors[availableNeighbors[j].CurrentGemType][gemgemLine].Add(availableNeighbors[j]); } } } } foreach (GemType type in matchedNeighbors.Keys) { foreach (Line line in matchedNeighbors[type].Keys) { Line oppositeLine = line == Line.Horizontal ? Line.Vertical : Line.Horizontal; if (matchedNeighbors[type][line].Count > 1) { if (matchedNeighbors[type].ContainsKey(oppositeLine)) { foreach (GemController keygem in matchedNeighbors[type][oppositeLine]) { PossibleMoveFound(matchedNeighbors[type][line].ToArray(), keygem, line); } } for (int i = 0; i < matchedNeighbors[type][line].Count; i++) { int otherindex = i == 0 ? 1 : 0; if (matchedNeighbors[type][line][i].GetPossibleMatchByDirection(line) != null) { PossibleMoveFound(new GemController[] { matchedNeighbors[type][line][i] }, matchedNeighbors[type][line][otherindex], line); } } } else if (matchedNeighbors[type][line].Count == 1) { if (matchedNeighbors[type].ContainsKey(oppositeLine)) { if (matchedNeighbors[type][line][0].GetPossibleMatchByDirection(line) != null) { foreach (GemController keygem in matchedNeighbors[type][oppositeLine]) { PossibleMoveFound(matchedNeighbors[type][line].ToArray(), keygem, line); } } } } } } needToCheckPossibleMove = false; if (needToCheckNeighborPossibleMove != Direction.None) { Direction[] dirs = Enum.GetValues(typeof(Direction)).Cast <Direction>().Where(d => d != Direction.None).ToArray(); foreach (Direction dir in dirs) { CheckNeighborPossibleMove(dir); } } }