Exemplo n.º 1
0
        public void SetColor()
        {
            switch (thisCubeState)
            {
            case CubeState.Start:
                this.BackColor = Color.Blue;
                if (Btn_start != null)
                {
                    Btn_start.thisCubeState = CubeState.Null;
                    Btn_start.SetColor();
                    Btn_start = this;
                }
                else
                {
                    Btn_start = this;
                }
                return;

            case CubeState.End:
                if (Btn_end != null)
                {
                    Btn_end.thisCubeState = CubeState.Null;
                    Btn_end.SetColor();
                    Btn_end = this;
                }
                else
                {
                    if (Btn_start == this)
                    {
                        Btn_start = null;
                    }
                    Btn_end = this;
                }
                this.BackColor = Color.Red;
                return;

            case CubeState.Null:
                this.BackColor = Color.Gray;
                break;

            case CubeState.Block:
                this.BackColor = Color.Black;
                break;

            case CubeState.Path:
                this.BackColor = Color.White;
                break;
            }
            if (Btn_start == this && thisCubeState != CubeState.Start)
            {
                Btn_start = null;
            }
            if (Btn_end == this && thisCubeState != CubeState.End)
            {
                Btn_end = null;
            }
        }
Exemplo n.º 2
0
 private void FindPath(MyBtn Node)
 {
     if (Node == MyBtn.Btn_start)
     {
         Path.Insert(0, Node);
         for (int i = 1; i < Path.Count - 1; i++)
         {
             Path[i].thisCubeState = CubeState.Path;
             Path[i].SetColor();
         }
         return;
     }
     Path.Insert(0, Node);
     FindPath(Node.ParentNode);
 }
Exemplo n.º 3
0
 private void button1_Click(object sender, EventArgs e)
 {
     if (!Int32.TryParse(this.txt_Height.Text, out height))
     {
         MessageBox.Show("请输入正确高度");
         return;
     }
     if (!Int32.TryParse(this.txt_width.Text, out width))
     {
         MessageBox.Show("请输入正确宽度");
         return;
     }
     if (!Int32.TryParse(this.txt_wadx.Text, out wadx))
     {
         MessageBox.Show("请输入正确权重");
         return;
     }
     if (!Int32.TryParse(this.txt_qezc.Text, out qezc))
     {
         MessageBox.Show("请输入正确权重");
         return;
     }
     if (this.MapPannel.Controls.Count != 0)
     {
         foreach (Control i in this.MapPannel.Controls)
         {
             i.Dispose();
         }
         this.MapPannel.Controls.Clear();
     }
     for (int x = 0; x < width; x++)
     {
         for (int y = 0; y < height; y++)
         {
             MyBtn _btn = new MyBtn();
             _btn.Tag      = new Point(x, y);
             _btn.BtnPoint = new Point(x, y);
             _btn.Location = new Point(x * 25, y * 25);
             _btn.Enabled  = true;
             _btn.Visible  = true;
             this.MapPannel.Controls.Add(_btn);
         }
     }
     this.Size            = new Size(240 + width * 25, Math.Max(340, height * 25 + 50));
     this.button2.Enabled = true;
     Console.WriteLine();
 }
Exemplo n.º 4
0
        private void CaculatePath(List <MyBtn> OpenList, List <MyBtn> CloseList, MyBtn Node)
        {
            Point nodepoint = Node.BtnPoint;
            Point endPoint  = MyBtn.Btn_end.BtnPoint;

            CloseList.Add(Node);

            List <MyBtn> BlockList = OpenList.Where(btn => btn.thisCubeState == CubeState.Block).ToList();

            OpenList.RemoveAll(btn => BlockList.Contains(btn) || CloseList.Contains(btn)
                               ||
                               BlockList.Where(a =>
                                               Math.Pow(a.BtnPoint.X - btn.BtnPoint.X, 2) + Math.Pow(a.BtnPoint.Y - btn.BtnPoint.Y, 2) == 1
                                               &&
                                               Math.Pow(Node.BtnPoint.X - a.BtnPoint.X, 2) + Math.Pow(Node.BtnPoint.Y - a.BtnPoint.Y, 2) == 1
                                               )
                               .ToList().Count != 0);

            if (OpenList.Contains(MyBtn.Btn_end))
            {
                MyBtn.Btn_end.ParentNode = Node;
                FindPath(MyBtn.Btn_end);
                return;
            }

            if (OpenList.Count == 0)
            {
                if (Node == MyBtn.Btn_start)
                {
                    return;
                }

                OpenList = (from btn in BtnList
                            where (
                                Math.Abs(btn.BtnPoint.X - Node.ParentNode.BtnPoint.X) <= 1
                                &&
                                Math.Abs(btn.BtnPoint.Y - Node.ParentNode.BtnPoint.Y) <= 1
                                &&
                                ((Point)btn.Tag) != ((Point)Node.ParentNode.Tag)
                                )
                            select btn).ToList();
                CaculatePath(OpenList, CloseList, Node.ParentNode);
                return;
            }

            foreach (MyBtn btn in OpenList)
            {
                Point btnPoint = btn.BtnPoint;
                if (btn.H == 0)
                {
                    btn.H = (Math.Abs(endPoint.Y - btnPoint.Y) + Math.Abs(endPoint.X - btnPoint.X)) * wadx;
                }
                if (btn.ParentNode == null)
                {
                    btn.ParentNode = Node;
                }

                Point parentNodePoint = btn.ParentNode.BtnPoint;

                if (btn.G == 0)
                {
                    if (Math.Pow((parentNodePoint.X - btnPoint.X), 2) + Math.Pow((parentNodePoint.Y - btnPoint.Y), 2) == 2)
                    {
                        btn.G = qezc + btn.ParentNode.G;
                    }
                    else
                    {
                        btn.G = wadx + btn.ParentNode.G;
                    }
                }
                else
                {
                    if (btn.ParentNode != Node)
                    {
                        int G = 0;
                        if (Math.Pow((nodepoint.X - btnPoint.X), 2) + Math.Pow((nodepoint.Y - btnPoint.Y), 2) == 2)
                        {
                            G = qezc + Node.G;
                        }
                        else
                        {
                            G = wadx + Node.G;
                        }
                        if (G < btn.G)
                        {
                            btn.G          = G;
                            btn.ParentNode = Node;
                        }
                    }
                }
            }
            int   MinF    = OpenList.Min(a => a.F);
            MyBtn newNode = OpenList.FindLast(btn => btn.F == MinF);

            OpenList = (from btn in BtnList
                        where (
                            Math.Abs(btn.BtnPoint.X - newNode.BtnPoint.X) <= 1
                            &&
                            Math.Abs(btn.BtnPoint.Y - newNode.BtnPoint.Y) <= 1
                            &&
                            ((Point)btn.Tag) != ((Point)newNode.Tag)
                            )
                        select btn).ToList();
            CaculatePath(OpenList, CloseList, newNode);
        }