Ejemplo n.º 1
0
        private int NumberStop = 0;                  // số lượng vật thể không thể đến đích
        private void VatDiChuyen(object ViTriVatThe) // hàm vẽ và xoá vị trí tiếp theo và hiện tại, tạo hiệu ứng chuyển động
        {
            Point    point = (Point)ViTriVatThe;
            Graphics gra   = BanCo.CreateGraphics();

            while (true)
            {
                int Row    = point.Y / Cao;
                int Column = point.X / Rong;
                if (MouseDowning && (SetObject_Clicked || DelOne)) // nếu người dùng đang đặt vật cản thì tạm dừng vật di chuyển...
                {
                    Thread.Sleep(500);                             // ...trong nửa giây
                    continue;
                }
                else
                {
                    if (main.ToaDo[Row, Column] == StartPos)
                    {
                        Number++;
                        main.OCo[Row, Column].MauOCo = MauBanCo;
                        // tổng số lượng vật thể đã đến đích và không thể đến đích sẽ bằng số lượng vật thể người dùng đặt
                        if (Number + NumberStop == (form2.Automatic ? form2.Soluong : ArrayEndPos.Count))
                        {
                            Invoke(new Action(() => LamMoi.Enabled = GreedyFS.Enabled = AStart.Enabled = true)); // gọi hàm dẫn control
                            MessageBox.Show("Có " + Number.ToString() + " vật đã đến đích!!", "Đến đích", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Number = NumberStop = 0;                                                             // reset lại giá trị
                            return;
                        }
                        return;
                    }

                    if (main.OCo[main.OCo[Row, Column].Goc.Y / Cao, main.OCo[Row, Column].Goc.X / Rong].MauOCo != Color.DarkOrange)
                    {
                        if (main.OCo[Row, Column].Goc != new Point(-1, -1))
                        {
                            // xoá ô hiện tại
                            main.VeOCo(gra, main.ToaDo[Row, Column].X, main.ToaDo[Row, Column].Y, MauBanCo, true);
                            main.OCo[Row, Column].MauOCo = MauBanCo;
                            // vẽ ô tiếp theo dựa vào gốc của ô cờ
                            main.VeOCo(gra, main.OCo[Row, Column].Goc.X, main.OCo[Row, Column].Goc.Y, Color.DarkOrange, true);
                            point = main.OCo[Row, Column].Goc;
                            main.OCo[point.Y / Cao, point.X / Rong].MauOCo = Color.DarkOrange;
                            Thread.Sleep(100);
                        }
                        else // trường hợp vật không tìm thấy đường đi đến đích thì thoát ra...
                        {
                            NumberStop++; // ... và tăng biến NumberStop lến 1 đơn vị
                            return;
                        }
                    }
                    else if (main.OCo[main.OCo[Row, Column].Goc.Y / Cao, main.OCo[Row, Column].Goc.X / Rong].MauOCo == Color.DarkOrange)
                    {
                        Thread.Sleep(200);
                    }
                }
            }
        }
Ejemplo n.º 2
0
 public Form1()
 {
     InitializeComponent();
     main           = new Main();
     form2          = new Form2();
     grs            = BanCo.CreateGraphics();
     ArrayEndPos    = new List <Point>();
     DanhSachVatCan = new List <Point>();
 }