Esempio n. 1
0
        public void Test_Maze_Travel()
        {
            var     graph = new SquireGridGraph(row, col, false);
            DisjSet ds    = new DisjSet(graph.VerticesNum());
            Random  r     = new Random(Environment.TickCount);

            while (ds.Find(0) != ds.Find(graph.VerticesNum() - 1))
            {
                int v1 = r.Next(0, graph.VerticesNum());
                if (graph.GetDegree(v1) >= 3)
                {
                    continue;
                }
                Direction direction = getRandomDirection();
                int       v2        = graph.GetNeighborVertex(v1, direction);
                if (v2 != -1)
                {
                    graph.SetEdge(v1, v2, 1);
                    ds.UnionSets(v1, v2);
                }
            }

            AStar aStar = new AStar(graph);

            if (aStar.Travel(0, graph.VerticesNum() - 1))
            {
                IList <int> result = aStar.reconstruct_path(graph.VerticesNum() - 1);

                Assert.IsTrue(result.Count > 0);
            }
            else
            {
                Assert.IsTrue(false);
            }
        }
Esempio n. 2
0
        public void Test_Maze_generate()
        {
            /**
             * 1. Éú³ÉSquareGridͼ
             *
             * */

            var     graph = new SquireGridGraph(row, col, false);
            DisjSet ds    = new DisjSet(graph.VerticesNum());
            Random  r     = new Random(Environment.TickCount);

            while (ds.Find(0) != ds.Find(graph.VerticesNum() - 1))
            {
                int v1 = r.Next(0, graph.VerticesNum());
                if (graph.GetDegree(v1) >= 3)
                {
                    continue;
                }
                Direction direction = getRandomDirection();
                int       v2        = graph.GetNeighborVertex(v1, direction);
                if (v2 != -1)
                {
                    graph.SetEdge(v1, v2, 1);
                    ds.UnionSets(v1, v2);
                }
            }

            Assert.IsTrue(graph.EdgeNum() > 1);
            Assert.IsTrue(ds.GetConnectedComponentNumber() > 1);
        }
Esempio n. 3
0
        private void genMazeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _graph = new SquireGridGraph(Row, Col, false);
            var ds = new DisjSet(_graph.VerticesNum());
            var r  = new Random(Environment.TickCount);

            while (ds.Find(0) != ds.Find(_graph.VerticesNum() - 1))
            {
                int v1 = r.Next(0, _graph.VerticesNum());
                if (_graph.GetDegree(v1) >= 3)
                {
                    continue;
                }
                Direction direction = getRandomDirection();
                int       v2        = _graph.GetNeighborVertex(v1, direction);
                if (v2 != -1)
                {
                    _graph.SetEdge(v1, v2, 1);
                    ds.UnionSets(v1, v2);
                }
            }

            var      pen    = new Pen(Color.Brown, 1);
            Brush    brush  = new SolidBrush(Color.Blue);
            var      bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            Graphics gg     = Graphics.FromImage(bitmap);

            for (int i = 1; i < _graph.VerticesNum() - 1; i++)
            {
                int row, col;
                _graph.GetRowAndColFromVertex(i, out row, out col);
                int v2 = _graph.GetNeighborVertex(i, Direction.West);
                if (v2 == -1 || !_graph.IsConnected(i, v2))
                {
                    // Draw Line
                    gg.DrawLine(pen, _margin + col * Radius, _margin + row * Radius,
                                _margin + col * Radius, _margin + row * Radius + Radius);
                }

                v2 = _graph.GetNeighborVertex(i, Direction.North);
                if (v2 == -1 || !_graph.IsConnected(i, v2))
                {
                    // Draw Line
                    gg.DrawLine(pen, _margin + col * Radius, _margin + row * Radius,
                                _margin + col * Radius + Radius, _margin + row * Radius);
                }

                v2 = _graph.GetNeighborVertex(i, Direction.East);
                if (v2 == -1 || !_graph.IsConnected(i, v2))
                {
                    // Draw Line
                    gg.DrawLine(pen, _margin + col * Radius + Radius, _margin + row * Radius,
                                _margin + col * Radius + Radius, _margin + row * Radius + Radius);
                }

                v2 = _graph.GetNeighborVertex(i, Direction.South);
                if (v2 == -1 || !_graph.IsConnected(i, v2))
                {
                    // Draw Line
                    gg.DrawLine(pen, _margin + col * Radius, _margin + row * Radius + Radius,
                                _margin + col * Radius + Radius, _margin + row * Radius + Radius);
                }
            }
            pictureBox1.Image = bitmap;
            pictureBox1.Refresh();
            pen.Dispose();
            brush.Dispose();
            gg.Dispose();
        }