public List <IDroid> addDroid() { IDroid droid = x.createDroid(); droids.Add(droid); return(droids); }
/// <summary> /// Method which steps through the passed in collection and only returns an array with usable data /// in each cell /// </summary> /// <param name="droidCollection"></param> private static IDroid[] RemoveNull(IDroid[] droidCollection) { IDroid[] temp = new IDroid[1]; int i = 0; foreach (IDroid droid in droidCollection) { if (droid != null) { if (i < temp.Length) { temp[i] = droid; i++; } else { Array.Resize <IDroid>(ref temp, temp.Length + 1); temp[i] = droid; i++; } } } droidCollection = new IDroid[temp.Length]; for (int k = 0; k < temp.Length; k++) { droidCollection[k] = temp[k]; } return(droidCollection); }
//****************** //CompareTo //****************** //CompareTo Method for merge sort that satisfies IComparable. public int CompareTo(Object obj) { if (obj == null) { return(1); } //if totalCost has never been calculated for this drone //note: as it was, totalCost was never calculated until it was displayed. //so this is there if this option is selected without that if (this.totalCost == 0) { CalculateTotalCost(); } //assign obj as an IDroid IDroid dro = obj as IDroid; if (dro != null) { //if totalCost has never been calculated for the drone we are comparing to if (dro.TotalCost == 0) { dro.CalculateTotalCost(); } //compares the two decimal values and will return an accurate value return(this.totalCost.CompareTo(dro.TotalCost)); } else { throw new Exception("Failure, everything is trash"); } }
//Sort by total cost public void sortByTotalCost() { //Create a new Mergsort object MergSort sorter = new MergSort(); //Create a new array to hold only the valid droids which would be the length of the collection IDroid[] realDroids = new IDroid[lengthOfCollection]; //copy the valid indexs into the array for (int i = 0; i <= realDroids.Length; i++) { //copy only if null if (droidCollection[i] != null) { realDroids[i] = droidCollection[i]; } } //create a new array to hold the sorted values IComparable[] x = sorter.merge(realDroids, 0, realDroids.Length); //print out the values foreach (IDroid y in x) { Console.WriteLine("\n*****************************\n" + y.ToString() + "\n" + y.TotalCost + "\n*****************************"); } }
/*************************************************************************** * Helper sorting function. ***************************************************************************/ /// <summary> /// This method does a null check on IDroid v. If v is null, it returns false. /// It then returns a bool based on the result of the CompareTo method in the /// Droid class. /// </summary> /// <param name="v"></param> /// <param name="w"></param> /// <returns>bool</returns> private static bool Less(IDroid v, IDroid w) { if (v == null) { return(false); } return(v.CompareTo(w) < 0); }
/// <summary> /// Rearranges the array in ascending order, using the natural order. /// This method is called from the MergeSort constructor. /// The two Sort methods have different signatures, so the recursive call will be made /// to the Srt method Sort(IDroid[] a, IDroid[] aux, int lo, int hi). /// </summary> /// <param name="a"></param> private static void Sort(IDroid[] a) { // create an IDroid[], aux, equal in length to a[] IDroid[] aux = new IDroid[a.Length]; // sort ausing auxiliary array aux, starting at zero, with the length of array a minus one Sort(a, aux, 0, a.Length - 1); }
public int CompareTo(Object obj) { IDroid anotherDroid = (IDroid)obj; decimal EndCost = this.TotalCost; decimal costTotal = anotherDroid.TotalCost; return(EndCost.CompareTo(costTotal)); }
public static string StoredEnergyText(IDroid droid) { return(string.Concat(new string[] { "PowerBatteryStored".Translate(), ": ", droid.StoredEnergy.ToString("######0.0"), " / ", DroidPawn.storedEnergyMax.ToString("######0.0"), " Wd" })); }
/// <summary> /// Sends each droid in the queue created by the GenericQueue class into the droids array /// by using a while loop to determine if the queue size is zero, setting the droid in /// index i equal to that droid in the queue, and then incrementing the counter. /// This method is protected and can only be accessed via the CategorizeByModel method. /// </summary> protected void QueueToArray() { int i = 0; while (droidQueue.Size != 0) { IDroid droid = droidQueue.Dequeue(); droids[i] = droid; i++; } }
public void AddDroid(IDroid droid) { bool droidAdded = false; for (int i = 0; i < droidArray.Length; i++) { if (droidArray[i] == null && droidAdded == false) //only add a droid if the spot is empty and a droid has not yet been added { droidArray[i] = droid; arrayLength++; droidAdded = true; } } }
public int CompareTo(object obj) { IDroid tempDroid = (IDroid)obj; if (TotalCost < tempDroid.TotalCost) { return(-1); } if (TotalCost == tempDroid.TotalCost) { return(0); } return(1); }
public void SortByPrice() { MergeSort mergeSorter = new MergeSort(); IDroid[] droidCollectionTrimmed = new IDroid[lengthOfCollection]; for (int a = 0; a < lengthOfCollection; a++) { //This weeds out all of the null values, that mess up the merge sort if (droidCollection[a] != null) { droidCollectionTrimmed[a] = droidCollection[a]; } } droidCollection = (IDroid[])mergeSorter.sortArray(droidCollectionTrimmed); }
public override void CompTick() { base.CompTick(); // Find a Droid exactly on our position // - keep charging the same droid if there are multiple if (found != null && (found.Position != this.parent.Position || found.destroyed)) { found = null; } if (found == null) { found = Find.ThingGrid.ThingAt <DroidPawn> (this.parent.Position); } if (found == null) { found = Find.ThingGrid.ThingAt <DroidInactive> (this.parent.Position); } // Find the CompPowerTrader CompPowerTrader comp = this.parent.GetComp <CompPowerTrader> (); if (comp == null) { Log.Error("CompDroidCharger in " + parent.def.defName + " needs a CompPowerTrader to function"); return; } float rate = 0.01f; // use 1% when not charging if (found != null) { rate = 1f; IDroid droid = (IDroid)found; if (comp.PowerOn && droid.StoredEnergy < DroidPawn.storedEnergyMax) { droid.StoredEnergy += comp.def.basePowerConsumption * comp.def.efficiency * CompPower.WattsToWattDaysPerTick; } } // set power consumption comp.powerOutput = -rate * comp.def.basePowerConsumption; }
/// <summary> /// This merge method will use the properties of IComparable to compre two droids based on total cost and then /// merge the arrays back together in sorted order /// </summary> /// <param name="droidCollection"></param> /// <param name="lo"></param> /// <param name="mid"></param> /// <param name="hi"></param> private static void merge(IDroid[] droidCollection, int lo, int mid, int hi) { //Declaring the temporary array for storing sorted values IDroid[] tempCollection = new IDroid[droidCollection.Length]; //Setting indeces for the original and temp array for stepping through each part individually int leftIndex = lo; int rightIndex = mid + 1; int tempIndex = lo; //While loop to maintain the method until all objects have been compared while (leftIndex <= mid && rightIndex <= hi) { //Compare the two objects then place them into the temporary array as sorted if ((CompareTo(droidCollection, leftIndex, mid)) == 1) { tempCollection[tempIndex++] = droidCollection[lo++]; } else { tempCollection[tempIndex++] = droidCollection[mid++]; } if (leftIndex == rightIndex) { mid = mid++; while (rightIndex <= hi) { if ((CompareTo(droidCollection, lo, mid)) == 1) { tempCollection[tempIndex++] = droidCollection[rightIndex++]; } else { tempCollection[tempIndex++] = droidCollection[mid++]; } } } } for (int k = 0; k < tempCollection.Length; k++) { droidCollection[k] = tempCollection[k]; } }
//implement compareTo method public int CompareTo(object obj) { if (obj == null) { return(1); } IDroid otherDroid = obj as IDroid; this.CalculateTotalCost(); otherDroid.CalculateTotalCost(); if (otherDroid != null) { return(this.totalCost.CompareTo(otherDroid.TotalCost)); } else { throw new ArgumentException("Object is not Droid"); } }
/// <summary> /// Adds a single droid to the current list. /// </summary> /// <param name="aDroid"></param> public void AddDroid(IDroid aDroid) { // If there is room for more items in array. if (droidListSizeInt < lenghtOfArrayInt) { indexInt = 0; // While current spot is not empty; while (droidCollection[indexInt] != null) { indexInt++; } // Adds droid to first empty spot. droidCollection[indexInt] = aDroid; droidListSizeInt++; } else { ExpandArray(); AddDroid(aDroid); } }
public HumanoidAdapter(IDroid wildDogs) { this._droid = wildDogs; }
//Sorting droid collection by categorizing public void sortByModel() { //create 4 stacks for each type of droid Stack <IDroid> ProtocolStack = new Stack <IDroid>(); Stack <IDroid> UtilityStack = new Stack <IDroid>(); Stack <IDroid> JanitorStack = new Stack <IDroid>(); Stack <IDroid> AstromechStack = new Stack <IDroid>(); //Foreach loop that gets model for each droid in the collection and compares it to put it into the proper stack foreach (IDroid x in droidCollection) { if (x != null) { if (x.Model.Equals("Astromech")) { AstromechStack.push(x); } if (x.Model.Equals("Janitor")) { JanitorStack.push(x); } if (x.Model.Equals("Utility")) { UtilityStack.push(x); } if (x.Model.Equals("Protocol")) { ProtocolStack.push(x); } } } //Create new queue to store stack in desired order Queue <IDroid> DroidQueue = new Queue <IDroid>(); //enqueue Astromechs while (!AstromechStack.isEmpty()) { DroidQueue.enqueue(AstromechStack.pop()); } //enqueue janitors while (!JanitorStack.isEmpty()) { DroidQueue.enqueue(JanitorStack.pop()); } //enqueue utilitys while (!UtilityStack.isEmpty()) { DroidQueue.enqueue(UtilityStack.pop()); } //enqueue protocols while (!ProtocolStack.isEmpty()) { DroidQueue.enqueue(ProtocolStack.pop()); } //Create new arr to store sorted values in queue IDroid[] arr = new IDroid[lengthOfCollection]; //for loop to transfer values from queue to arr for (int i = 0; i < lengthOfCollection; i++) { arr[i] = DroidQueue.dequeue(); } // Print new sorted array foreach (IDroid y in arr) { Console.WriteLine("***************************\n" + y.ToString() + "\n" + y.TotalCost + "\n***************************"); } }
private static void AddDroid(DroidCollecion droidCollection, UserInterface userInterface) { //istantiate variables DroidCollecion dc = droidCollection; UserInterface ui = userInterface; IDroid droid = null; string droidMaterial; string droidColor; string droidType; int droidNumberLanguages; bool droidToolBox; bool droidComputerConnection; bool droidArm; bool droidFireExtingusher; int droidNumberShips; bool droidTrashCompactor; bool droidVacuum; try //catch incorrect entries { ui.DroidMaterial(); droidMaterial = ui.ReadLine(); ui.DroidColor(); droidColor = ui.ReadLine(); ui.DroidType(); droidType = ui.ReadLine(); if (droidType == "Astromech") //astromech path { //utility variables ui.ToolBoxAsk(); droidToolBox = Convert.ToBoolean(ui.ReadLine()); ui.ComputerConnectionAsk(); droidComputerConnection = Convert.ToBoolean(ui.ReadLine()); ui.ArmAsk(); droidArm = Convert.ToBoolean(ui.ReadLine()); //Astromech variables ui.FireExtinguisher(); droidFireExtingusher = Convert.ToBoolean(ui.ReadLine()); ui.NumberShips(); droidNumberShips = Convert.ToInt32(ui.ReadLine()); //create new astromech in the droid variable droid = new Astromech(droidMaterial, droidType, droidColor, droidToolBox, droidComputerConnection, droidArm, droidFireExtingusher, droidNumberShips); } else if (droidType == "Janitor") //janitor path { //Utitlity variables ui.ToolBoxAsk(); droidToolBox = Convert.ToBoolean(ui.ReadLine()); ui.ComputerConnectionAsk(); droidComputerConnection = Convert.ToBoolean(ui.ReadLine()); ui.ArmAsk(); droidArm = Convert.ToBoolean(ui.ReadLine()); //Janitor variables ui.TrashCompactor(); droidTrashCompactor = Convert.ToBoolean(ui.ReadLine()); ui.Vacuum(); droidVacuum = Convert.ToBoolean(ui.ReadLine()); //Create new janitor in the droid variable droid = new Janitor(droidMaterial, droidType, droidColor, droidToolBox, droidComputerConnection, droidArm, droidTrashCompactor, droidVacuum); } else if (droidType == "Protocol") //protocol path { //Protocol variables ui.DroidLanguages(); droidNumberLanguages = Convert.ToInt32(ui.ReadLine()); //create new Protocol droid in the droid variable droid = new Protocol(droidMaterial, droidType, droidColor, droidNumberLanguages); } else { ui.DroidTypeError(); //output type error message } } catch { ui.Print("Invalid entry"); } if (droid == null) //if no droid was made do not add it to the list { ui.DroidNotAdded(); } else { //if the droid is made add it to the list } { dc.AddDroid(droid); ui.DroidAdded(); } }
public IDroid createDroid() { Console.WriteLine("pick model:\n1 R2D2\n2 C3P0\n3 BB-8"); int modelNum = Convert.ToInt32(Console.ReadLine()); switch (modelNum) { case 1: model = "R2D2"; break; case 2: model = "C3P0"; break; case 3: model = "BB-8"; break; } Console.WriteLine("pick material:\n1 titanium\n2 aluminum\n3 steel"); int materialNum = Convert.ToInt32(Console.ReadLine()); switch (materialNum) { case 1: material = "titanium"; break; case 2: material = "aluminum"; break; case 3: material = "steel"; break; } Console.WriteLine("Choose custom color"); color = Console.ReadLine(); Console.WriteLine("Pick droid type\n1 Protocol\n2 janitor\n3 astromech"); int droidtype = Convert.ToInt32(Console.ReadLine()); switch (droidtype) { case 1: Console.WriteLine("enter number of languages"); int numLang = Convert.ToInt32(Console.ReadLine()); x = new Protocol(material, model, color, numLang); break; case 2: Console.WriteLine("would you connection?\n1 yes\n2 no"); connectionNum = Convert.ToInt32(Console.ReadLine()); if (connectionNum == 1) { connection = true; } Console.WriteLine("would you toolbox?\n1 yes\n2 no"); toolNum = Convert.ToInt32(Console.ReadLine()); if (toolNum == 1) { toolbox = true; } Console.WriteLine("would you arm?\n1 yes\n2 no"); armNum = Convert.ToInt32(Console.ReadLine()); if (armNum == 1) { arm = true; } Console.WriteLine("would you vacuum?\n1 yes\n2 no"); int vacNum = Convert.ToInt32(Console.ReadLine()); if (vacNum == 1) { vacuum = true; } Console.WriteLine("would you trashCompactor?\n1 yes\n2 no"); int trashNum = Convert.ToInt32(Console.ReadLine()); if (trashNum == 1) { trashCompactor = true; } x = new Janitor(material, model, color, connection, toolbox, arm, trashCompactor, vacuum); droid = x.ToString(); break; case 3: Console.WriteLine("would you connection?\n1 yes\n2 no"); connectionNum = Convert.ToInt32(Console.ReadLine()); if (connectionNum == 1) { connection = true; } Console.WriteLine("would you toolbox?\n1 yes\n2 no"); toolNum = Convert.ToInt32(Console.ReadLine()); if (toolNum == 1) { toolbox = true; } Console.WriteLine("would you arm?\n1 yes\n2 no"); armNum = Convert.ToInt32(Console.ReadLine()); if (armNum == 1) { arm = true; } Console.WriteLine("Would you like a fire exstingusiher?\n1 yes\n2 no"); int fireNum = Convert.ToInt32(Console.ReadLine()); if (fireNum == 1) { fireEx = true; } Console.WriteLine("how many ships do you want?"); numberOfShips = Convert.ToInt32(Console.ReadLine()); x = new AstroMech(material, model, color, connection, toolbox, arm, numberOfShips, fireEx); droid = x.ToString(); break; } return(x); }
/// <summary> /// Public method to Sort the droids into categories using a modified bucket sort /// </summary> public void SortIntoCategories() { // Create a generic stack for each type of droid, and pass in the droid type as the generic that will // come through on the stack class as T. GenericStack <ProtocolDroid> protocolStack = new GenericStack <ProtocolDroid>(); GenericStack <UtilityDroid> utilityStack = new GenericStack <UtilityDroid>(); GenericStack <JanitorDroid> janitorStack = new GenericStack <JanitorDroid>(); GenericStack <AstromechDroid> astromechStack = new GenericStack <AstromechDroid>(); // Create a queue to hold the droids as we pop them off the stack. GenericQueue <IDroid> categorizedDroidQueue = new GenericQueue <IDroid>(); // For each IDroid in the droidCollection foreach (IDroid droid in this.droidCollection) { // If the droid is not null we want to process it. If it is null we will go to the else if (droid != null) { // The testing of the droids must occur in this order. It must be done in the order of // most specific droid to least specific. // If we were to test a droid that IS of type Astromech against Utility BEFORE we test against // Astromech, it would pass and be put into the Utility stack and not the Astromech. That is why it // is important to test from most specific to least. // If the droid is an Astromech, push it on the astromech stack if (droid is AstromechDroid) { astromechStack.Push((AstromechDroid)droid); } // Else if it is a JanitorDroid, push it on the janitor stack else if (droid is JanitorDroid) { janitorStack.Push((JanitorDroid)droid); } // Do for Utility else if (droid is UtilityDroid) { utilityStack.Push((UtilityDroid)droid); } // Do for Protocol else if (droid is ProtocolDroid) { protocolStack.Push((ProtocolDroid)droid); } } // The droid we are trying to consider is null, break out of the loop. else { break; } } // Now that the droids are all in thier respective stacks we can do the work // of poping them off of the stacks and adding them to the queue. // It is required that they be popped off from each stack in this order so that they have // the correct order going into the queue. // This is a primer pop. It gets the first droid off the stack, which could be null if the stack is empty AstromechDroid currentAstromechDroid = astromechStack.Pop(); // While the droid that is popped off is not null while (currentAstromechDroid != null) { // Add the popped droid to the queue. categorizedDroidQueue.Enqueue(currentAstromechDroid); // Pop off the next droid for the loop test currentAstromechDroid = astromechStack.Pop(); } // See above method for Astromech. It is the same except for Janitor JanitorDroid currentJanitorDroid = janitorStack.Pop(); while (currentJanitorDroid != null) { categorizedDroidQueue.Enqueue(currentJanitorDroid); currentJanitorDroid = janitorStack.Pop(); } // See above method for Astromech. It is the same except for Utility UtilityDroid currentUtilityDroid = utilityStack.Pop(); while (currentUtilityDroid != null) { categorizedDroidQueue.Enqueue(currentUtilityDroid); currentUtilityDroid = utilityStack.Pop(); } // See above method for Astromech. It is the same except for Protocol ProtocolDroid currentProtocolDroid = protocolStack.Pop(); while (currentProtocolDroid != null) { categorizedDroidQueue.Enqueue(currentProtocolDroid); currentProtocolDroid = protocolStack.Pop(); } // Now that the droids have all been removed from the stacks and put into the queue // we need to dequeue them all and put them back into the original array. // Set a int counter to 0. int counter = 0; // This is a primer dequeue that will get the first droid out of the queue. IDroid iDroid = categorizedDroidQueue.Dequeue(); // While the dequeued droid is not null. while (iDroid != null) { // Add the droid to the droid collection using the int counter as the index this.droidCollection[counter] = iDroid; // Increment the counter counter++; // Dequeue the next droid off the queue so it can be used in the while condition iDroid = categorizedDroidQueue.Dequeue(); } // Set the length of the collection to the value of the counter. It should be the same, but in case it changed. this.lengthOfCollection = counter; }
static void Main(string[] args) { // Create class instances UserInterface ui = new UserInterface(); //Array IDroid[] droidList = new IDroid[20]; DroidCollection droidCollect = new DroidCollection(); int length = 0; string output = ""; //I dont think I needed this, but I was getting some weird formatting issues intially Console.BufferHeight = 1000; //Header (cause console programs are boring) ui.Output("************************************************************" + Environment.NewLine + "********* CIS237 *********" + Environment.NewLine + "******** Assignment #3 ********" + Environment.NewLine + "******* Jawa Inventory Managment *******" + Environment.NewLine + "************************************************************" + Environment.NewLine); //Return tag MenuReturn: int choice = ui.GetMainInput(); //Keep running menu untill user exits while (choice != 3) { //****************************************** //Print option //****************************************** if (choice == 1) { Console.Clear(); foreach (IDroid droid in droidList) { if (droid != null) { output += droid.ToString(); } } Console.WriteLine(output); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); Console.Clear(); goto MenuReturn; } //**************************************** //Add Droid Option //**************************************** if (choice == 2) { Console.Clear(); droidCollect.AddDroid(droidList, length); length++; Console.WriteLine(""); Console.WriteLine("Complete! Press any key to continue..."); Console.ReadKey(); Console.Clear(); goto MenuReturn; } Environment.Exit(0); } }
/// <summary> /// Generic version of AddDroid /// </summary> /// <param name="droid">The droid to add</param> public void AddDroid(IDroid droid) { this.droids[this.index++] = droid; }