using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace SilverlightApplication1 { public partial class ruudustik : UserControl { const int N = 4; const int D = 20; Button[,] kujundid = new Button[N, N]; public ruudustik() { InitializeComponent(); for (int rida = 0; rida < N; rida++) { for (int veerg = 0; veerg < N; veerg++) { if (!(rida == N - 1 && veerg == N - 1)) { Button kujund = new Button(); kujund.Content = rida * N + veerg + 1; kujundid[rida, veerg] = kujund; kujund.Width = D; kujund.Height = D; juur.Children.Add(kujund); Canvas.SetLeft(kujund, veerg * D); Canvas.SetTop(kujund, rida * D); kujund.Click += new RoutedEventHandler( kujund_Click); // kujund.MouseLeftButtonDown += // new MouseButtonEventHandler(kujund_MouseLeftButtonDown); } } } } bool prooviLiikuda(UIElement kujund, int vrida, int vveerg, int rida, int veerg){ if (rida < 0) { return false; } if (rida >= N) { return false; } if (veerg < 0) { return false; } if (veerg >= N) { return false; } if (kujundid[rida, veerg] == null) { Canvas.SetLeft(kujund, veerg * D); Canvas.SetTop(kujund, rida * D); kujundid[rida, veerg] = kujund as Button; kujundid[vrida, vveerg] = null; } return true; } void kujund_Click(object sender, RoutedEventArgs e) { UIElement kujund=sender as UIElement; int rida = (int)Math.Round(Canvas.GetTop(kujund) / D); int veerg = (int)Math.Round(Canvas.GetLeft(kujund) / D); prooviLiikuda(kujund, rida, veerg, rida + 1, veerg); prooviLiikuda(kujund, rida, veerg, rida - 1, veerg); prooviLiikuda(kujund, rida, veerg, rida, veerg+1); prooviLiikuda(kujund, rida, veerg, rida, veerg-1); } } }