Exemplo n.º 1
0
        private void gMaze_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (isEditing)
            {
                Point pt = e.GetPosition(gMaze);
                System.Drawing.Point p = new System.Drawing.Point((int)(pt.X / MCommon.MazeGridLenght), (int)(pt.Y / MCommon.MazeGridLenght));


                if (cs == CellState.obstacle)
                {
                    if (md.startPoint != p && md.endPoint != p)
                    {
                        //点击处不为起点和终点,变为障碍
                        md.MazeString[md.p2i(p)] = '0';
                        FillGridInvoke(p, MCommon.getCellColor(CellState.obstacle));
                    }
                }
                else if (cs == CellState.access)
                {
                    //变为通路
                    md.MazeString[md.p2i(p)] = '1';
                    FillGridInvoke(p, MCommon.getCellColor(CellState.access));
                }
                else if (md.MazeString[md.p2i(p)] == '1')
                {
                    //点击处为通路
                    if (cs == CellState.start)
                    {
                        //设置为起点
                        md.startPoint = p;
                        MoveToPoint(p, cStart);
                    }
                    else if (cs == CellState.end)
                    {
                        //设置为终点
                        md.endPoint = p;
                        MoveToPoint(p, cEnd);
                    }
                }
            }
        }
Exemplo n.º 2
0
        void initGrid()
        {
            if (listRect == null)
            {
                listRect = new List <Rectangle>();
            }
            else
            {
                listRect.Clear();
            }

            #region gMaze

            gMaze.Children.Clear();
            gMaze.ColumnDefinitions.Clear();
            gMaze.RowDefinitions.Clear();

            if (md == null)
            {
                return;
            }

            gMaze.Width  = md.Width * md.MazeGridLenght;
            gMaze.Height = md.Height * md.MazeGridLenght;


            for (int r = 0; r < md.Height; r++)
            {
                gMaze.RowDefinitions.Add(new RowDefinition());
            }

            for (int c = 0; c < md.Width; c++)
            {
                gMaze.ColumnDefinitions.Add(new ColumnDefinition());
            }

            System.Drawing.Point p = new System.Drawing.Point();
            for (p.Y = 0; p.Y < md.Height; p.Y++)
            {
                for (p.X = 0; p.X < md.Width; p.X++)
                {
                    Rectangle r = new Rectangle();
                    r.Height = r.Width = md.MazeGridLenght;
                    listRect.Add(r);
                    //r.StrokeThickness = 0;
                    if (md.MazeString[p.Y * md.Width + p.X] == '1')
                    {
                        r.Fill = MCommon.getCellColor(CellState.access);
                    }
                    else
                    {
                        r.Fill = MCommon.getCellColor(CellState.obstacle);
                    }

                    gMaze.Children.Add(r);

                    MoveToPoint(p, r);
                }
            }

            if (cStart == null || cEnd == null)
            {
                MazeControl m = new MazeControl();
                cStart = m.sStart;
                cEnd   = m.sStar;

                m.sPanel.Children.Remove(cStart);
                m.sPanel.Children.Remove(cEnd);
            }

            gMaze.Children.Add(cStart);
            gMaze.Children.Add(cEnd);

            #endregion


            tbMazeName.Text = string.IsNullOrWhiteSpace(md.MazeName) ? strMaze :
                              (md.MazeName + " (" + md.Height + " x " + md.Width + ") ");
            tbMazeNewName.Text = md.MazeName;
            tbMazeWidth.Text   = md.Width.ToString();
            tbMazeHeight.Text  = md.Height.ToString();

            autoShowGrid();
        }