private void MoveItem(ItemUC5 it, int i, int j) { ItemUC5 a = new ItemUC5(1); a.I = 1; Board[i, j] = Board[it.I, it.J]; Board[it.I, it.J] = 0; Storyboard sb = new Storyboard(); DoubleAnimation da = new DoubleAnimation(); if (i == it.I) { da.From = it.J * it.Width; da.By = j * it.Width - da.From; } else { da.From = it.I * it.Height; da.By = i * it.Height - da.From; } da.Duration = new Duration(TimeSpan.FromSeconds(.2)); sb.Children.Add(da); object prop = it.I == i ? Canvas.LeftProperty : Canvas.TopProperty; Storyboard.SetTargetProperty(da, new PropertyPath(prop)); sb.Begin(it, true); sb.Completed += new EventHandler(sb_Completed); it.I = i; it.J = j; }
private void AutoSlideImage() { Random rnd = new Random(); ItemUC5 it = new ItemUC5(1); int temp; int count; bool flag = true; while (flag) { temp = 0; count = 1; for (int k = 0; k < 300; k++) { int i = rnd.Next(0, 4); int j = rnd.Next(0, 4); it.I = i; it.J = j; it.Width = 0; it.Height = 0; if (CheckMove(it.I - 1, it.J)) { MoveItem(it, it.I - 1, it.J); } else if (CheckMove(it.I, it.J + 1)) { MoveItem(it, it.I, it.J + 1); } else if (CheckMove(it.I + 1, it.J)) { MoveItem(it, it.I + 1, it.J); } else if (CheckMove(it.I, it.J - 1)) { MoveItem(it, it.I, it.J - 1); } } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (Board[i, j] == count) { temp++; } count++; } } if (temp < Global.level + 4) { flag = false; break; } flag = true; } }
void cnv_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { ItemUC5 it = (ItemUC5)sender; if (CheckMove(it.I - 1, it.J)) { MoveItem(it, it.I - 1, it.J); } else if (CheckMove(it.I, it.J + 1)) { MoveItem(it, it.I, it.J + 1); } else if (CheckMove(it.I + 1, it.J)) { MoveItem(it, it.I + 1, it.J); } else if (CheckMove(it.I, it.J - 1)) { MoveItem(it, it.I, it.J - 1); } if (CheckWin()) { Board[3, 3] = 16; ItemUC5 cnv = new ItemUC5(16); Canvas.SetTop(cnv, 330); Canvas.SetLeft(cnv, 330); cnBoard.Children.Add(cnv); UCWin uCWin = new UCWin(); uc.Children.Add(uCWin); Uri uri = new Uri("../../Sound/chucmung.mp3", UriKind.Relative); mediaPlayer.Open(uri); mediaPlayer.Play(); bantay.Visibility = Visibility.Visible; next.Visibility = Visibility.Visible; } }
private void DrawBord() { cnBoard.Children.Clear(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (Board[i, j] > 0) { ItemUC5 cnv = new ItemUC5(Board[i, j]) { Width = cnBoard.Width / 4, Height = cnBoard.Height / 4, I = i, J = j }; Canvas.SetTop(cnv, i * cnBoard.Height / 4); Canvas.SetLeft(cnv, j * cnBoard.Width / 4); cnBoard.Children.Add(cnv); cnv.MouseLeftButtonUp += new MouseButtonEventHandler(cnv_MouseLeftButtonUp); } } } }