private void GenerateRandom() { Random random = new Random(); for (var i = 0; i < Size; i++) { for (var j = 0; j < Size; j++) { CellControl cell = new CellControl(); cell.CellType = random.Next(5); cell.X = i; cell.Y = j; cell.OnSelected += OnCellSelected; Canvas.SetLeft(cell, i * 75); Canvas.SetTop(cell, j * 75); Cells.Add(cell); } } }
private void SwapCells(CellControl cell1, CellControl cell2, bool isBack = false) { cell1.OnMovingComplete += sender => { var newCell1 = new CellControl(); newCell1.X = cell1.X; newCell1.Y = cell1.Y; newCell1.OnSelected += OnCellSelected; newCell1.CellType = cell2.CellType; Canvas.SetLeft(newCell1, newCell1.X * 75); Canvas.SetTop(newCell1, newCell1.Y * 75); Cells[cell1.Y + Size * cell1.X] = newCell1; var newCell2 = new CellControl(); newCell2.X = cell2.X; newCell2.Y = cell2.Y; newCell2.OnSelected += OnCellSelected; newCell2.CellType = cell1.CellType; Canvas.SetLeft(newCell2, newCell2.X * 75); Canvas.SetTop(newCell2, newCell2.Y * 75); Cells[cell2.Y + Size * cell2.X] = newCell2; if (FindLinesAndRemove() == 0 && isBack == false) { SwapCells(Cells[cell1.Y + Size * cell1.X], Cells[cell2.Y + Size * cell2.X], true); } else { while (FindLinesAndRemove() != 0) { ; } } }; cell1.MoveTo(cell2.X, cell2.Y); cell2.MoveTo(cell1.X, cell1.Y); }
private void InsertCells() { Random random = new Random(); bool hasEmpty = true; while (hasEmpty) { hasEmpty = false; for (var i = 0; i < Size; i++) { for (var j = 0; j < Size; j++) { if (Cells[j + Size * i].Removed) { hasEmpty = true; if (j == 0) { Cells[Size * i] = new CellControl(); Cells[Size * i].CellType = random.Next(5); Cells[Size * i].X = i; Cells[Size * i].Y = j; Cells[Size * i].OnSelected += OnCellSelected; Cells[Size * i].MoveTo(i, j, i, -1); } else { Cells[j + Size * i] = new CellControl(); Cells[j + Size * i].CellType = Cells[j - 1 + Size * i].CellType; Cells[j + Size * i].X = i; Cells[j + Size * i].Y = j; Cells[j + Size * i].OnSelected += OnCellSelected; Cells[j + Size * i].MoveTo(i, j, i, j - 1); Cells[j - 1 + Size * i].Removed = true; } } } } } }