Example #1
0
 /// <summary>
 /// Color
 /// </summary>
 /// <param name="c"></param>
 /// <param name="control"></param>
 public void ChangeControlColor(Color c, LabelControl control)
 {
     if (control.InvokeRequired)
     {
         SetColorCallBack d = new SetColorCallBack(ChangeControlColor);
         control.Invoke(d, new object[] { c, control });
     }
     else
     {
         control.BackColor = c;
         control.BringToFront();
         control.Refresh();
     }
 }
Example #2
0
        public static void Execute(LabirintGraph graph, TableLayoutPanel panel, LabelControl first,
                                   LabelControl best)
        {
            var found = false;

            graph.Start.Mark = 0;
            if (graph.Size < maxSize)
            {
                index = graph.Vertices.IndexOf(graph.Start);
                if (graph.Size < 25)
                {
                    panel.Controls[index].Controls[0].Invoke((MethodInvoker) delegate
                    {
                        panel.Controls[index].Controls[0].Text = "0";
                    });
                }
            }

            var iterations = 0;

            while (!graph.Vertices.TrueForAll(v => v.Visited) && condition)
            {
                var query = from vert in graph.Vertices
                            where !vert.Visited
                            select vert;
                var curr = query.MinBy(s => s.Mark + graph.ManhattanToFinish(s));
                int tmp  = curr.Mark + graph.ManhattanToFinish(curr);
                if (curr.Mark > int.MaxValue - 100000)
                {
                    break;
                }
                foreach (var v in curr.Neighbours)
                {
                    if (v.Visited)
                    {
                        continue;
                    }
                    if (v.Mark > curr.Mark + 1)
                    {
                        v.Mark = curr.Mark + 1;
                        if (graph.Size < maxSize)
                        {
                            index = graph.Vertices.IndexOf(v);
                            if (graph.Size < 25)
                            {
                                panel.Controls[index].Controls[0].Invoke((MethodInvoker) delegate
                                {
                                    panel.Controls[index].Controls[0].Text = v.Mark.ToString();
                                });
                            }

                            if (v != graph.Finish)
                            {
                                panel.Controls[index].BackColor = GraphParser.Colors[5];
                            }
                        }

                        v.Parent = curr;
                    }

                    if (v == graph.Finish)
                    {
                        found = true;
                        tmp   = iterations;
                        tmp++;
                        dataFirst.Data = tmp;
                        first.Invoke((MethodInvoker) delegate { first.Text = tmp.ToString(); });
                    }
                }
                curr.Visited = true;
                if (curr == graph.Finish)
                {
                    condition = false;
                }

                if (panel == null || curr.NodeType != CellTypes.Empty)
                {
                    continue;
                }

                iterations++;

                if (graph.Size < maxSize)
                {
                    panel.Controls[graph.Vertices.IndexOf(curr)].BackColor = GraphParser.Colors[4];
                }


                if (first != null && !found)
                {
                    first.Invoke((MethodInvoker) delegate { first.Text = iterations.ToString(); });
                }

                best?.Invoke((MethodInvoker) delegate { best.Text = iterations.ToString(); });
                if (graph.Size < maxSize)
                {
                    Thread.Sleep(speed);
                }

                while (wait)
                {
                    ;
                }
            }

            dataBest.Data  = iterations;
            dataBest.Algo  = "AStar";
            dataFirst.Algo = "AStar";
            dataBest.Type  = "Best";
            dataFirst.Type = "First";

            if (graph.Size > maxSize)
            {
                return;
            }

            var built   = false;
            var current = graph.Finish;

            while (!built)
            {
                if (panel == null)
                {
                    continue;
                }
                if (current.Parent == graph.Start)
                {
                    built = true;
                    continue;
                }

                panel.Controls[graph.Vertices.IndexOf(current.Parent)].BackColor = Color.RoyalBlue;
                current = current.Parent;
            }
        }
Example #3
0
        public static void Execute(LabirintGraph graph, TableLayoutPanel panel = null, LabelControl first = null,
                                   LabelControl best = null)
        {
            var iterations = 0;
            var queue      = new Queue <LabirintVertex>();

            queue.Enqueue(graph.Start);
            while (queue.Count > 0 && !found)
            {
                var curr = queue.Dequeue();
                curr.Visited = true;
                var elems = curr.Neighbours.FindAll(v => !v.Visited);
                foreach (var elem in elems)
                {
                    if (!queue.Contains(elem))
                    {
                        queue.Enqueue(elem);
                        if (graph.Size < maxSize)
                        {
                            index = graph.Vertices.IndexOf(elem);
                            if (elem != graph.Finish && panel != null)
                            {
                                panel.Controls[index].BackColor = GraphParser.Colors[5];
                            }
                        }

                        elem.Parent = curr;
                    }
                }

                if (queue.Contains(graph.Finish))
                {
                    found = true;
                }

                if (panel == null || curr.NodeType != CellTypes.Empty)
                {
                    continue;
                }

                iterations++;

                if (first != null) //&& !found)
                {
                    dataFirst.Data = iterations;
                    first.Invoke((MethodInvoker) delegate { first.Text = iterations.ToString(); });
                }

                best?.Invoke((MethodInvoker) delegate { best.Text = iterations.ToString(); });
                if (graph.Size < maxSize)
                {
                    panel.Controls[graph.Vertices.IndexOf(curr)].BackColor = GraphParser.Colors[4];
                }

                if (graph.Size < maxSize)
                {
                    Thread.Sleep(speed);
                }

                while (wait)
                {
                    ;
                }
            }

            dataBest.Data  = iterations;
            dataBest.Algo  = "BreadthFirst";
            dataFirst.Algo = "BreadthFirst";
            dataBest.Type  = "Best";
            dataFirst.Type = "First";

            if (graph.Size > maxSize)
            {
                return;
            }

            var built   = false;
            var steps   = 1;
            var current = graph.Finish;

            while (!built)
            {
                if (panel == null)
                {
                    continue;
                }
                if (current.Parent == graph.Start)
                {
                    built = true;
                    continue;
                }

                panel.Controls[graph.Vertices.IndexOf(current.Parent)].BackColor = Color.RoyalBlue;
                current = current.Parent;
                steps++;
            }

            if (graph.Size < 25)
            {
                built   = false;
                current = graph.Finish;
                while (!built)
                {
                    if (current == graph.Start)
                    {
                        built = true;
                        continue;
                    }

                    panel.Controls[graph.Vertices.IndexOf(current)].Controls[0].Invoke((MethodInvoker) delegate
                    {
                        panel.Controls[graph.Vertices.IndexOf(current)].Controls[0].Text = steps.ToString();
                    });
                    steps--;
                    if (current == graph.Start)
                    {
                        built = true;
                        continue;
                    }

                    current = current.Parent;
                }
            }
        }