/// <summary> /// Находит любую возможную комбинацию /// </summary> /// <param name="chuzzles">Список элементов в котором надо найти комбинацию</param> /// <returns>Список элементов которые составляют эту комбинацию</returns> public static List<Chuzzle> Tip(TilesCollection chuzzles, out IntVector2 isHorizontalMove, out Chuzzle chuzzleToMove) { var bottom = chuzzles.FirstOrDefault( x => BetweenYCheck(x, chuzzles)); if (bottom != null && bottom.Current.Top != null && bottom.Current.Top.Type != CellTypes.Block) { var top = chuzzles.First(ch => ch.Current == bottom.Current.Top.Top); var bottomPart = RecursiveFind(bottom, new List<Chuzzle>(), chuzzles); var middlePart = GetHorizontalLineChuzzles(bottom.Current.Y + 1, bottom.Color, chuzzles); var topPart = RecursiveFind(top, new List<Chuzzle>(), chuzzles); var possibleCombination = new List<Chuzzle>(); possibleCombination.AddRange(bottomPart); possibleCombination.AddRange(middlePart); possibleCombination.AddRange(topPart); //Debug.Log("Combination 1"); isHorizontalMove = new IntVector2(bottom.Current.X, bottom.Current.Y + 1); chuzzleToMove = middlePart.First(); //var target = possibleCombination.Aggregate("", (current, chuzzle) => current + (chuzzle + Environment.NewLine)); //Debug.Log(target); return possibleCombination; } var left = chuzzles.FirstOrDefault(x => BetweenXCheck(x, chuzzles)); if (left != null && left.Current.Right != null && left.Current.Right.Type != CellTypes.Block) { var right = chuzzles.First(ch => ch.Current == left.Current.Right.Right); var leftPart = RecursiveFind(left, new List<Chuzzle>(), chuzzles); var middlePart = GetVerticalLineChuzzles(left.Current.Right.X, left.Color, chuzzles); var rightPart = RecursiveFind(right, new List<Chuzzle>(), chuzzles); var possibleCombination = new List<Chuzzle>(); possibleCombination.AddRange(leftPart); possibleCombination.AddRange(middlePart); possibleCombination.AddRange(rightPart); isHorizontalMove = new IntVector2(left.Current.X + 1, left.Current.Y); chuzzleToMove = middlePart.First(); //Debug.Log("Combination 2: " + chuzzleToMove); //var target = possibleCombination.Aggregate("", (current, chuzzle) => current + (chuzzle + Environment.NewLine)); //Debug.Log(target); return possibleCombination; } var combinations = FindCombinations(chuzzles, 2); foreach (var combination in combinations) { var first = combination[0]; var second = combination[1]; //vertical combination if (first.Current.X == second.Current.X) { combination.Sort(CompareByY); var topChuzzle = combination[0]; var bottomChuzzle = combination[1]; //try left if ((topChuzzle.Current.Left != null && topChuzzle.Current.Left.Type != CellTypes.Block) || (bottomChuzzle.Current.Left != null && bottomChuzzle.Current.Left.Type != CellTypes.Block)) { var leftPart = GetVerticalLineChuzzles(topChuzzle.Current.X - 1, topChuzzle.Color, chuzzles).ToList(); if (leftPart.Any()) { var possibleCombination = new List<Chuzzle>(); possibleCombination.AddRange(combination); possibleCombination.AddRange(leftPart); //Debug.Log("Combination 3"); //var target = possibleCombination.Aggregate("", (current, chuzzle) => current + (chuzzle + Environment.NewLine)); //Debug.Log(target); isHorizontalMove = new IntVector2(topChuzzle.Current.X - 1, topChuzzle.Current.Y); chuzzleToMove = leftPart.First(); return possibleCombination; } } //try right if ((topChuzzle.Current.Right != null && topChuzzle.Current.Right.Type != CellTypes.Block) || (bottomChuzzle.Current.Right != null && bottomChuzzle.Current.Right.Type != CellTypes.Block)) { var rightPart = GetVerticalLineChuzzles(topChuzzle.Current.X + 1, topChuzzle.Color, chuzzles).ToList(); if (rightPart.Any()) { var possibleCombination = new List<Chuzzle>(); possibleCombination.AddRange(combination); possibleCombination.AddRange(rightPart); //Debug.Log("Combination 4"); //Svar target = possibleCombination.Aggregate("", (current, chuzzle) => current + (chuzzle + Environment.NewLine)); //Debug.Log(target); isHorizontalMove = new IntVector2(topChuzzle.Current.X + 1, topChuzzle.Current.Y); chuzzleToMove = rightPart.First(); return possibleCombination; } } //try top if (bottomChuzzle.Current.Top != null && bottomChuzzle.Current.Top.Type != CellTypes.Block && chuzzles.Any(x => x.Current == bottomChuzzle.Current.Top)) { var topPart = GetHorizontalLineChuzzles(bottomChuzzle.Current.Top.Y, bottomChuzzle.Color, chuzzles).ToList(); if (topPart.Any()) { var possibleCombination = new List<Chuzzle>(); possibleCombination.AddRange(combination); possibleCombination.AddRange(topPart); //Debug.Log("Combination 5"); //var target = possibleCombination.Aggregate("", (current, chuzzle) => current + (chuzzle + Environment.NewLine)); //Debug.Log(target); isHorizontalMove = new IntVector2(bottomChuzzle.Current.X, bottomChuzzle.Current.Top.Y); chuzzleToMove = topPart.First(); return possibleCombination; } } //try bottom if (topChuzzle.Current.Bottom != null && topChuzzle.Current.Bottom.Type != CellTypes.Block && chuzzles.Any(x => x.Current == topChuzzle.Current.Bottom)) { var bottomPart = GetHorizontalLineChuzzles(topChuzzle.Current.Bottom.Y, topChuzzle.Color, chuzzles).ToList(); if (bottomPart.Any()) { var possibleCombination = new List<Chuzzle>(); possibleCombination.AddRange(combination); possibleCombination.AddRange(bottomPart); //Debug.Log("Combination 6"); //var target = possibleCombination.Aggregate("", (current, chuzzle) => current + (chuzzle + Environment.NewLine)); //Debug.Log(target); isHorizontalMove = new IntVector2(bottomChuzzle.Current.X, bottomChuzzle.Current.Bottom.Y); chuzzleToMove = bottomPart.First(); return possibleCombination; } } } else { combination.Sort(CompareByX); var leftChuzzle = combination[0]; var rightChuzzle = combination[1]; //horizontal combinations //try left if (leftChuzzle.Current.Left != null && leftChuzzle.Current.Left.Type != CellTypes.Block) { var leftPart = GetVerticalLineChuzzles(leftChuzzle.Current.Left.X, leftChuzzle.Color, chuzzles).ToList(); if (leftPart.Any()) { var possibleCombination = new List<Chuzzle>(); possibleCombination.AddRange(combination); possibleCombination.AddRange(leftPart); // //Debug.Log("Left:"+leftChuzzle); // //Debug.Log("Right:"+rightChuzzle.ToString()); //Debug.Log("Combination 7"); //var target = possibleCombination.Aggregate("", (current, chuzzle) => current + (chuzzle + Environment.NewLine)); //Debug.Log(target); isHorizontalMove = new IntVector2(leftChuzzle.Current.Left.X, leftChuzzle.Current.Y); chuzzleToMove = leftPart.First(); return possibleCombination; } } //try right if (rightChuzzle.Current.Right != null && rightChuzzle.Current.Right.Type != CellTypes.Block) { var rightPart = GetVerticalLineChuzzles(rightChuzzle.Current.X + 1, rightChuzzle.Color, chuzzles).ToList(); if (rightPart.Any()) { var possibleCombination = new List<Chuzzle>(); possibleCombination.AddRange(combination); possibleCombination.AddRange(rightPart); //Debug.Log("Combination 8"); //var target = possibleCombination.Aggregate("", (current, chuzzle) => current + (chuzzle + Environment.NewLine)); //Debug.Log(target); isHorizontalMove = new IntVector2(rightChuzzle.Current.X + 1, rightChuzzle.Current.Y); chuzzleToMove = rightPart.First(); return possibleCombination; } } //try top if ( (leftChuzzle.Current.Top != null && leftChuzzle.Current.Top.Type != CellTypes.Block && chuzzles.Any(x => x.Current == leftChuzzle.Current.Top)) || (rightChuzzle.Current.Top != null && rightChuzzle.Current.Top.Type != CellTypes.Block && chuzzles.Any(x => x.Current == rightChuzzle.Current.Top)) ) { var topPart = GetHorizontalLineChuzzles(rightChuzzle.Current.Y + 1, rightChuzzle.Color, chuzzles).ToList(); if (topPart.Any()) { var possibleCombination = new List<Chuzzle>(); possibleCombination.AddRange(combination); possibleCombination.AddRange(topPart); //Debug.Log("Combination 9"); //var target = possibleCombination.Aggregate("", (current, chuzzle) => current + (chuzzle + Environment.NewLine)); //Debug.Log(target); isHorizontalMove = new IntVector2(rightChuzzle.Current.X, rightChuzzle.Current.Y + 1); chuzzleToMove = topPart.First(); return possibleCombination; } } //try bottom if ( (leftChuzzle.Current.Bottom != null && leftChuzzle.Current.Bottom.Type != CellTypes.Block && chuzzles.Any(x => x.Current == leftChuzzle.Current.Bottom)) || (rightChuzzle.Current.Bottom != null && rightChuzzle.Current.Bottom.Type != CellTypes.Block && chuzzles.Any(x => x.Current == rightChuzzle.Current.Bottom)) ) { var bottomPart = GetHorizontalLineChuzzles(leftChuzzle.Current.Y - 1, leftChuzzle.Color, chuzzles).ToList(); if (bottomPart.Any()) { var possibleCombination = new List<Chuzzle>(); possibleCombination.AddRange(combination); possibleCombination.AddRange(bottomPart); //Debug.Log("Combination 10"); //var target = possibleCombination.Aggregate("", (current, chuzzle) => current + (chuzzle + Environment.NewLine)); //Debug.Log(target); isHorizontalMove = new IntVector2(leftChuzzle.Current.X, leftChuzzle.Current.Y - 1); chuzzleToMove = bottomPart.First(); return possibleCombination; } } } } //Debug.Log("Combination NOOOOOOOOOO 11"); Repaint(chuzzles,100); Tip(chuzzles, out isHorizontalMove, out chuzzleToMove); isHorizontalMove = new IntVector2(); chuzzleToMove = null; return new List<Chuzzle>(); }
private void CheckPossibleCombinations() { _targetPosition = null; _arrowChuzzle = null; int numberOfTries = 0; do { if (GamefieldUtility.Repaint(Gamefield.Level.Chuzzles, numberOfTries)) break; _possibleCombination = GamefieldUtility.Tip(Gamefield.Level.Chuzzles, out _targetPosition, out _arrowChuzzle); // Debug.Log(string.Format("Tip. From: {0} To: {1}", _arrowChuzzle, _targetPosition)); numberOfTries++; } while (!_possibleCombination.Any()); }
public static void ShowArrow(Chuzzle from, IntVector2 to, TipArrow tipArrow) { //Debug.Log(string.Format("Arrow. From:{0} To:{1} ", from, to)); if (@from.Current.X == to.x) { //vertical if (@from.Current.Y >= to.y) { //to down //do nothing tipArrow.transform.rotation = Quaternion.Euler(0, 0, 0); } else { //to up //mirror vertical tipArrow.transform.rotation = Quaternion.Euler(0, 0, 180); } } else { //horizontal if (@from.Current.X < to.x) { //to right tipArrow.transform.rotation = Quaternion.Euler(0, 0, 90); } else { //to left //to right tipArrow.transform.rotation = Quaternion.Euler(0, 0, -90); } } tipArrow.Chuzzle = @from; }