public void Reload() { LockKeyPress = true; var animation = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(0.2))); animation.Completed += (object sender, EventArgs e) => { Menu.Visibility = Visibility.Hidden; Fill.Visibility = Visibility.Hidden; LockKeyPress = false; }; Menu.BeginAnimation(OpacityProperty, animation); Fill.BeginAnimation(OpacityProperty, animation); Moved = false; FilledCells = 0; Score = 0; AnimatedCells = 0; Cells = new Cell[4, 4]; Playground.Children.Clear(); CreateGrid(); CreateRandomCell(); CreateRandomCell(); UpdateGrid(); }
public void Move(int x, int y, Direction dir) { Point StartPos = new Point(x, y); Point EndPos = new Point(x, y); var val = Cells[x, y].Value; var color = Cells[x, y].Fill.Fill; int xOffset = 0; int yOffset = 0; if (dir == Direction.Left) { xOffset = -1; if (x == 0) { return; } } else if (dir == Direction.Right) { xOffset = 1; if (x == CellsCount - 1) { return; } } else if (dir == Direction.Up) { yOffset = -1; if (y == 0) { return; } } else if (dir == Direction.Down) { yOffset = 1; if (y == CellsCount - 1) { return; } } if (Cells[x + xOffset, y + yOffset].Value == Cells[x, y].Value && !Cells[x + xOffset, y + yOffset].Joined && !Cells[x, y].Joined && Cells[x, y].Value != 0) { var value = Cells[x, y].Value; Cells[x, y].Value = 0; Cells[x + xOffset, y + yOffset].Value = value * 2; Score += value * 2; EndPos.X = x + xOffset; EndPos.Y = y + yOffset; Cells[x + xOffset, y + yOffset].Joined = true; if (value > 0) { Moved = true; } } else { bool end = false; for (; !end;) { if (Cells[x + xOffset, y + yOffset].Value == 0 && Cells[x, y].Value != 0) { var value = Cells[x, y].Value; Cells[x, y].Value = 0; Cells[x + xOffset, y + yOffset].Value = value; if (Cells[x, y].Joined) { Cells[x, y].Joined = false; Cells[x + xOffset, y + yOffset].Joined = true; } if (!Cells[x, y].Joined) { Cells[x + xOffset, y + yOffset].Joined = false; } EndPos.X = x + xOffset; EndPos.Y = y + yOffset; x += xOffset; y += yOffset; if (value > 0) { Moved = true; } ; } else { if (Cells[x + xOffset, y + yOffset].Value == Cells[x, y].Value && !Cells[x + xOffset, y + yOffset].Joined && !Cells[x, y].Joined && Cells[x, y].Value != 0) { var value = Cells[x, y].Value; Cells[x, y].Value = 0; Cells[x + xOffset, y + yOffset].Value = value * 2; Score += value * 2; EndPos.X = x + xOffset; EndPos.Y = y + yOffset; Cells[x + xOffset, y + yOffset].Joined = true; if (value > 0) { Moved = true; } } break; } if (dir == Direction.Left && x <= 0) { end = true; } else if (dir == Direction.Right && x >= CellsCount - 1) { end = true; } else if (dir == Direction.Up && y <= 0) { end = true; } else if (dir == Direction.Down && y >= CellsCount - 1) { end = true; } } } var width = CanvasSize.Width - 2 * BlockMargin; var height = CanvasSize.Height - 2 * BlockMargin; var cellWidth = width / CellsCount - (CellsCount - 1.0) / CellsCount * BlockMargin; var cellHeight = height / CellsCount - (CellsCount - 1.0) / CellsCount * BlockMargin; var x1 = StartPos.X * cellWidth + BlockMargin * (StartPos.X + 1); var y1 = StartPos.Y * cellWidth + BlockMargin * (StartPos.Y + 1); var x2 = EndPos.X * cellWidth + BlockMargin * (EndPos.X + 1); var y2 = EndPos.Y * cellWidth + BlockMargin * (EndPos.Y + 1); var itteration = 0; var itterations = 10; var cellFill = new Rectangle(); cellFill.Fill = GetColor(val); cellFill.Width = cellWidth; cellFill.Height = cellHeight; cellFill.SetValue(LeftProperty, (x2 - x1) * itteration / (double)itterations + x1); cellFill.SetValue(TopProperty, (y2 - y1) * itteration / (double)itterations + y1); cellFill.RadiusX = BorderRadius; cellFill.RadiusY = BorderRadius; var label = new Label(); label.Content = val.ToString(); var viewBox = new Viewbox(); label.Foreground = ((Label)Cells[(int)StartPos.X, (int)StartPos.Y].Content.Child).Foreground; viewBox.Child = label; viewBox.Width = cellWidth; viewBox.Height = cellHeight; viewBox.SetValue(LeftProperty, (x2 - x1) * itteration / (double)itterations + x1); viewBox.SetValue(TopProperty, (y2 - y1) * itteration / (double)itterations + y1); if (val != 0) { AnimatedCells++; Playground.Children.Add(cellFill); Playground.Children.Add(viewBox); var animationX = new DoubleAnimation(x1, x2, new Duration(TimeSpan.FromSeconds(0.2))); var animationY = new DoubleAnimation(y1, y2, new Duration(TimeSpan.FromSeconds(0.2))); animationX.Completed += (object sender, EventArgs e) => { Playground.Children.Remove(cellFill); Playground.Children.Remove(viewBox); UpdateGrid((int)EndPos.X, (int)EndPos.Y); if (AnimatedCells >= FilledCells) { LockKeyPress = false; UpdateGrid(); } var soltionExists = CheckCells(); if (!soltionExists) { LockKeyPress = true; Menu.Visibility = Visibility.Visible; Fill.Visibility = Visibility.Visible; MenuMessage.Content = "Game over"; var animation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(0.2))); Menu.BeginAnimation(OpacityProperty, animation); Fill.BeginAnimation(OpacityProperty, animation); } foreach (var cell in Cells) { if (cell.Value == 2048) { LockKeyPress = true; Menu.Visibility = Visibility.Visible; Fill.Visibility = Visibility.Visible; MenuMessage.Content = "You win"; var animation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(0.2))); Menu.BeginAnimation(OpacityProperty, animation); Fill.BeginAnimation(OpacityProperty, animation); } } }; cellFill.BeginAnimation(LeftProperty, animationX); cellFill.BeginAnimation(TopProperty, animationY); viewBox.BeginAnimation(LeftProperty, animationX); viewBox.BeginAnimation(TopProperty, animationY); Cells[(int)StartPos.X, (int)StartPos.Y].Fill.Fill = ForeColor; ((Label)Cells[(int)StartPos.X, (int)StartPos.Y].Content.Child).Content = ""; } }