Пример #1
0
        public void Test1()
        {
            var graph = new Graph();

            var a = new StringVertex("A");
            var b = new StringVertex("B");
            var c = new StringVertex("C");
            var d = new StringVertex("D");

            graph.AddVertex(a);
            graph.AddVertex(b);
            graph.AddVertex(c);
            graph.AddVertex(d);

            graph.AddEdge(new Edge(a, b));
            //graph.AddEdge(new Edge(a, c));
            graph.AddEdge(new Edge(a, d));
            graph.AddEdge(new Edge(b, c));
            graph.AddEdge(new Edge(b, d));
            graph.AddEdge(new Edge(c, d));

            Console.WriteLine(GraphPrinter.ToDot(graph));

            var cycles = graph.GetCycles(true);

            Assert.IsTrue(cycles.Count == 2);
            Assert.IsTrue(cycles[0].EdgeCount == 3);
            Assert.IsTrue(cycles[1].EdgeCount == 3);

            List <List <Edge> > overlap = cycles[0].Overlap(cycles[1]);

            Assert.IsTrue(overlap.Count == 1);
            Assert.IsTrue(overlap[0].Count == 1);
        }
Пример #2
0
        public void Print2()
        {
            var graph = new Graph();

            var v1 = new StringVertex("blue");

            graph.AddVertex(v1);
            var v2 = new StringVertex("green");

            graph.AddVertex(v2);
            var v3 = new StringVertex("yellow");

            graph.AddVertex(v3);
            var v4 = new StringVertex("yellow");

            graph.AddVertex(v4);
            var v5 = new StringVertex("blue");

            graph.AddVertex(v5);
            var v6 = new StringVertex("green");

            graph.AddVertex(v6);

            graph.AddEdge(new StringEdge(v1, v4, "green"));
            graph.AddEdge(new StringEdge(v2, v5, "yellow"));
            graph.AddEdge(new StringEdge(v3, v6, "blue"));
            graph.AddEdge(new StringEdge(v4, v5, "green"));
            graph.AddEdge(new StringEdge(v5, v6, "yellow"));
            graph.AddEdge(new StringEdge(v6, v4, "blue"));

            string graphString = GraphPrinter.ToDot(graph, true, true);

            File.WriteAllText("print2.gv", graphString);
        }
Пример #3
0
        public void Print1()
        {
            var graph = new Graph();

            var v1 = new IntVertex(1);

            graph.AddVertex(v1);
            var v2 = new IntVertex(2);

            graph.AddVertex(v2);
            var v3 = new IntVertex(3);

            graph.AddVertex(v3);
            var v4 = new IntVertex(2);

            graph.AddVertex(v4);
            var v5 = new IntVertex(3);

            graph.AddVertex(v5);
            var v6 = new IntVertex(2);

            graph.AddVertex(v6);

            var e1 = new Edge(v1, v2);

            graph.AddEdge(e1);
            var e2 = new Edge(v2, v3);

            graph.AddEdge(e2);
            var e3 = new Edge(v1, v4);

            graph.AddEdge(e3);
            var e4 = new Edge(v2, v5);

            graph.AddEdge(e4);
            var e5 = new Edge(v3, v6);

            graph.AddEdge(e5);
            var e6 = new Edge(v4, v5);

            graph.AddEdge(e6);
            var e7 = new Edge(v5, v6);

            graph.AddEdge(e7);

            string graphString = GraphPrinter.ToDot(graph);

            File.WriteAllText("print1.gv", graphString);
        }
Пример #4
0
        public void Test()
        {
            Dungeon = new Graph();

            int victoryPathLength = 10;

            REssential[] victoryPath = new REssential[victoryPathLength];
            for (int i = 0; i < victoryPathLength; i++)
            {
                victoryPath[i] = new REssential();
                Dungeon.AddVertex(victoryPath[i]);
                if (i > 0)
                {
                    Dungeon.AddEdge(new Edge(victoryPath[i], victoryPath[i - 1]));
                }
            }

            for (int i = 0; i < 2; i++)
            {
                var builder = new ReplacementRuleBuilder();

                builder.MappedVertex <REssential>("loop")
                .MappedVertexWithEdge <REssential, Edge>()
                .MappedVertexWithEdge <REssential, Edge>()
                .MappedVertexWithEdge <REssential, Edge>()
                .ReplacementVertexWithEdge <RBasic, Edge>()
                .ReplacementEdge <Edge>()
                .MoveToTag("loop");

                ReplacementRule addShortcut = builder.GetResult();

                Assert.IsTrue(addShortcut.Pattern.Vertices.Count == 4);
                Assert.IsTrue(addShortcut.Mapping.Count == 4);
                Assert.IsTrue(addShortcut.Replacement.Vertices.Count == 5);

                bool success = Dungeon.Replace(addShortcut, randomMatch: true);
                Assert.IsTrue(success);
            }

            var bossTreasure = new ReplacementRule();

            var anchorP = new RBasic();

            bossTreasure.Pattern.AddVertex(anchorP);

            var anchorR  = new RBasic();
            var boss     = new RBoss();
            var treasure = new RTreasure();

            bossTreasure.Replacement.AddVertex(anchorR);
            bossTreasure.Replacement.AddVertex(boss);
            bossTreasure.Replacement.AddVertex(treasure);
            bossTreasure.Replacement.AddEdge(new Edge(anchorR, boss));
            bossTreasure.Replacement.AddEdge(new Edge(boss, treasure));
            bossTreasure.Mapping[anchorP] = anchorR;

            Assert.IsTrue(bossTreasure.Pattern.Vertices.Count == 1);
            Assert.IsTrue(bossTreasure.Mapping.Count == 1);
            Assert.IsTrue(bossTreasure.Replacement.Vertices.Count == 3);

            bool success2 = Dungeon.Replace(bossTreasure, randomMatch: true);

            Assert.IsTrue(success2);

            File.WriteAllText("dungeon.gv", GraphPrinter.ToDot(Dungeon));
        }
Пример #5
0
        public void Test1()
        {
            //   1
            //  /  \
            // 3 -- 2
            Graph graph = new Graph();
            var   v1    = new Vertex();
            var   v2    = new Vertex();
            var   v3    = new Vertex();

            graph.AddVertex(v1);
            graph.AddVertex(v2);
            graph.AddVertex(v3);
            graph.AddEdge(new Edge(v1, v2));
            graph.AddEdge(new Edge(v2, v3));
            graph.AddEdge(new Edge(v3, v1));

            string initialGraphString = GraphPrinter.ToDot(graph);

            // 1 -- 2
            Graph pattern = new Graph();
            var   vp1     = new Vertex();
            var   vp2     = new Vertex();

            pattern.AddVertex(vp1);
            pattern.AddVertex(vp2);

            pattern.AddEdge(new Edge(vp1, vp2));

            // 1 -- 2 -- 3
            Graph replacement = new Graph();
            var   vr1         = new Vertex();
            var   vr2         = new Vertex();
            var   vr3         = new Vertex();

            replacement.AddVertex(vr1);
            replacement.AddVertex(vr2);
            replacement.AddVertex(vr3);

            replacement.AddEdge(new Edge(vr1, vr2));
            replacement.AddEdge(new Edge(vr2, vr3));

            var mapping = new Dictionary <Vertex, Vertex>
            {
                { vp1, vr1 },
                { vp2, vr3 }
            };

            graph.Replace(pattern, replacement, mapping, true);

            Assert.IsTrue(graph.Vertices.Count == 4);
            Assert.IsTrue(graph.Vertices.TrueForAll(v => v.Edges.Count == 2));

            Assert.IsTrue(graph.Vertices.Contains(vr1));
            Assert.IsTrue(graph.Vertices.Contains(vr2));
            Assert.IsTrue(graph.Vertices.Contains(vr3));

            string afterReplaceString = GraphPrinter.ToDot(graph);

            File.WriteAllText("before.gv", initialGraphString);
            File.WriteAllText("after.gv", afterReplaceString);
        }
Пример #6
0
        public void Start()
        {
            if (seed == 0)
            {
                seed = (int)System.DateTime.Now.Ticks;
            }

            Console.WriteLine("Seed: " + seed);
            rnd = new Random(seed);

            var builder = new ReplacementRuleBuilder();

            builder.MappedVertex <StartingRoom>("start")
            .ReplacementVertexWithEdge <BasicRoom, Edge>().ReplacementVertexWithEdge <BasicRoom, Edge>().MoveToTag("start")
            .ReplacementVertexWithEdge <BasicRoom, Edge>().ReplacementVertexWithEdge <BasicRoom, Edge>().MoveToTag("start")
            .ReplacementVertexWithEdge <BasicRoom, Edge>().ReplacementVertexWithEdge <BasicRoom, Edge>();

            var initialRule = builder.GetResult();

            var dungeon = new Graph();

            dungeon.AddVertex(new StartingRoom());
            dungeon.Replace(initialRule, true);

            for (int i = 0; i < 15; i++)
            {
                builder.Reset()
                .MappedVertex <BasicRoom>("a")
                .PatternVertexWithEdge <BasicRoom, Edge>("b")
                .MoveToTag("a").ReplacementVertexWithEdge <Junction, Edge>("j")
                .ReplacementVertexWithEdge <BasicRoom, Edge>().MoveToTag("j")
                .ReplacementVertexWithEdge <BasicRoom, Edge>().MapToTag("b");

                var addJunction = builder.GetResult();

                builder.Reset()
                .MappedVertex <BasicRoom>("a")
                .PatternVertexWithEdge <BasicRoom, Edge>("b").MoveToTag("a")
                .ReplacementVertexWithEdge <BasicRoom, Edge>()
                .ReplacementVertexWithEdge <BasicRoom, Edge>().MapToTag("b");

                var stretch = builder.GetResult();

                builder.Reset()
                .MappedVertex <BasicRoom>("a")
                .PatternVertexWithEdge <Junction, Edge>("j")
                .PatternVertexWithEdge <BasicRoom, Edge>("b").MoveToTag("j")
                .PatternVertexWithEdge <BasicRoom, Edge>("c").MoveToTag("a")
                .ReplacementVertexWithEdge <BasicRoom, Edge>().MapToTag("b")
                .ReplacementVertexWithEdge <BasicRoom, Edge>().MapToTag("c")
                .ReplacementEdge <Edge>().MoveToTag("a");

                var transformJunction = builder.GetResult();

                builder.Reset()
                .MappedVertex <BasicRoom>("a")
                .MappedVertexWithEdge <BasicRoom, Edge>()
                .MappedVertexWithEdge <BasicRoom, Edge>()
                .MappedVertexWithEdge <BasicRoom, Edge>()
                .ReplacementEdge <Edge>().MoveToTag("a");

                var createLoop = builder.GetResult();

                builder.Reset()
                .MappedVertex <BasicRoom>()
                .ReplacementVertexWithEdge <BasicRoom, Edge>();

                var addRoom = builder.GetResult();

                var rules = new Tuple <ReplacementRule, int>[]
                {
                    Tuple.Create(addJunction, 3),
                    Tuple.Create(stretch, 2),
                    Tuple.Create(createLoop, 2),
                    Tuple.Create(transformJunction, 1)
                };

                int   acc = 0;
                int[] absoluteDistribution = rules.Select(t => acc += t.Item2).ToArray();

                int  endurance = 10;
                int  ruleIndex;
                bool ruleSuccess;

                do
                {
                    if (endurance-- == 0)
                    {
                        dungeon.Replace(addRoom, true);
                        break;
                    }

                    int r = rnd.Next(acc);

                    for (ruleIndex = 0; ruleIndex < rules.Length; ruleIndex++)
                    {
                        if (r < absoluteDistribution[ruleIndex])
                        {
                            break;
                        }
                    }

                    ruleSuccess = dungeon.Replace(rules[ruleIndex].Item1, true);
                } while (!ruleSuccess);
            }

            File.WriteAllText("advancedDungeon.gv", GraphPrinter.ToDot(dungeon));
        }