private void InstallPegs()
        {
            int pos = 0;

            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    if (level[i, j] == 1)
                    {
                        if (i != 3 || j != 3)
                        {
                            PegControl peg = peglist[pos];

                            peg.XK = i;
                            peg.YK = j;

                            peg.Visibility = Visibility.Visible;
                            peg.ChangeColor(false);

                            peg.SetValue(Grid.RowProperty, i);
                            peg.SetValue(Grid.ColumnProperty, j);


                            pegs[i, j] = peg;

                            pos++;
                        }
                    }
                }
            }
        }
        private void StartGame()
        {
            Init();

            InstallPegs();
            clickedPeg = null;

            TextBlockMessage.Text = "";
        }
        private void CreatePegs()
        {
            for (int i = 0; i < 32; i++)
            {
                PegControl peg = new PegControl();

                peglist.Add(peg);
                peg.MouseLeftButtonDown += new MouseButtonEventHandler(p_MouseLeftButtonDown);

                GridBoard.Children.Add(peg);
            }
        }
        void p_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            PegControl peg = sender as PegControl;

            if (clickedPeg == null)
            {
                peg.ChangeColor(true);

                clickedPeg = peg;
            }
            else
            {
                if (clickedPeg == peg)
                {
                    peg.ChangeColor(false);

                    clickedPeg = null;
                }
            }
        }
        void rec_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            CheckGame();

            if (clickedPeg != null)
            {
                TableControl rec = sender as TableControl;

                if (pegs[rec.XK, rec.YK] == null)
                {
                    if ((clickedPeg.XK == rec.XK && Math.Abs(clickedPeg.YK - rec.YK) == 2) ||
                        (clickedPeg.YK == rec.YK && Math.Abs(clickedPeg.XK - rec.XK) == 2))
                    {
                        int kx = (clickedPeg.XK + rec.XK) / 2;
                        int ky = (clickedPeg.YK + rec.YK) / 2;

                        if (pegs[kx, ky] != null)
                        {
                            pegs[clickedPeg.XK, clickedPeg.YK] = null;

                            pegs[kx, ky].Visibility = Visibility.Collapsed;
                            pegs[kx, ky]            = null;

                            clickedPeg.XK = rec.XK;
                            clickedPeg.YK = rec.YK;

                            pegs[rec.XK, rec.YK] = clickedPeg;

                            clickedPeg.SetValue(Grid.RowProperty, rec.XK);
                            clickedPeg.SetValue(Grid.ColumnProperty, rec.YK);

                            clickedPeg.ChangeColor(false);

                            clickedPeg = null;
                        }
                    }
                }
            }

            CheckGame();
        }