public void GenerateGameOverWindow2() { var BoardIter = new TilesCollection(); for (int i = 0; i < 20; i++) { for (int y = 0; y < 20; y++) { BoardIter[i, y] = TilesSet.GetTile(i, y); } } AbstractIterator iter = BoardIter.CreateIterator(); object item = iter.First(); while (item != null) { Rectangle ColorBlock = new Rectangle(); ColorBlock.Name = ""; ColorBlock.Fill = Brushes.Black; Tile tile = (Tile)item; Grid.SetColumn(ColorBlock, tile.x); Grid.SetRow(ColorBlock, tile.y); gridMain.Children.Add(ColorBlock); item = iter.Next(); } }
public ReadOnlyTilesCollection(TilesCollection parent) : this(parent.RowCount, parent.ColumnCount) { for (int row = 0; row < RowCount; row++) { for (int col = 0; col < ColumnCount; col++) { Tiles[row][col] = parent[row, col]; } } }
public static void Save(Tile[] tiles, int score) { if (tiles != null) { try { TilesCollection TS = new TilesCollection(); TS.tsd = new TileSaveData[tiles.Length]; for (int i = 0; i < tiles.Length; i++) { TS.tsd[i] = new TileSaveData(); TS.tsd[i].i = i; TS.tsd[i].value = tiles[i].Value; } TS.score = score; HighScoreData hsd = new HighScoreData(); hsd.highScore = score > highScore ? score : highScore; string saveJSON = UnityEngine.JsonUtility.ToJson(TS, true); string highJSON = UnityEngine.JsonUtility.ToJson(hsd); if (!File.Exists(Application.persistentDataPath + "/save.json")) { File.Create(Application.persistentDataPath + "/save.json"); } if (!File.Exists(Application.persistentDataPath + "/score.json")) { File.Create(Application.persistentDataPath + "/score.json"); } StreamWriter sr; if (score > highScore) { sr = new StreamWriter(Application.persistentDataPath + "/score.json"); sr.Write(highJSON); sr.Close(); } sr = new StreamWriter(Application.persistentDataPath + "/save.json"); sr.Write(saveJSON); sr.Close(); } catch { return; } } else { try { File.Delete(Application.persistentDataPath + "/save.json"); } catch { } } }
public BoardIterator(TilesCollection aggregate, bool even = false) { _aggregate = aggregate; _even = even; if (even) { _currentx = 0; _currenty = 0; } }
public static bool Load(out Tile[] tiles, out int score) { try { loadHighScore(); tiles = null; score = 0; if (File.Exists(Application.persistentDataPath + "/save.json")) { StreamReader sr = new StreamReader(Application.persistentDataPath + "/save.json"); string loadJSON = sr.ReadToEnd(); sr.Close(); if (loadJSON == "") { File.Delete(Application.persistentDataPath + "/save.json"); return(false); } TilesCollection TS = UnityEngine.JsonUtility.FromJson <TilesCollection>(loadJSON); tiles = new Tile[TS.tsd.Length]; for (int i = 0; i < TS.tsd.Length; i++) { tiles[i] = new Tile(); tiles[i].Value = TS.tsd[i].value; tiles[i].Position = TS.tsd[i].i; } score = TS.score; return(true); } else { return(false); } } catch { score = 0; tiles = null; return(false); } }
/// <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>(); }
public static List<List<Chuzzle>> SelectedTips(TilesCollection chuzzles, List<Chuzzle> selectedChuzzles, int combinationSize) { var combinations = new List<List<Chuzzle>>(); foreach (var c in chuzzles) { c.IsCheckedForSearch = false; } //find combination foreach (var c in selectedChuzzles) { if (c.IsCheckedForSearch) continue; var combination = RecursiveFind(c, new List<Chuzzle>(), chuzzles); if (combination.Count() >= combinationSize) { combinations.Add(combination); } } foreach (var c in chuzzles) { c.IsCheckedForSearch = false; } return combinations; }
public static bool Repaint(TilesCollection tiles,int numberOfTries) { if (numberOfTries == 0) { return false; } var possible = tiles.Where(IsUsual); //.GetTiles(IsUsual); //complex logic of repainting //check number of invaders //if more then third of maximum and possible less then 10 if (InvaderChuzzle.AllInvaderChuzzles.Count > InvaderChuzzle.MaxInvadersOnLevel/3 && possible.Count() < 10) { ////Debug.Log("Repaint invaders"); var invadersForRepaint = InvaderChuzzle.AllInvaderChuzzles.Where( x => InvaderChuzzle.AllInvaderChuzzles.IndexOf(x) < InvaderChuzzle.MaxInvadersOnLevel/3).ToArray(); //repaint them to random color foreach (var invaderToReplace in invadersForRepaint) { Instance.TilesFactory.ReplaceWithRandom(invaderToReplace); } //TODO show message to player } else { //try to find pair and repaint only one var combinations = FindCombinations(tiles, 2); if (combinations.Any()) { foreach (var comb in combinations) { //if vertical if (comb[0].Current.X == comb[1].Current.X) { //try to find up and bottom var top = comb[0].Current.Y > comb[1].Current.Y ? comb[0] : comb[1]; var bottom = top == comb[0] ? comb[1] : comb[0]; var repainted = false; if (top.Current.Top != null && top.Current.Top.Type != CellTypes.Block) { var possibleAbove = tiles.Where( x => IsUsual(x) && x.Current.Y == top.Current.Top.Y && x.Current != top.Current.Top) .ToArray(); if (possibleAbove.Any()) { Chuzzle toReplace = possibleAbove[Random.Range(0, possibleAbove.Length)]; Instance.TilesFactory.ReplaceWithColor(toReplace, top.Color); // //Debug.Log("Repaint above pair"); repainted = true; } } if (bottom.Current.Bottom != null && bottom.Current.Bottom.Type != CellTypes.Block && !repainted) { var possibleBellow = tiles.Where( x => IsUsual(x) && x.Current.Y == bottom.Current.Bottom.Y && x.Current != bottom.Current.Bottom) .ToArray(); if (possibleBellow.Any()) { Chuzzle toReplace = possibleBellow[Random.Range(0, possibleBellow.Length)]; Instance.TilesFactory.ReplaceWithColor( toReplace, bottom.Color); repainted = true; // //Debug.Log("Repaint bellow pair"); } } if (repainted) { break; } } else { //horizontal pair var left = comb[0].Current.X < comb[1].Current.X ? comb[0] : comb[1]; var right = comb[0] == left ? comb[1] : comb[0]; var repainted = false; //left? if (left.Current.Left != null && left.Current.Left.Type != CellTypes.Block) { var possibleLeft = tiles.Where( x => IsUsual(x) && x.Current.X == left.Current.Left.X && x.Current != left.Current.Left) .ToArray(); if (possibleLeft.Any()) { Chuzzle toReplace = possibleLeft[Random.Range(0, possibleLeft.Length)]; Instance.TilesFactory.ReplaceWithColor(toReplace, left.Color); // //Debug.Log("Repaint left pair"); repainted = true; } } //right? if (right.Current.Right != null && right.Current.Right.Type != CellTypes.Block && !repainted) { var possibleRight = tiles.Where( x => IsUsual(x) && x.Current.X == right.Current.Right.X && x.Current != right.Current.Right) .ToArray(); if (possibleRight.Any()) { Chuzzle toReplace = possibleRight[Random.Range(0, possibleRight.Length)]; Instance.TilesFactory.ReplaceWithColor( toReplace, right.Color); repainted = true; // //Debug.Log("Repaint right pair"); } } if (repainted) { break; } } } } else { //if now pairs if (numberOfTries > 5) { //create combination guaranteed //get random usual cell var possibleCellsLeftLeft = Instance.TilesFactory.Gamefield.Level.Cells.GetCells().Where( cell => //слева есть обычная клетка и слева слева тоже есть обычная клетка и в стобце левее левого есть еще одна обычная клетка и в ней находится обычный тайл (cell.Left != null && cell.Left.Type == CellTypes.Usual && (cell.Left.Left != null && cell.Left.Left.Type == CellTypes.Usual && Instance.TilesFactory.Gamefield.Level.Cells.GetCells().Count(x => x.X == cell.Left.Left.X && x.Type == CellTypes.Usual) > 1) && tiles.FirstOrDefault(chuzzle => chuzzle.Current == cell) != null && IsUsual(tiles.FirstOrDefault(chuzzle => chuzzle.Current == cell))) ).ToList(); if (possibleCellsLeftLeft.Any()) { var randomLeftLeft = possibleCellsLeftLeft[Random.Range(0, possibleCellsLeftLeft.Count)]; var randomLeftLeftChuzzle = GetChuzzleInCell(randomLeftLeft, tiles); Instance.TilesFactory.ReplaceWithColor( GetChuzzleInCell(randomLeftLeft.Left, tiles), randomLeftLeftChuzzle.Color); var possibleLeftLeftLeft = tiles.Where( x => x.Current != randomLeftLeft.Left.Left && x.Current.X == randomLeftLeft.Left.Left.X && (IsUsual(x) || !IsOrdinaryDestroyable(x))) .ToList(); Instance.TilesFactory.ReplaceWithColor( possibleLeftLeftLeft[Random.Range(0, possibleLeftLeftLeft.Count)], randomLeftLeftChuzzle.Color); //Debug.Log("Random left left"); } else { //Debug.LogWarning("All our life is a lie"); return true; } //check if has free neighbour //repaint to same color //repaint random tile in near row to same color } else { var possibleChuzzles = tiles.Where(IsUsual).ToArray(); for (int index = 0; index < possibleChuzzles.Length; index++) { var possibleChuzzle = possibleChuzzles[index]; Instance.TilesFactory.ReplaceWithRandom(possibleChuzzle); } } } } //if create combination - repaint random var combination = FindOnlyOneCombination(tiles); if (combination.Any()) { foreach (var chuzzle in combination) { if (IsUsual(chuzzle)) { ////Debug.Log("Oops, combination. Repaint"); Instance.TilesFactory.ReplaceWithOtherColor(chuzzle); break; } } } return false; }
/// <summary> /// Try to find any combination of chuzzles from chuzzles with length more then combinationSize /// </summary> /// <param name="chuzzles">list of chuzzle to find in</param> /// <param name="combinationSize">required combination size</param> /// <returns></returns> public static List<Chuzzle> FindOnlyOneCombination(TilesCollection chuzzles, int combinationSize = 3) { foreach (var c in chuzzles) { c.IsCheckedForSearch = false; } //find combination foreach (var c in chuzzles) { if (c.IsCheckedForSearch) continue; var combination = RecursiveFind(c, new List<Chuzzle>(), chuzzles); if (combination.Count() >= combinationSize) { return combination; } } foreach (var c in chuzzles) { c.IsCheckedForSearch = false; } return new List<Chuzzle>(); }