public void MSTest_shortestpath() { Node node1 = new Node("1_1"); Node node2 = new Node("1_2"); Node node3 = new Node("1_3"); Node node4 = new Node("1_4"); Node node5 = new Node("1_5"); node2.connect(node1); node2.connect(node3); node4.connect(node1); node4.connect(node5); node5.connect(node3); Dungeon d = new Dungeon(); d.zone = new Dictionary <int, List <Node> >(); d.zone[1] = new List <Node>(); d.zone[1].Add(node1); d.zone[1].Add(node2); d.zone[1].Add(node3); d.zone[1].Add(node4); d.zone[1].Add(node5); List <Node> sp = d.shortestpath(node1, node3); List <Node> expectedSp = new List <Node>(); expectedSp.Add(node2); expectedSp.Add(node3); Assert.IsTrue(sp.Count == 2); Assert.IsTrue(sp[0] == expectedSp[0]); Assert.IsTrue(sp[1] == expectedSp[1]); }
public void NTest_shortestpath() { List <Node> nodes = new List <Node>(); Node n1, n2; Node startNode = new Node("" + 0, 1); nodes.Add(startNode); for (int i = 1; i <= 10; i++) { Node newNode = new Node("" + (i), 1); nodes.Add(newNode); } for (int i = 1; i <= 3; i++) { startNode.connect(nodes.ElementAt(i)); } for (int i = 3; i < 9; i++) { n1 = nodes.ElementAt(i + 1); n2 = nodes.ElementAt(i + 2); nodes.ElementAt(i).connect(n1); nodes.ElementAt(i).connect(n2); } List <Node> listToCheck = new List <Node>(); List <Node> pathToCheck = new List <Node>(); pathToCheck.Add(nodes.ElementAt(3)); listToCheck.Add(nodes.ElementAt(3)); for (int i = 4; i <= 10; i++) { listToCheck.Add(nodes.ElementAt(i)); i++; } Assert.AreSame(listToCheck, dungeon.shortestpath(nodes.ElementAt(3), nodes.ElementAt(10))); }
/*Create a random dungeon and choose a random path from it.*/ public void TestPath() { Prop.ForAll <uint, uint[], uint[]>( /*n is dungeon size, xs is random path from start, ys is random path from end * Intended to simulate selecting random nodes, but because shorter lists * are most likely generated more often, it's not completely random. * But that's not important for this test.*/ (n, xs, ys) => { if (n == 0) { return(false.When(false)); } var d = new Dungeon(n, 1); Node a = d.startNode; foreach (int x in xs) { a = a.neighbors[x % a.neighbors.Count]; } Node b = d.exitNode; foreach (int y in ys) { b = b.neighbors[y % b.neighbors.Count]; } return(p.isPath(Dungeon.shortestpath(a, b)).ToProperty()); } ).QuickCheckThrowOnFailure(); }
public static void DisplayPaths(Player player) { for (int t = 0; t < player.location.neighbors.Count; t++) { string text = ""; text += (t + 1) + ") Node " + player.location.neighbors[t].id + ", "; text += "Path length to exit is " + Dungeon.shortestpath(player.location.neighbors[t], Dungeon.current.exitNode).Count + "."; Console.WriteLine(text); } }
/* Move the pack one node further along a shortest path to u. */ public void moveTowards(Node u) { List <Node> path = dungeon.shortestpath(location, u); if (path.Count == 0) { return; } // first element of path is current node, so go to next one move(path[1]); }
public void NTest_moveTowards_validMove() { Dungeon dungeon = new Dungeon(3, 8); Pack pack1 = new Pack("1", 4, dungeon); pack1.location = dungeon.startNode; dungeon.startNode.packs.Add(pack1); Node nextNode = (dungeon.shortestpath(dungeon.startNode, dungeon.exitNode))[0]; Logger.log("Next node will be " + nextNode.id); Logger.log("exitnode is " + dungeon.exitNode.id); pack1.MoveTowards(dungeon.exitNode); Assert.AreSame(nextNode.id, pack1.location.id); }
/* Move the pack one node further along a shortest path to u. */ public void moveTowards(Node u, Node playerlocation = null) { //Console.WriteLine("from: " + this.location.id + " to: " + u.id); List <Node> path = dungeon.shortestpath(location, u).Distinct().ToList(); path.Reverse(); path.Remove(path[0]); if (!(path.Count > 0)) { return; } move(path[0], playerlocation); if (playerlocation != null) { playerlocation.contested = playerlocation.packs.Count > 0; } }
/*As far as I know, testing if a given path is the shortest is as * complex as finding the path in the first place. Meaning that making a * test in that way would be meaningless, considering that test containing * an error is just as likely. This means I can't use FsCheck, and will use * a regular unit test instead.*/ public void TestShortest() { Node[] nodes = new Node[5]; for (int i = 0; i < 5; i++) { nodes[i] = new Node(i.ToString()); } nodes[0].connect(nodes[1]); nodes[0].connect(nodes[2]); nodes[1].connect(nodes[2]); nodes[1].connect(nodes[3]); nodes[2].connect(nodes[3]); nodes[2].connect(nodes[4]); nodes[3].connect(nodes[4]); Assert.AreEqual( Dungeon.shortestpath(nodes[0], nodes[4]).Select(nd => nd.id).ToArray(), new string[] { "0", "2", "4" }); }
/* * A single update turn to the game. */ public Boolean update(Command userCommand) { //// Player Action ///// GUI(userCommand); if (player.location == dungeon.exitNode) { Console.WriteLine("Congratulations, you've succeeded and beat the dungeon!"); if (UI.writer != null) { Environment.Exit(0); } UI.ReadKey(); return(false); } // Cleans up all monsters that died for (int t = monsterPacks.Count - 1; t >= 0; t--) { if (monsterPacks[t].members.Count == 0) { monsterPacks.Remove(monsterPacks[t]); } } //// Monster Actions ///// foreach (Pack pack in monsterPacks) { if (Dungeon.shortestpath(pack.location, player.location) != null) { pack.prevLoc = pack.location; if (REndZone(pack)) // EndZone rule activated { pack.moveTowards(player.location); } else { // move or do nothing bool moving = RandomGenerator.rnd.NextDouble() > 0.5; if (moving) { if (RAlert(pack)) // if pack is in alarmed zone { pack.moveTowards(player.location); } else { bool moved = false; int tried = 0; List <Node> temp_neighbours = new List <Node>(pack.location.neighbors); while (!moved && tried != pack.location.neighbors.Count) // tries till success { int destination = RandomGenerator.rnd.Next(temp_neighbours.Count); Node node_destination = temp_neighbours[destination]; if (RZone(pack, node_destination)) // Stays in zone { moved = pack.move(node_destination); } temp_neighbours.RemoveAt(destination); tried++; } } } } } } return(true); }
public void MSTest_no_shortest_path() { Dungeon d = new Dungeon(5); List <Node> sp = d.shortestpath(d.zone[1][0], d.zone[1][3]); }
/*Because only block coverage is needed for this project, testing if * it fails for more complex graphs with disconnected nodes isn't necessary. * Just testing if it can fail at all is enough.*/ public void FailPath() { Assert.IsNull(Dungeon.shortestpath(new Node(), new Node())); }
/* Move the pack one node further along a shortest path to u. */ public void moveTowards(Node u) { List <Node> path = Dungeon.shortestpath(location, u); move(path[1]); // 0 or 1 depending whether own node included }
/* Move the pack one node further along a shortest path to u. */ public void moveTowards(Node u) { List <Node> path = dungeon.shortestpath(location, u); move(path[0]); }
private int distance(Node u, Player p) { return(Dungeon.shortestpath(u, p.location).Count); }