/// <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); }
/// <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); }