//Use this method to check earlier if the objects of npc and player are being stored in the list. Becomes pretty useless later on. public static void AccessObjectList(PlayerGenerator playergenerator, NPCGenerator[] npcgenerator) { IEnumerator<object> enumerator = list.GetEnumerator (); //Create enumerator for the linkedlist. while (enumerator.MoveNext ()) { object value = enumerator.Current; if (value.Equals (playergenerator)) { PlayerGenerator playerList = (PlayerGenerator)value; Console.WriteLine (playerList.Name + " " + playerList.xCoord + " " + playerList.yCoord); } for (int i = 0; i < NPCSize; i++) { if (value.Equals (npcgenerator [i])) { NPCGenerator npcList = (NPCGenerator)value; Console.WriteLine (npcList.Name + " " + npcList.xCoord + " " + npcList.yCoord); } } } }
//Function to sort the list by distance for each iteration. public static void DistanceSort(PlayerGenerator player, NPCGenerator[] npc) { NPCGenerator[] detectedNPC = new NPCGenerator[NPCSize]; //Store the npcs in range into this object for easier data abstraction. Dictionary<String, Int32> items = new Dictionary<String, Int32> (); //Use a dictionary so that we can store the value of the npcs in. Console.WriteLine ("Name: " + player.Name + " X: " + player.xCoord + " Y: " + player.yCoord); int withinDistance = 0, approxDist = 0; //Add the x and y coordinates together, to get the actual distance from the player. 0 to 20 should be the max range. for (int i = 0; i < NPCSize; i++) { //Find absolute value. Subtract the npcs x and y coordinates with the players and see if they are less than 10. if (Math.Abs (npc [i].xCoord - player.xCoord) <= 10 && Math.Abs (npc [i].yCoord - player.yCoord) <= 10) { detectedNPC [i] = npc [i]; withinDistance = Math.Abs (npc [i].xCoord - player.xCoord) + Math.Abs (npc [i].yCoord - player.yCoord); items.Add (detectedNPC [i].Name, withinDistance); } //For all the other values of distance that's greater than 10, add them to the dictionary. else { //Console.WriteLine ("Name: " + npc [i].Name + " X: " + npc [i].xCoord + " Y: " + npc [i].yCoord); approxDist = Math.Abs (npc [i].xCoord - player.xCoord) + Math.Abs (npc [i].yCoord - player.yCoord); items.Add (npc [i].Name, approxDist); } } //Use LINQ to sort the dictionary by ascending order var sortedDict = from entry in items orderby entry.Value ascending select entry; //Show the value of the detected npcs, and their range from the player. Also add to the DoublyLinkedList. Make sure to clear the previous list. list.Clear (); foreach (var value in sortedDict) { // Console.WriteLine(value); list.Add (value); } //Access the list to show the new sorted list. AccessList (); }
static void Main(string[] args) { PlayerGenerator playergenerator = new PlayerGenerator (); Stopwatch stopwatch = new Stopwatch (); //Generate npc's and see what they look like. // Create an array of objects to hold all our objects NPCGenerator[] npcgenerator = new NPCGenerator[NPCSize]; for (int i = 0; i < NPCSize; i++) { // Instantiate a new object everytime, and randomize the x and y coords, and the name. npcgenerator [i] = new NPCGenerator () { xCoord = GetRandomNumber (0, maxGridSize), yCoord = GetRandomNumber (0, maxGridSize), Name = RandomName (nameSize) }; } //Add the npc and the player to the DoublyLinkedList. for (int i = 0; i < NPCSize; i++) { //Console.WriteLine ("Name: " + npcgenerator [i].Name + " X: " + npcgenerator [i].xCoord + " Y: " + npcgenerator [i].yCoord); list.Add (npcgenerator [i]); } playergenerator.GeneratePlayer (); list.Add (playergenerator); //Start the timer and the main iteration for the npcs stopwatch.Reset (); stopwatch.Start (); for (int i = 0; i < 100; i++) { int moveGen; moveGen = GetRandomNumber (0, 4); switch (moveGen) { //Move right. case 0: if (playergenerator.xCoord < maxGridSize) { playergenerator.xCoord = playergenerator.xCoord + 1; } for (int j = 0; j < NPCSize; j++) { if (npcgenerator [j].xCoord < maxGridSize) { npcgenerator [j].xCoord = npcgenerator [j].xCoord + 1; } } Console.WriteLine ("Move right " + playergenerator.xCoord); break; //Move left. case 1: if (playergenerator.xCoord > 0) { playergenerator.xCoord = playergenerator.xCoord - 1; } for (int j = 0; j < NPCSize; j++) { if (npcgenerator [j].xCoord > 0) { npcgenerator [j].xCoord = npcgenerator [j].xCoord - 1; } } Console.WriteLine ("Move left " + playergenerator.xCoord); break; //Move up. case 2: if (playergenerator.yCoord > 0) { playergenerator.yCoord = playergenerator.yCoord - 1; } for (int j = 0; j < NPCSize; j++) { if (npcgenerator [j].yCoord > 0) { npcgenerator [j].yCoord = npcgenerator [j].yCoord - 1; } } Console.WriteLine ("Move up " + playergenerator.yCoord); break; //Move down. case 3: if (playergenerator.yCoord < maxGridSize) { playergenerator.yCoord = playergenerator.yCoord + 1; } for (int j = 0; j < NPCSize; j++) { if (npcgenerator [j].yCoord < maxGridSize) { npcgenerator [j].yCoord = npcgenerator [j].yCoord + 1; } } Console.WriteLine ("Move down " + playergenerator.yCoord); break; } //Code to test distance, and sort by distance. //Find the distance within 10 units // FindDistance (playergenerator, npcgenerator); //Console.WriteLine ("Name: " + npcgenerator [i].Name + " X: " + npcgenerator [i].xCoord + " Y: " + npcgenerator [i].yCoord); //Sort the list by distance from the player //DistanceSort(playergenerator,npcgenerator); } stopwatch.Stop (); Console.WriteLine ("Duration: " + stopwatch.Elapsed); /* Reversing a doubly linked list. Console.WriteLine ("\n Start of list: \n"); AccessObjectList (playergenerator, npcgenerator); Console.WriteLine ("\n Reversing list: \n"); list.reverseHappen (); AccessObjectList (playergenerator, npcgenerator); */ Console.ReadLine (); }
//This method returns all the npcs within 10 units from the player. public static void FindDistance(PlayerGenerator player, NPCGenerator[] npc) { NPCGenerator[] detectedNPC = new NPCGenerator[NPCSize]; //Store the npcs in range into this object for easier data abstraction. Console.WriteLine ("Name: " + player.Name + " X: " + player.xCoord + " Y: " + player.yCoord); for (int i = 0; i < NPCSize; i++) { //Console.WriteLine ("Name: " + npc [i].Name + " X: " + npc [i].xCoord + " Y: " + npc [i].yCoord); //Find absolute value. Subtract the npcs x and y coordinates with the players and see if they are less than 10. if (Math.Abs (npc [i].xCoord - player.xCoord) <= 10 && Math.Abs (npc [i].yCoord - player.yCoord) <= 10) { detectedNPC [i] = npc [i]; Console.WriteLine ("Detected NPC: " + detectedNPC [i].Name + " X Coord: " + detectedNPC [i].xCoord + " Y Coord: " + detectedNPC [i].yCoord + " #: " + i); } } }